🎩 Master Quiz: Virtual Environments
20 Questions. Prove you can manage your dependencies like an Architect!
Green = Correct, Red = Incorrect.
In the “Restaurant” analogy, what represents a Virtual Environment?
💡 Explanation
A Separate Kitchen.
Having a separate kitchen ensures that flavors (dependencies) from one dish (project) don’t mix with and ruin another.
What is “Dependency Hell”?
💡 Explanation
Version Conflicts.
It occurs when Project A needs `requests==1.0` and Project B needs `requests==2.0`. You can’t install both in the global Python.
Why should you NEVER use the System Python (Linux/RHEL) for projects?
💡 Explanation
System Stability.
Linux tools like `yum` or `dnf` depend on specific versions of Python libraries. Messing with the global environment can crash your OS.
Does `venv` copy the entire Python installation?
💡 Explanation
No, it is lightweight.
`venv` creates a folder with a copy of the Python executable (or a link to it) and a separate `site-packages` folder, but shares the core files.
What is the command to create a virtual environment named “venv”?
💡 Explanation
python -m venv venv.
You invoke the venv module using the -m flag. The second “venv” is just the name of the folder you want to create.
How do you activate the environment on Linux/macOS?
💡 Explanation
source venv/bin/activate.
You must “source” the script to modify your current shell’s path variables.
How do you activate the environment on Windows (PowerShell)?
💡 Explanation
.\venv\Scripts\Activate.ps1.
Windows stores scripts in a `Scripts` folder (not `bin`), and uses backslashes.
What is the most common cause of “ModuleNotFoundError” even after installing a package?
💡 Explanation
Forgot to activate.
If you don’t activate, you are using the global Python, which doesn’t have the libraries you installed inside the venv.
What can venv NOT install?
💡 Explanation
System-level libraries.
venv is for Python packages only. It cannot install OS dependencies like C headers or drivers; you need `apt` or Docker for that.
Why is venv important in CI/CD pipelines?
💡 Explanation
Security & Auditing.
A fresh venv ensures the build uses exactly what is in `requirements.txt`, reducing the attack surface and avoiding “pollution” from other builds.
What does it mean that a venv is “Disposable”?
💡 Explanation
Delete and recreate easily.
If an environment gets corrupted, you just delete the folder and run `python -m venv venv` again. It has no permanent effect on the OS.
Which tool allows you to manage multiple Python versions (e.g., 3.8 vs 3.12)?
💡 Explanation
Pyenv.
venv creates isolated environments, but it relies on the installed Python. Pyenv installs different base Python versions.
How do you install dependencies from a file inside a venv?
💡 Explanation
pip install -r requirements.txt.
This is the standard command to read a list of packages and install them into the currently active environment.
How does venv solve “It works on my machine”?
💡 Explanation
Strict Versioning.
By isolating dependencies, you ensure everyone uses the exact same versions of libraries, preventing conflicts caused by global updates.
What command allows you to exit the virtual environment?
💡 Explanation
deactivate.
This command reverts your shell’s PATH variable back to the system default, effectively “turning off” the venv.
If python3 -m venv fails on Linux, what package is usually missing?
💡 Explanation
python3-venv.
On some Linux distros (like Ubuntu), the `venv` module is packaged separately to save space and must be installed via `apt`.
Where are the libraries stored when using a venv?
💡 Explanation
Inside the ‘venv’ folder.
Specifically in `venv/lib/pythonX.X/site-packages`. This keeps them isolated from the rest of the system.
What file is standard for listing dependencies?
💡 Explanation
requirements.txt.
This is the Python standard convention for listing packages and their versions.
How does venv reduce the “Attack Surface”?
💡 Explanation
Only necessary packages.
By not including every global library, you reduce the risk of a vulnerability existing in unused code within your project environment.
What allows you to “freeze” an environment?
💡 Explanation
pip freeze.
This command outputs the exact versions of all installed libraries, allowing you to replicate the environment precisely on another machine.