Explore the core concepts behind managing Python virtual environments using pip and venv tools. This quiz assesses your understanding of environment creation, activation, dependency handling, and common workflows for isolated Python package management.
Which command should you use to create a new virtual environment named 'myenv' using the built-in Python venv tool?
Explanation: The correct command to create a virtual environment with the Python standard tool is 'python -m venv myenv'. The option 'pip install myenv' is incorrect, as pip is used to install packages, not to create environments. 'python --create-venv=myenv' is not a valid or recognized flag. 'venv new myenv' is also not a valid command for this operation.
What is the correct way to activate a virtual environment named 'projectenv' on a Unix-like system?
Explanation: On Unix-like systems, the script to activate a virtual environment is run using 'source projectenv/bin/activate'. The 'activate projectenv' command is not recognized by the shell. 'python activate projectenv' and 'pip -m activate projectenv' are both invalid as there is no such subcommand in Python or pip for this purpose.
Why does installing packages inside a virtual environment prevent conflicts with global Python packages?
Explanation: A virtual environment isolates installed packages by using a separate directory, which means the packages do not interfere with global ones. It does not upgrade system packages automatically; such behavior would defeat isolation purposes. Packages are not simply linked from the global environment, so that option is incorrect. Virtual environments never delete global packages on activation.
After activating a virtual environment, what happens when you run 'pip install requests'?
Explanation: Running pip install inside an activated virtual environment adds the package to that environment alone, maintaining isolation. It does not remove any packages from the system. The package is not installed globally unless you specifically run pip outside the environment. The '--env' flag is not required for typical virtual environment pip usage.
What is the primary purpose of using 'pip freeze > requirements.txt' when working inside a virtual environment?
Explanation: 'pip freeze > requirements.txt' captures the exact installed package versions for the purpose of consistent environment recreation. It does not delete packages or manage activation routines. Upgrading packages is handled by another pip command and not by pip freeze.