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.
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.
.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.
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.