Quiz: Libraries and Virtual Environments
Test your understanding before moving on. Target: 70% or higher to proceed confidently.
Section 1: Multiple Choice (1 point each)
1. What is the primary purpose of a virtual environment?
- A) To make Python run faster
- B) To isolate project dependencies from the system Python and other projects
- C) To encrypt your Python source code
- D) To allow running Python 2 and Python 3 simultaneously
Answer
**B)** To isolate project dependencies from the system Python and other projects *Why B:* Virtual environments create an isolated Python installation with its own packages, preventing version conflicts between projects. *Why not A:* Virtual environments don't affect performance. *Why not C:* Virtual environments don't provide encryption. *Why not D:* While separate Python versions can coexist, that's managed by tools like `pyenv`, not `venv`. *Reference:* Section 23.1.22. Which command creates a virtual environment named .venv?
- A)
pip install venv - B)
python -m venv .venv - C)
python --create-env .venv - D)
venv create .venv
Answer
**B)** `python -m venv .venv` *Why B:* The `venv` module is part of Python's standard library and is invoked with `python -m venv` followed by the environment name. *Why not A:* `venv` is a standard library module, not a pip-installable package. *Why not C:* There is no `--create-env` flag for Python. *Why not D:* `venv` is a Python module, not a standalone command. *Reference:* Section 23.2.13. After activating a virtual environment on macOS/Linux, how do you deactivate it?
- A)
exit - B)
quit - C)
deactivate - D)
source .venv/bin/deactivate
Answer
**C)** `deactivate` *Why C:* The `deactivate` command is a shell function created by the activation script. It restores your shell's original PATH. *Why not A:* `exit` closes the terminal session entirely. *Why not B:* `quit` is not a shell command (it's a Python REPL command). *Why not D:* There is no `deactivate` script to source — `deactivate` is a function added to your shell by activation. *Reference:* Section 23.2.24. What does pip freeze output?
- A) A list of all available packages on PyPI
- B) A list of installed packages with exact version numbers, in requirements.txt format
- C) A frozen (read-only) copy of the virtual environment
- D) The installation log for the current session
Answer
**B)** A list of installed packages with exact version numbers, in requirements.txt format *Why B:* `pip freeze` outputs every installed package in `package==version` format, which can be redirected to a `requirements.txt` file. *Why not A:* That would be a search, not a freeze. *Why not C:* "Freeze" refers to capturing a snapshot of versions, not making anything read-only. *Why not D:* pip doesn't maintain a session log in this way. *Reference:* Section 23.3.15. In semantic versioning (e.g., 2.31.0), what does a change to the MAJOR version number indicate?
- A) A bug fix that doesn't change behavior
- B) New features that are backward compatible
- C) Breaking changes that may require code updates
- D) The package has been completely rewritten
Answer
**C)** Breaking changes that may require code updates *Why C:* In semver, MAJOR version increments signal breaking changes — your existing code may need modifications. *Why not A:* That's a PATCH increment (e.g., 2.31.0 → 2.31.1). *Why not B:* That's a MINOR increment (e.g., 2.31 → 2.32). *Why not D:* A major version bump doesn't necessarily mean a full rewrite — it means the public API has changed in incompatible ways. *Reference:* Section 23.3.26. What is PyPI?
- A) A Python IDE
- B) The Python Package Index — a repository of third-party Python packages
- C) A debugging tool for Python
- D) A Python testing framework
Answer
**B)** The Python Package Index — a repository of third-party Python packages *Why B:* PyPI (pypi.org) hosts over 500,000 open-source Python packages that can be installed with pip. *Why not A:* PyPI is a package repository, not an IDE. *Why not C:* PyPI hosts packages; it doesn't debug code. *Why not D:* Testing frameworks like pytest are hosted *on* PyPI, but PyPI itself is a repository. *Reference:* Section 23.3.37. Which version constraint means "at least version 2.28, but less than version 3.0"?
- A)
requests==2.28 - B)
requests>=2.28,<3.0 - C)
requests~=2.28 - D)
requests<=3.0
Answer
**B)** `requests>=2.28,<3.0` *Why B:* The comma-separated constraints mean "greater than or equal to 2.28 AND less than 3.0." *Why not A:* `==` pins to exactly version 2.28, nothing else. *Why not C:* `~=2.28` means compatible release (>=2.28, <2.29 for two-part versions, or >=2.28.0, <2.29.0 for three-part), which is more restrictive. *Why not D:* This allows any version up to 3.0, including very old versions. *Reference:* Section 23.3.28. You install requests with pip and notice that four additional packages were also installed. These additional packages are called:
- A) Optional dependencies
- B) Virtual dependencies
- C) Transitive dependencies
- D) Peer dependencies
Answer
**C)** Transitive dependencies *Why C:* Transitive dependencies are packages that your direct dependencies themselves depend on. When you install `requests`, pip also installs the packages that `requests` needs. *Why not A:* Optional dependencies are explicitly declared as optional and not installed by default. *Why not B:* "Virtual dependency" is not standard Python terminology. *Why not D:* "Peer dependencies" is a Node.js/npm concept, not a Python/pip concept. *Reference:* Section 23.3.19. What is the main advantage of pyproject.toml over requirements.txt?
- A) It installs packages faster
- B) It combines project metadata, dependencies, and build configuration in one structured file
- C) It works on more operating systems
- D) It doesn't require a virtual environment
Answer
**B)** It combines project metadata, dependencies, and build configuration in one structured file *Why B:* `pyproject.toml` holds the project name, version, description, dependencies, dev dependencies, build system, and more — all in one TOML file. *Why not A:* The format of the dependency file doesn't affect installation speed. *Why not C:* Both work on all major operating systems. *Why not D:* Virtual environments are recommended regardless of which dependency format you use. *Reference:* Section 23.4.210. Which of the following is a red flag when evaluating a third-party package?
- A) It has MIT license
- B) The source code repository has no link
- C) It has 100,000 weekly downloads
- D) It was last updated 2 weeks ago
Answer
**B)** The source code repository has no link *Why B:* If you can't read the source code, you can't verify what the package does. This is a significant trust and security concern. *Why not A:* MIT is a widely used, permissive open-source license — that's a green flag. *Why not C:* High download counts suggest wide adoption and community trust. *Why not D:* Recent updates indicate active maintenance — another green flag. *Reference:* Section 23.6.2Section 2: True/False with Justification (1 point each)
11. "You should commit the .venv/ directory to version control so collaborators have the exact same environment."
Answer
**False** *Explanation:* Virtual environments are platform-specific and large. Instead of committing `.venv/`, you commit `requirements.txt` (or `pyproject.toml`). Collaborators create their own virtual environment and install from the requirements file. The `.venv/` directory should be listed in `.gitignore`.12. "Inside an activated virtual environment, pip install pandas installs pandas into your system Python."
Answer
**False** *Explanation:* When a virtual environment is activated, `pip` points to the environment's private copy. Packages are installed into the environment's `lib/` directory, not the system Python. That's the whole point of virtual environments.13. "pip freeze and pip list produce identical output."
Answer
**False** *Explanation:* `pip list` shows packages in a human-readable table format with columns for package name and version. `pip freeze` produces output in `package==version` format suitable for `requirements.txt`. They show the same packages but in different formats.14. "A wheel (.whl file) installs faster than a source distribution because it's pre-built."
Answer
**True** *Explanation:* Wheels are pre-built binary distributions that don't require compilation during installation. Source distributions (`.tar.gz`) may need to compile C extensions, which is slower and requires build tools to be installed. Modern pip prefers wheels when available.Section 3: Short Answer (2 points each)
15. Explain in 2-3 sentences what "dependency hell" is and how virtual environments help prevent it.
Sample Answer
Dependency hell occurs when two or more projects require different, incompatible versions of the same package. Since only one version of a package can be installed in a given Python environment, updating for one project can break another. Virtual environments solve this by giving each project its own isolated set of packages, so different projects can use different versions of the same library without conflict. *Rubric — full credit requires:* - Defining the version conflict problem - Explaining why it's a problem (can't have two versions in one environment) - Explaining how virtual environments solve it (isolation)16. You receive a project from a colleague that includes a requirements.txt. Write the exact sequence of terminal commands you would use to set up and run their project (assume the project has a main.py file). Include creating the virtual environment, activating it, installing dependencies, and running the script. Use the macOS/Linux syntax.
Sample Answer
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python main.py
*Rubric — full credit requires:*
- Creating a virtual environment (any valid name)
- Activating it (correct syntax for the specified platform)
- Installing from requirements.txt with the `-r` flag
- Running the script with `python`
17. A student writes the following requirements.txt:
requests
pandas
matplotlib
rich
What's wrong with this file? How should it be improved?
Sample Answer
The file lists package names without version numbers. This means `pip install -r requirements.txt` will install the latest versions available at the time of installation, which may differ from the versions the student tested with. A future change to any of these packages could break the code. The file should include version pins, ideally generated with `pip freeze`:requests==2.31.0
pandas==2.1.4
matplotlib==3.8.2
rich==13.7.0
*Rubric — full credit requires:*
- Identifying the missing version numbers
- Explaining the reproducibility problem
- Showing the corrected format with `==` pins
Section 4: Scenario Analysis (2 points each)
18. You're starting a new data analysis project. Walk through your first five terminal commands, in order, explaining what each one does.
Sample Answer
mkdir data-analysis # Create a project directory
cd data-analysis # Enter the directory
python -m venv .venv # Create a virtual environment
source .venv/bin/activate # Activate the environment
pip install pandas matplotlib # Install the libraries needed
Follow-up: `pip freeze > requirements.txt` to record the installed versions.
*Rubric — full credit requires:*
- Creating a directory and navigating to it
- Creating and activating a virtual environment
- Installing packages (any reasonable data science packages)
- Bonus: mentioning `pip freeze`
19. Dr. Patel wants to install biopython for her DNA analysis project. She already has a separate project that uses pandas and requests. Should she install biopython into her existing environment, or create a new one? Justify your answer.
Sample Answer
She should create a new virtual environment for the bioinformatics project. Even if the current packages don't conflict with `biopython` right now, mixing project dependencies creates risk: upgrading a package for one project could break the other, and it becomes unclear which packages belong to which project. A separate environment keeps each project self-contained with its own `requirements.txt`, making both projects easier to maintain, share, and deploy independently. *Rubric — full credit requires:* - Recommending a new environment - Explaining at least one reason: isolation, clarity, or independent maintenance20. You find two libraries for making terminal tables:
| Library X | Library Y | |
|---|---|---|
| Downloads/week | 2 million | 800 |
| Last updated | 1 month ago | 3 years ago |
| GitHub stars | 45,000 | 25 |
| Dependencies | 3 | 0 |
| License | MIT | No license |
Which would you choose and why? Address at least three factors from the evaluation checklist.
Sample Answer
Library X is the clear choice for three reasons: 1. **Maintenance:** Library X was updated 1 month ago; Library Y hasn't been touched in 3 years. Library Y likely has unpatched security issues and may not support current Python versions. 2. **Adoption:** 2 million weekly downloads vs. 800 suggests Library X is battle-tested by a large community. Bugs are found and fixed quickly. 3. **License:** Library Y has no license, which means you technically cannot legally use, modify, or distribute it. MIT is a well-understood permissive license. The only apparent advantage of Library Y (zero dependencies) isn't enough to overcome the maintenance, adoption, and legal concerns. *Rubric — full credit requires:* - Choosing Library X - Discussing at least three evaluation factors with reasoning - Specifically flagging the "no license" issue as a dealbreakerScoring & Next Steps
| Score | Assessment | Recommended Action |
|---|---|---|
| < 50% | Needs review | Re-read sections 23.1-23.3, redo exercises A.1-A.5 |
| 50-70% | Partial | Review dependency management (23.4) and pitfalls (23.7) |
| 70-85% | Solid | Ready to proceed; practice the Action Checklist on a real project |
| > 85% | Strong | Proceed to Chapter 24; explore poetry or pip-tools |