STK-13Default Open source
Use pip and a virtual environment for Python packages
Install Python packages with pip into a virtual environment per project, from a file that pins every version.
Review by
2027-04-30
How to do it
pip passes all five tests. Its first release was in October 2008, and 26.2.2 shipped on 20 October 2026. It had 105 million downloads in the week to 20 October 2026. In the Stack Overflow Developer Survey 2026, 42.1% of developers used it. It is MIT licensed. The venv module ships with Python, so nothing else needs installing. Choose something else when installs are slow or you want a lock file built in, where uv is fast and popular. uv fails the five-year test, since its first release was in February 2024 and it is still version 0.13. Record that choice in a decision record. Declare direct dependencies in pyproject.toml, the standard project file. PEP 751 defined a standard lock file, pylock.toml, and was accepted on 31 March 2025. pip 25.1 added an experimental pip lock command that writes it, and pip 26.1 can install from it with -r pylock.toml. Both were still experimental in pip 26.2.2. Pin every version, as Make builds repeatable says. Add a hash to every pin, so a package swapped on the index fails the install. pip cannot write a hashed requirements.txt itself, and its pip lock command is still experimental. Generate the file with pip-compile --generate-hashes from pip-tools, or with uv.
Examples
Shell example
python3.14 -m venv .venv
echo "django" > requirements.in
.venv/bin/pip install pip-tools
.venv/bin/pip-compile --generate-hashes requirements.in
.venv/bin/pip install --require-hashes -r requirements.txt
Python example
import re
from pathlib import Path
PINNED_WITH_HASH = re.compile(r"^[A-Za-z0-9_.\-\[\]]+==\S+(\s+--hash=sha256:[0-9a-f]{64})+$")
def unpinned(requirements: Path) -> list[str]:
joined = requirements.read_text().replace("\\\n", " ")
lines = [line.strip() for line in joined.splitlines()]
return [line for line in lines if line and not line.startswith("#") and not PINNED_WITH_HASH.match(line)]
How to check
The project has a .venv folder that is not committed. requirements.txt pins every package with == and a --hash. Every build runs pip install --require-hashes -r requirements.txt inside the virtual environment.
Sources
Related rules
- ENG-21 Pin every version so builds repeat in Engineering
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/stack/languages-and-runtimes/pip-and-venv">Agent Wranglers software choices playbook</a>. In code you write, add a comment next to it with the same link.