Python Environments Cheat Sheet: venv, pip and uv
Checked against venv, pip and uv Python 3 venv docs, pip 26.3, uv 0.13.10 docs; venv and pip rows run on Python 3.12.8 with pip 26.3 on 28 November 2026. Each section links to the official docs it was checked against.
This sheet is for Python developers who want the right environment command without reading three manuals. It covers the standard library venv, pip 26.3 and uv 0.13.10 (released 24 Nov 2026). The venv and pip rows were run on Python 3.12.8 on macOS; uv wasn't installed on the test machine, so its rows come from the uv docs and are marked untested.
When to use what
Every project gets its own environment. The only real question is which tool makes it.
python -m venv plus pip is always there, needs nothing extra and is what most CI images and Dockerfiles assume. Use it for small scripts, for teaching, and anywhere you can't install another tool. The weak spot is locking: pip freeze captures what is installed, including things you didn't ask for, and pip has no lockfile of its own.
uv covers the whole job in one binary: it installs Python versions, creates the .venv, resolves dependencies into uv.lock and keeps the environment in step every time you call uv run. For a new application I start with uv init and treat pyproject.toml as the source of truth. If a team isn't ready to switch, uv pip and uv venv are drop-in faster versions of the commands they already know. The uv post walks through a migration.
If pip prints externally-managed-environment, your system Python is owned by the OS package manager under PEP 668. Make a venv, or use uvx/pipx for command-line tools, instead of reaching for --break-system-packages. For typed models from sample data, the JSON to Pydantic tool and JSON to Python tool save some typing.
On Windows you may need to allow PowerShell scripts before Activate.ps1 will run. You don't have to activate at all: calling .venv/bin/python (or .venv\Scripts\python.exe) uses the environment directly.
python3 -m venv .venvCreate an environment in .venv with its own python and pip
Add .venv/ to .gitignore
python3 -m venv --upgrade-deps .venvCreate the environment and upgrade pip in it straight away
source .venv/bin/activateActivate on macOS or Linux with bash or zsh
source .venv/bin/activate.fishActivate from the fish shell
.venv\Scripts\activate.batActivate on Windows in cmd.exe
.venv\Scripts\Activate.ps1Activate on Windows in PowerShell
deactivateLeave the active environment and restore your PATH
cat .venv/pyvenv.cfgShow the base interpreter, Python version and whether system site-packages are visible
Run pip as python -m pip so it always belongs to the interpreter you think it does. Flags are listed at https://pip.pypa.io/en/stable/cli/pip_install/.
python -m pip install requests==2.32.3Install an exact version into the current environment
python -m pip freeze > requirements.txtWrite every installed package with == pins, transitive ones included
python -m pip install -r requirements.txtInstall everything listed in a requirements file
python -m pip install requests -c constraints.txtInstall a package but cap versions with a constraints file; with urllib3<2.3 it chose urllib3 2.2.3
python -m pip install -e ./demoEditable install of a local project; code changes apply without reinstalling
python -m pip list --editableList only editable installs and their source folders
python -m pip list --outdatedShow installed packages with a newer release, with installed and latest versions
python -m pip install --upgrade pipUpgrade pip itself inside the environment (went from 24.3.1 to 26.3 here)
Full flag reference: https://docs.astral.sh/uv/reference/cli/. --locked fails if uv.lock would change; --frozen uses the lockfile as is without checking it against pyproject.toml.
uv init hello-worldCreate a project folder with pyproject.toml, .python-version, README.md, .gitignore, a git repo and a src/hello_world package
uv add requestsAdd a dependency to pyproject.toml, update uv.lock and sync the environment
uv add --dev pytestAdd to the dev dependency group (alias for --group dev)
uv add -r requirements.txt -c constraints.txtImport an existing requirements file into the project
uv remove requestsRemove a dependency from the project
uv lock --upgrade-package requestsMove one package to its newest allowed version in uv.lock
uv sync --lockedInstall exactly what uv.lock says and fail if it is out of date; a good CI step
uv run -- flask run -p 3000Run a command in the project environment after making sure it is in sync
The pip-compatible commands and their differences from pip are described at https://docs.astral.sh/uv/pip/compatibility/.
uv python install 3.13Download a managed Python build
uv python pin 3.13Write the version to .python-version so uv uses it in this folder
uvx ruff check .Run a CLI tool in a temporary isolated environment (short for uv tool run)
uv tool install ruffInstall a CLI tool permanently and put it on your PATH
uv venvCreate a .venv the fast way, for use with uv pip
uv pip install -r requirements.txtpip-style install into the active or .venv environment
uv pip compile requirements.in -o requirements.txtResolve loose requirements into a pinned, annotated requirements.txt
uv export --format requirements-txtExport uv.lock as a requirements file for tools that need one
PEP 668 is at https://peps.python.org/pep-0668/. It makes installers refuse to write into an interpreter that has an EXTERNALLY-MANAGED file in its stdlib folder when you aren't in a virtual environment.
[project]Table for name, version, requires-python and dependencies; read by most build backends
dependencies = ["idna>=3"]Runtime requirements under [project], as PEP 508 strings
[dependency-groups]Development-only groups such as test and lint, kept out of the published package
[build-system]Names the backend (for example setuptools) needed to build the project; pip install -e used it here
error: externally-managed-environmentPEP 668 refusal from pip on an OS-managed Python; create a venv instead
python -m pip install --break-system-packages pkgOverride the PEP 668 check; it can break OS tools, so avoid it
Worked example: Pin a transitive dependency with a constraints file
Your app depends on requests, and you need to hold urllib3 below 2.3 while you test something, without listing urllib3 as a direct dependency. Constraints files do exactly that:
$ python3 -m venv .venv
$ .venv/bin/python -m pip install -q --upgrade pip
$ echo 'urllib3<2.3' > constraints.txt
$ .venv/bin/python -m pip install -q requests -c constraints.txt
$ .venv/bin/python -m pip show urllib3 | head -2
Name: urllib3
Version: 2.2.3
The constraint never installs anything by itself; it only limits versions if something else asks for that package. That's the difference from a requirements file.
Now the trap. If you first ran pip freeze > req.txt while urllib3 2.8.0 was installed and then combine the two files, pip can't satisfy both:
$ .venv/bin/python -m pip install -r req.txt -c constraints.txt
ERROR: ResolutionImpossible: ...
The frozen file pins urllib3==2.8.0 and the constraint says below 2.3, so the resolver gives up rather than picking one. Keep your hand-written direct dependencies in one file and apply constraints to that, or let uv pip compile produce the pinned file for you.
Common mistakes
Running
pip installand getting a package in a different Python than the one you runUse
python -m pipfrom the interpreter you mean, or call.venv/bin/python -m pip, so pip and python always match.Fixing an
externally-managed-environmenterror with--break-system-packagesCreate a venv for the project, and install CLI tools with
uvxoruv tool installso the OS Python stays untouched.Using
pip freezeoutput as the only list of what your project needsFreeze captures every transitive package at one moment. Keep direct dependencies in pyproject.toml and generate pins with a lock tool such as uv.
Committing the
.venvfolderEnvironments contain absolute paths and platform-specific binaries. Ignore
.venv/in git and recreate it from the lockfile or requirements.Running
uv syncin CI and letting it update uv.lockUse
uv sync --lockedso the job fails when pyproject.toml and uv.lock disagree, and the fix happens in a reviewed commit.Moving or renaming a venv folder and finding its scripts broken
Scripts in a venv have the absolute interpreter path in their shebang. Delete the folder and create a new environment in the new place.
Related cheat sheets
Sources
- https://docs.python.org/3/library/venv.html
- https://pip.pypa.io/en/stable/user_guide/
- https://pip.pypa.io/en/stable/cli/pip_install/
- https://docs.astral.sh/uv/guides/projects/
- https://docs.astral.sh/uv/guides/tools/
- https://docs.astral.sh/uv/reference/cli/
- https://github.com/astral-sh/uv/releases/tag/0.13.10
- https://peps.python.org/pep-0668/
- https://packaging.python.org/en/latest/guides/writing-pyproject-toml/
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.