Lesson 43 of 60
โฎPrev Nextโฏ

PY-101 ยท Modules and packages

pip and virtual environments

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

43of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    pip installs packages from PyPI, such as pip install requests. A virtual environment, made with python -m venv .venv, is a private folder of packages for one project, so projects never fight over versions. requirements.txt lists a project's packages so anyone can recreate it with pip install -r requirements.txt.

    • Install packages with pip
    • Create and activate a venv
    • Use requirements.txt

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    pip and virtual environments, running
    1. requirements = """requests==2.32.3
    2. pandas>=2.2
    3. # comment lines are ignored
    4. pytest
    5. """
    6. for line in requirements.splitlines():
    7. line = line.strip()
    8. if not line or line.startswith("#"):
    9. continue
    10. name = line.replace(">=", "==").split("==")[0]
    11. print(name)
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemProject A needs pandas 1.5 and project B needs pandas 2.2. Installed globally, one of them breaks. A venv per project keeps both happy.
    2. The Python ideapython -m venv .venvpip installs packages from PyPI, such as pip install requests.
    3. In real lifeCloud deployments (Azure App Service, Docker images) read requirements.txt to install exactly the packages your app needs.

    Start with a question

    Project A needs pandas 1.5 and project B needs pandas 2.2. Installed globally, one of them breaks. A venv per project keeps both happy.

    Every professional Python project uses an isolated environment and a pinned list of dependencies.

    Explain it like I'm 10

    A virtual environment is a separate pencil case for each subject, so the maths pencils never get mixed up with the art ones.

    The technical explanation

    Create: python -m venv .venv. Activate: source .venv/bin/activate (macOS/Linux) or .venv\Scripts\activate (Windows).

    Install: pip install requests==2.32.3. Save: pip freeze > requirements.txt.

    This browser editor cannot run pip, but Python Academy's playground can import numpy, pandas and matplotlib directly.

    Syntax

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

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    Cloud deployments (Azure App Service, Docker images) read requirements.txt to install exactly the packages your app needs.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. What does pip do?
    2. 2. Why use a virtual environment?
    3. 3. How do you create a venv?
    4. 4. What should you commit to Git?
    5. 5. pip install -r requirements.txt does what?

    Questions people ask

    What is the difference between pip and conda?

    pip installs Python packages from PyPI. conda is a separate package and environment manager popular in data science that can also install non-Python libraries.

    Do I need to activate the virtual environment every time?

    Yes, in each new terminal. Editors like VS Code can select the venv for you automatically.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner What is the difference between pip and conda?

    pip installs Python packages from PyPI. conda is a separate package and environment manager popular in data science that can also install non-Python libraries.

    beginner Do I need to activate the virtual environment every time?

    Yes, in each new terminal. Editors like VS Code can select the venv for you automatically.

    intermediate What does pip do?

    Installs packages. pip installs from PyPI.

    intermediate Why use a virtual environment?

    Separate packages per project. Projects stop clashing over versions.

    Quick revision

    Remember

    • Install packages with pip
    • Create and activate a venv
    • Use requirements.txt
    • Add .venv/ to .gitignore. Commit requirements.txt, never the venv folder itself.

    Syntax

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

    Official documentation