Skip to content

Menu

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

Create an environment in .venv with its own python and pip

Add .venv/ to .gitignore

python3 -m venv --upgrade-deps .venv

Create the environment and upgrade pip in it straight away

From the docs, not run by us
source .venv/bin/activate

Activate on macOS or Linux with bash or zsh

From the docs, not run by us
source .venv/bin/activate.fish

Activate from the fish shell

From the docs, not run by us
.venv\Scripts\activate.bat

Activate on Windows in cmd.exe

From the docs, not run by us
.venv\Scripts\Activate.ps1

Activate on Windows in PowerShell

From the docs, not run by us
deactivate

Leave the active environment and restore your PATH

From the docs, not run by us
cat .venv/pyvenv.cfg

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

Install an exact version into the current environment

python -m pip freeze > requirements.txt

Write every installed package with == pins, transitive ones included

python -m pip install -r requirements.txt

Install everything listed in a requirements file

python -m pip install requests -c constraints.txt

Install a package but cap versions with a constraints file; with urllib3<2.3 it chose urllib3 2.2.3

python -m pip install -e ./demo

Editable install of a local project; code changes apply without reinstalling

python -m pip list --editable

List only editable installs and their source folders

python -m pip list --outdated

Show installed packages with a newer release, with installed and latest versions

python -m pip install --upgrade pip

Upgrade 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-world

Create a project folder with pyproject.toml, .python-version, README.md, .gitignore, a git repo and a src/hello_world package

From the docs, not run by us
uv add requests

Add a dependency to pyproject.toml, update uv.lock and sync the environment

From the docs, not run by us
uv add --dev pytest

Add to the dev dependency group (alias for --group dev)

From the docs, not run by us
uv add -r requirements.txt -c constraints.txt

Import an existing requirements file into the project

From the docs, not run by us
uv remove requests

Remove a dependency from the project

From the docs, not run by us
uv lock --upgrade-package requests

Move one package to its newest allowed version in uv.lock

From the docs, not run by us
uv sync --locked

Install exactly what uv.lock says and fail if it is out of date; a good CI step

From the docs, not run by us
uv run -- flask run -p 3000

Run a command in the project environment after making sure it is in sync

From the docs, not run by us

The pip-compatible commands and their differences from pip are described at https://docs.astral.sh/uv/pip/compatibility/.

uv python install 3.13

Download a managed Python build

From the docs, not run by us
uv python pin 3.13

Write the version to .python-version so uv uses it in this folder

From the docs, not run by us
uvx ruff check .

Run a CLI tool in a temporary isolated environment (short for uv tool run)

From the docs, not run by us
uv tool install ruff

Install a CLI tool permanently and put it on your PATH

From the docs, not run by us
uv venv

Create a .venv the fast way, for use with uv pip

From the docs, not run by us
uv pip install -r requirements.txt

pip-style install into the active or .venv environment

From the docs, not run by us
uv pip compile requirements.in -o requirements.txt

Resolve loose requirements into a pinned, annotated requirements.txt

From the docs, not run by us
uv export --format requirements-txt

Export uv.lock as a requirements file for tools that need one

From the docs, not run by us

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

From the docs, not run by us
[build-system]

Names the backend (for example setuptools) needed to build the project; pip install -e used it here

error: externally-managed-environment

PEP 668 refusal from pip on an OS-managed Python; create a venv instead

From the docs, not run by us
python -m pip install --break-system-packages pkg

Override the PEP 668 check; it can break OS tools, so avoid it

From the docs, not run by us

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 install and getting a package in a different Python than the one you run

    Use python -m pip from the interpreter you mean, or call .venv/bin/python -m pip, so pip and python always match.

  • Fixing an externally-managed-environment error with --break-system-packages

    Create a venv for the project, and install CLI tools with uvx or uv tool install so the OS Python stays untouched.

  • Using pip freeze output as the only list of what your project needs

    Freeze 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 .venv folder

    Environments contain absolute paths and platform-specific binaries. Ignore .venv/ in git and recreate it from the lockfile or requirements.

  • Running uv sync in CI and letting it update uv.lock

    Use uv sync --locked so 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.