TExcelTAD
Home › Python Packaging: pip, venv, Poetry & uv › requirements.txt & Constraints Files

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

text
# 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.

bash · try it yourself
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 freeze
Ignoring 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:

  • colorama was skipped: its marker is false on Linux.
  • requests and rich were pinned down to the constraint versions, even though the index has newer ones.
  • attrs appears 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:

bash · try it yourself
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 '^
  

Looking in indexes: http://127.0.0.1:8080/simple/
ERROR: Cannot install requests>=2.32 because these package versions have conflicting dependencies.
The conflict is caused by:
    The user requested requests>=2.32
    The user requested (constraint) requests==2.31.0
Additionally, some packages in these conflicts have no matching distributions available for your environment:
    requests
To fix this you could try to:
1. loosen the range of package versions you've specified
2. remove package versions to allow pip to attempt to solve the dependency conflict
ERROR: ResolutionImpossible: for help visit https://pip.pypa.io/en/latest/topics/dependency-resolution/#dealing-with-dependency-conflicts

Tip: pip also reads constraints from the PIP_CONSTRAINT environment variable. Some teams set it once in their CI image so every pip install in every job respects the same pins.

Layering files

A common layout splits what the app needs from what developers need:

text
requirements/
  base.txt      # runtime: requests, rich
  dev.txt       # -r base.txt, plus pytest, mypy
  constraints.txt   # exact versions for everything, generated

The generated file is the interesting part: you want exact versions for every package, transitive ones included, so every install is identical. That's a lockfile, and tools generate it for you: pip freeze (crude), pip-tools' pip-compile or uv pip compile (proper). The next lesson shows pip freeze, and uv pip shows uv pip compile.

A runnable model: applying constraints

This model shows the decision pip makes for each package: requirements decide what goes in, constraints intersect with the allowed versions:

python · try it yourself
available = {"requests": ["2.31.0", "2.34.2"], "rich": ["13.9.4", "15.0.0"], "attrs": ["24.2.0", "26.1.0"]}

def v(s):
    return tuple(int(x) for x in s.split("."))

def allowed(version, spec):
    if spec.startswith("=="):
        return v(version) == v(spec[2:])
    if spec.startswith(">="):
        return v(version) >= v(spec[2:])
    return True        # "" = any version

def plan(requirements, constraints):
    result = {}
    for name, spec in requirements.items():
        ok = [ver for ver in available[name]
              if allowed(ver, spec) and allowed(ver, constraints.get(name, ""))]
        result[name] = max(ok, key=v) if ok else "CONFLICT"
    return result

reqs = {"requests": ">=2.30", "rich": ""}
print(plan(reqs, {}))
print(plan(reqs, {"requests": "==2.31.0", "rich": "==13.9.4", "attrs": "==24.2.0"}))
print(plan({"requests": ">=2.32"}, {"requests": "==2.31.0"}))
{'requests': '2.34.2', 'rich': '15.0.0'}
{'requests': '2.31.0', 'rich': '13.9.4'}
{'requests': 'CONFLICT'}

Test yourself

A constraints file lists attrs==24.2.0, but no requirement needs attrs. What does pip do?

  1. Installs attrs 24.2.0
  2. Fails
  3. Nothing: constraints never add packages
  4. Installs the newest attrs

What does the line colorama; sys_platform == "win32" do on Linux?

  1. Installs colorama
  2. Fails the install
  3. Skips colorama because the environment marker is false
  4. Installs colorama only for 32-bit Python

Why is requirements.txt not a standard like pyproject.toml?

  1. It is deprecated
  2. It is simply a list of pip arguments, one per line, defined by pip itself
  3. It only supports exact pins
  4. It's a YAML file