requirements.txt & Constraints Files
Before pyproject.toml and lockfiles, the requirements.txt file was how Python projects wrote down their dependencies, and it is still everywhere: Dockerfiles, CI jobs, Jupyter projects, deployment platforms. It is not a standard file format, it is simply a list of pip command-line arguments, one per line. Constraints files are its lesser-known sibling, and they solve a problem requirements files can't.
What can go in a requirements file
# requirements.txt
requests>=2.30,<3 # a name with a version specifier
rich==13.9.4 # an exact pin
attrs[tests] # extras (optional dependency sets)
colorama; sys_platform == "win32" # an environment marker: only on Windows
-r requirements-base.txt # include another file
-c constraints.txt # apply a constraints file
--index-url https://pypi.example.com/simple/ # pip options are allowed too
./libs/mytool # a local project directory
mypkg @ https://example.com/mypkg-1.0-py3-none-any.whl # a direct URL (PEP 508)pip reads it with pip install -r requirements.txt. Because it's "just arguments", there's no schema and no validation beyond what pip does with each line.
Requirements vs constraints
A requirement says "install this". A constraint says "if this gets installed, it must match this specifier". Constraints never add packages; they only narrow the versions of packages something else asks for.
That makes a constraints file the right place for organisation-wide or CI-wide pins: you can apply the same constraints.txt to many different requirement sets, and packages that aren't needed simply aren't installed.
python3 -m venv .venv && . .venv/bin/activate
cat > requirements.txt <<'TXT'
requests>=2.30
rich
colorama; sys_platform == "win32"
TXT
cat > constraints.txt <<'TXT'
requests==2.31.0
rich==13.9.4
attrs==24.2.0
TXT
pip install -r requirements.txt -c constraints.txt 2>&1 | grep -E 'Ignoring|Successfully'
pip freezeIgnoring colorama: markers 'sys_platform == "win32"' don't match your environment
Successfully installed certifi-2026.10.23 charset-normalizer-3.5.2 idna-3.20 markdown-it-py-4.2.0 mdurl-0.1.2 pygments-2.21.0 requests-2.31.0 rich-13.9.4 urllib3-2.8.0
certifi==2026.10.23
charset-normalizer==3.5.2
idna==3.20
markdown-it-py==4.2.0
mdurl==0.1.2
Pygments==2.21.0
requests==2.31.0
rich==13.9.4
urllib3==2.8.0
What happened, line by line:
coloramawas skipped: its marker is false on Linux.requestsandrichwere pinned down to the constraint versions, even though the index has newer ones.attrsappears in the constraints but nobody required it, so it wasn't installed.- The dependencies (
urllib3,idna,pygments, …) were not constrained and got the newest versions that fit.
When a constraint and a requirement disagree
Constraints are not suggestions. If a requirement can't be satisfied together with a constraint, the install fails, which is exactly what you want in CI:
python3 -m venv .venv && . .venv/bin/activate
echo 'requests>=2.32' > requirements.txt
echo 'requests==2.31.0' > constraints.txt
pip install -r requirements.txt -c constraints.txt 2>&1 | grep -v '^