Virtual Environment Fundamentals: pip and venv Essentials Quiz

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.

  1. Creating a Virtual Environment

    Which command should you use to create a new virtual environment named 'myenv' using the built-in Python venv tool?

    1. python -m venv myenv
    2. pip install myenv
    3. python --create-venv=myenv
    4. venv new myenv

    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.

  2. Activating Virtual Environments

    What is the correct way to activate a virtual environment named 'projectenv' on a Unix-like system?

    1. source projectenv/bin/activate
    2. activate projectenv
    3. python activate projectenv
    4. pip -m activate projectenv

    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.

  3. Isolating Package Installations

    Why does installing packages inside a virtual environment prevent conflicts with global Python packages?

    1. The virtual environment uses its own directory for installed packages
    2. The virtual environment automatically upgrades all system packages
    3. Packages are only linked from the global environment
    4. It deletes global packages upon activation

    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.

  4. Using pip Within Environments

    After activating a virtual environment, what happens when you run 'pip install requests'?

    1. The requests package is installed in the active virtual environment only
    2. The requests package is removed from the system environment
    3. requests is installed globally and overwrites system packages
    4. No action occurs unless you specify the '--env' option

    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.

  5. Freezing Requirements

    What is the primary purpose of using 'pip freeze > requirements.txt' when working inside a virtual environment?

    1. To record the exact package versions installed for reproducibility
    2. To delete all outdated packages from the environment
    3. To activate the virtual environment automatically on startup
    4. To upgrade all packages to their latest versions

    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.