Learning navigation

Code with Animation

Virtual Environments & Package Management (venv & pip)

Master project dependency isolation: creating lightweight virtual environments with python -m venv, installing third-party packages into site-packages with pip, and freezing reproducible builds via requirements.txt.

Python7 min readConcept 18 of 24

Why Virtual Environments are Mandatory

In modern software development, different projects require different versions of external libraries. For example, Project Alpha may rely on fastapi 0.95.0, while Project Beta requires fastapi 0.110.0.

If you install packages into your operating system's global Python environment, projects will conflict with one another, and updating a library for one application will break another application on the same machine.

A virtual environment (venv) solves this by providing a self-contained, lightweight directory holding its own Python binaries, scripts, and an isolated site-packages directory.

How venv Isolates site-packages

When you create a virtual environment with python -m venv .venv, Python creates a directory with local links to the base Python executable and an empty lib/pythonX.Y/site-packages folder.

When you activate the environment (source .venv/bin/activate on Linux/macOS or .venv\Scripts\activate on Windows), your shell's PATH environment variable is prepended with the virtual environment's bin or Scripts folder.

Consequently, running python or pip invokes the isolated virtual environment binaries. All subsequent packages installed with pip install land strictly inside that project's local site-packages without touching the host operating system.

Installing and Freezing Dependencies with pip

The Python package installer pip connects to the Python Package Index (PyPI) to download and install wheels and source distributions.

To ensure teammates, deployment servers, and CI/CD pipelines run the exact same versions of all dependencies, Python developers export their installed packages using pip freeze > requirements.txt.

In a fresh environment, running pip install -r requirements.txt automatically reconstructs the exact dependency graph in seconds.

The Virtual Environment Sandbox Simulator

Compare global system installation against project-local virtual environments. See how activation modifies PATH to route package installations into isolated site-packages folders, eliminating dependency version collisions.

Select Environment Architecture:
# 1. Create local virtual environment:
$ python -m venv .venv
# 2. Activate in terminal:
$ source .venv/bin/activate
# 3. Isolated installation:
(.venv) $ pip install fastapi
# Installed into .venv/lib/site-packages
DEPENDENCY ISOLATION ARCHITECTUREHOST OS PYTHON/usr/bin/python3 (System core runtime untouched)ACTIVE VENV: .venv/PATH prepended ➔ .venv/binLOCAL site-packages FOLDER:fastapi==0.110uvicorn==0.29pydantic==2.6ISOLATED RUNTIME: PATH routes pip to local site-packages✓ Each project maintains completely independent library versions
★ Python Rule: Never commit the .venv folder to Git. Virtual environments contain machine-specific symlinks and binary files. Commit only requirements.txt.

Check yourself

Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.

  1. 1What does activating a Python virtual environment primarily do to your shell session?
  2. 2Where does `pip install requests` store downloaded package files when run inside an active virtual environment?
  3. 3Why should the `.venv` directory never be committed to Git version control?

Remember this

  • Virtual environments isolate dependencies per project and prevent global Python pollution.
  • Create an environment with python -m venv .venv and activate it before installing packages.
  • Activation prepends the environment's bin or Scripts folder to your PATH environment variable.
  • pip install installs packages into the active environment's local site-packages folder.
  • Export reproducible dependency graphs with pip freeze > requirements.txt; never commit .venv to Git.

Done with this concept?

Mark it complete to track your progress. No login needed.