Python Virtual Environments
Imagine two projects on your computer. One needs version 4 of a web framework, the other needs version 5. If every package is installed into one global Python, they can't both be happy. A virtual environment solves this: it's a private folder containing its own Python interpreter link and its own site-packages, so each project gets exactly the packages it needs.
Note: Virtual environments are a command-line tool for your own computer. The terminal sessions below are simulated; paths and version numbers will differ on your machine.
Why use one?
| Without a venv | With a venv |
|---|---|
| All projects share one set of packages | Each project has its own packages |
| Upgrading a package for one project can break another | Projects are isolated |
| Hard to know what a project needs | pip freeze lists exactly that project's dependencies |
Linux distributions may block pip install globally |
Installing is always allowed inside a venv |
Creating a virtual environment
Python includes the venv module. Run it from your project folder; by convention the environment is called .venv:
cd my-project
python3 -m venv .venv
ls -a .venv. .. bin include lib lib64 pyvenv.cfg
On Windows the folder contains Scripts instead of bin.
Activating it
Activation adjusts your shell's PATH so that python and pip refer to the environment's copies. Your prompt usually shows the environment name:
| System | Command |
|---|---|
| Linux / macOS (bash, zsh) | source .venv/bin/activate |
| Windows PowerShell | .venv\Scripts\Activate.ps1 |
| Windows cmd | .venv\Scripts\activate.bat |
source .venv/bin/activate
which python
python -m pip list/home/you/my-project/.venv/bin/python
Package Version
------- -------
pip 24.0
A brand-new environment contains only pip. Anything you install now goes into .venv and nowhere else:
python -m pip install requests
python -m pip freeze > requirements.txt
cat requirements.txtcertifi==2024.7.4
charset-normalizer==3.3.2
idna==3.7
requests==2.32.3
urllib3==2.2.2
When you're done, deactivate returns the shell to normal.
Recreating an environment elsewhere
You don't copy or commit the .venv folder — it's large and tied to one machine. Instead, commit requirements.txt; a teammate rebuilds the environment with:
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txtAdd .venv/ to your .gitignore file.
Where do things live?
A virtual environment is just a folder layout. This small program builds the paths that a venv on Linux would use, to show the structure (it only computes paths; it doesn't create anything):
from pathlib import PurePosixPath
project = PurePosixPath("/home/you/my-project")
venv = project / ".venv"
version = "3.12"
layout = {
"interpreter": venv / "bin" / "python",
"activate script": venv / "bin" / "activate",
"installed packages": venv / "lib" / f"python{version}" / "site-packages",
"config": venv / "pyvenv.cfg",
}
for what, path in layout.items():
print(f"{what:<19} {path}")
print(layout["interpreter"].relative_to(project))interpreter /home/you/my-project/.venv/bin/python
activate script /home/you/my-project/.venv/bin/activate
installed packages /home/you/my-project/.venv/lib/python3.12/site-packages
config /home/you/my-project/.venv/pyvenv.cfg
.venv/bin/python
Am I inside a venv?
Inside your own scripts, you can check whether you're running in a virtual environment by comparing sys.prefix (the active environment) with sys.base_prefix (the original Python installation):
import sys
print("in a venv:", sys.prefix != sys.base_prefix)Other tools
venv + pip is built in and enough for most projects. You'll also meet these:
| Tool | What it adds |
|---|---|
virtualenv |
the older, faster third-party ancestor of venv |
pipx |
installs command-line apps each into their own venv |
poetry, pdm, hatch |
dependency management + packaging via pyproject.toml |
uv |
a very fast all-in-one replacement for pip and venv |
conda |
environments that also manage non-Python libraries (popular in data science) |
Tip: Editors such as VS Code detect a
.venvfolder in your project and offer to use it automatically. Select it as the interpreter so autocompletion sees your installed packages.
Test yourself
Which command creates a virtual environment named .venv?
- pip install venv .venv
- python3 -m venv .venv
- python3 --new-env .venv
- venv create .venv
What should you commit to version control so others can rebuild your environment?
- The whole .venv folder
- requirements.txt (or pyproject.toml)
- site-packages
- Nothing
What does activating a virtual environment do?
- Installs Python again
- Makes
pythonandpipin this shell refer to the environment's copies - Deletes global packages
- Starts a web server