Sub topic 9
Repeatable installs with constraints files
This chapter assumes you can create and activate a virtual environment, install packages with python -m pip, and read version specifiers such as >=2.31,<3. It also assumes you have written a basic requirements.txt.
The problem a requirements file leaves open
A requirements file usually lists only the packages your project uses directly. If it says requests, pip also installs the packages that requests depends on. Those transitive dependencies are not listed, so pip picks the newest allowed release of each one on the day you install. Two installs made a month apart can produce different environments, even from the same file.
You have two ways to fix this:
- Pin every package, direct and transitive, in
requirements.txt. The file becomes long, and it no longer shows which packages you chose yourself. - Keep
requirements.txtshort and put the pins in a separate constraints file.
What a constraints file does
A constraints file limits the versions pip may choose. It does not ask pip to install anything. If a package named in the constraints file is never needed, pip ignores that line.
# constraints.txt
requests==2.32.3
urllib3<3
# requirements.txt
requestsYou pass the constraints file with -c:
python -m pip install -r requirements.txt -c constraints.txt
python -m pip show requestsHere requirements.txt asks for requests, and constraints.txt limits the choice to version 2.32.3. The same constraint also covers urllib3 when requests pulls it in.
Requirements versus constraints
| Aspect | Requirements file (`-r`) | Constraints file (`-c`) |
|---|---|---|
| Triggers an install | Yes, every line | No, never |
| Typical content | Packages your project uses directly | Pins for direct and transitive packages |
| Effect on unlisted packages | None | None |
| Conflicting version | Resolver reports an error | Resolver reports an error |
A constraint can also make an install fail. If requirements.txt asks for requests>=2.33 and the constraints file pins requests==2.32.3, no version satisfies both. pip stops with a ResolutionImpossible error instead of choosing one silently.
Generating a constraints file
Install what you need in a working environment, then record the result:
python -m pip install -r requirements.txt
python -m pip freeze > constraints.txtYou can then rebuild the environment elsewhere:
python3 -m venv .venv-new
.venv-new/bin/python -m pip install -r requirements.txt -c constraints.txtTo update a dependency on purpose, install the new version, run pip freeze again, review the diff, and commit the new file. Version control then shows exactly which packages changed.
Applying constraints automatically
Typing -c constraints.txt each time is easy to forget. pip also reads the PIP_CONSTRAINT environment variable, which holds the path to a constraints file:
export PIP_CONSTRAINT=constraints.txt
python -m pip install -r requirements.txtUse this in a CI job or a build script, where every install should follow the same pins. A setting in a shell profile is easy to forget, so avoid it for everyday development.
Common mistakes
- Expecting a line in
constraints.txtto install a package. It only limits versions. - Freezing into
requirements.txtfrom an environment that has unrelated packages in it. Freeze from a clean environment built for the project. - Never refreshing the file. Pins that are too old will miss security fixes, so schedule regular updates.
Exercise
Your constraints file contains idna==3.7, and your requirements file lists only requests. After python -m pip install -r requirements.txt -c constraints.txt, which version of idna is installed, and why is it installed at all?
Show answer
Version 3.7 is installed. requests depends on idna, so pip installs it, and the constraints file limits the version to 3.7. If nothing in the requirements needed idna, pip would ignore that line and not install it.