Phase 4 · Intermediate PythonModule 19~28 min read

Virtual Environments, pip & the Ecosystem

Isolate projects and manage dependencies with venv, pip, and PyPI.

What you'll learn

Python's superpower is its ecosystem — hundreds of thousands of installable packages on PyPI. But installing everything globally quickly turns into a mess where two projects need conflicting versions. Virtual environments solve this, and pip is how you install packages into them.

By the end you'll be able to:

  • Explain why per-project isolation matters
  • Create and activate a virtual environment with venv
  • Install, list, and remove packages with pip
  • Pin dependencies with requirements.txt

Why isolation matters

Imagine Project A needs Django 4 and Project B needs Django 5. Installed globally, one would overwrite the other — you can't have both. A virtual environment is a self-contained, per-project folder holding its own Python and its own packages. Each project gets exactly the versions it needs, isolated from every other project and from your system Python.

Key idea

The rule: one virtual environment per project, and never pip install into your global Python. Isolation keeps projects reproducible and stops one project's upgrade from silently breaking another.

Creating a venv

Python ships with the venv module. Create an environment (conventionally in a .venv folder inside your project), then activate it so that python and pip point at the environment instead of the system:

terminal
# Create a virtual environment in a .venv folder
python -m venv .venv

# Activate it:
source .venv/bin/activate       # macOS / Linux
.venv\Scripts\activate          # Windows

# Your prompt now shows (.venv). Deactivate any time:
deactivate

Tip

Add .venv/ to your .gitignore — you never commit the environment itself, only the requirements.txt that describes it. Anyone who clones your project recreates the environment from that file.

Installing with pip

With the environment active, pip installs packages from PyPI (the Python Package Index) into this environment only:

terminal
pip install requests            # install a package from PyPI
pip install "django>=5,<6"      # a version range
pip list                        # what's installed here
pip show requests               # details about a package
pip uninstall requests          # remove it

requirements.txt

To make your project reproducible, record its dependencies. pip freeze writes the exact installed versions to a file, and pip install -r reinstalls them elsewhere — so a teammate (or a server) gets an identical environment:

terminal
# Freeze exact versions into a file:
pip freeze > requirements.txt

# requirements.txt then looks like:
#   requests==2.32.3
#   rich==13.7.1

# Anyone can recreate your environment:
pip install -r requirements.txt

Watch out

Pin your versions. Recording requests==2.32.3 rather than just requests means your app won't mysteriously break six months later when a dependency ships a breaking change. Reproducible builds depend on exact versions.

Modern tooling

venv + pip is the standard foundation, but newer tools streamline the workflow. pipx installs command-line apps in their own isolated environments; Poetry and uv manage dependencies, virtual environments, and lock files together (with pyproject.toml), and uv in particular is remarkably fast. Learn the fundamentals here first — every tool builds on the same isolation concept.

Recap & quick check

Key takeaways

  • A virtual environment isolates a project's packages so different projects can use different versions.
  • Create one with python -m venv .venv, then activate it; use one venv per project and never pip-install globally.
  • pip installs packages from PyPI into the active environment: pip install, list, show, uninstall.
  • pip freeze > requirements.txt records exact versions; pip install -r recreates the environment.
  • Pin versions for reproducibility; modern tools (pipx, Poetry, uv) build on the same isolation idea.

Quick check

1. Why use a virtual environment?

2. How do you create a virtual environment?

3. What does pip install -r requirements.txt do?

4. Should you commit the .venv folder to git?

5. Why pin exact versions (requests==2.32.3)?

With a clean environment ready, let's make our programs robust. Next up: Module 20 — Errors & Exceptions.