Virtual environments & pip (concept)

Computer Science · Python

Question 1 of 10

0

    By default, pip downloads packages from which repository?

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Virtual environments & pip (concept) explained

    A virtual environment is a folder that holds its own Python installation and its own set of installed packages, separate from other projects. pip is the tool that installs packages, usually from the Python Package Index. Giving each project its own environment stops one project's package versions from breaking another.

    Create an environment with python -m venv .venv. Activate it with source .venv/bin/activate on macOS and Linux, or .venv\Scripts\activate on Windows. While it is active, pip install requests installs the package into .venv only. pip freeze lists the installed packages with their versions, and saving that output to requirements.txt lets someone else recreate the same set with pip install -r requirements.txt. Keep the .venv folder out of version control, because it can be rebuilt from the requirements file. Leave the environment with the deactivate command.

    python -m venv .venv
    source .venv/bin/activate
    pip install requests
    pip freeze > requirements.txt
    

    Worked example

    Hypothetically, project A needs version 1.0 of a library and project B needs version 2.0 of the same library.

    1. With one shared installation, installing 2.0 for project B would replace 1.0 and break project A.
    2. Each project gets its own .venv. Project A's environment holds 1.0 and project B's holds 2.0.
    3. Each project uses the version it was tested with, and neither affects the other.

    What you will practise

    104 multiple-choice questions (Easy, Medium, Hard). Each answer is followed by an explanation.

    Example questions

    • A virtual environment:
    • pip installs:
    • requirements.txt:
    • An environment avoids:
    • pip freeze:
    • The built-in module used to create a virtual environment in Python 3 is:
    • To leave an active virtual environment in a terminal, you run:
    • Packages installed with pip while a virtual environment is active are installed:

    …and 96 more.