Sub topic 9
Repeatable installs with constraints files
This chapter assumes you can create and activate a virtual environment, run python -m pip install, and read a basic requirements.txt. It builds on those skills to answer one question. How do you get the same package versions on every machine without hard-coding every pin into the file that lists your dependencies?
The problem with one file doing two jobs
A requirements file usually mixes two different ideas:
- What your project needs. For example, "I use
requests, at version 2.31 or later but below 3." - What was actually installed when everything worked. For example,
requests==2.32.3,urllib3==2.2.2,idna==3.7, and so on.
If you put only the first idea in the file, two installs made a month apart can pick different versions of requests and of every package it pulls in. If you put the second idea in, the file lists dozens of packages you never chose. It becomes hard to tell which ones you depend on directly and which arrived as dependencies of dependencies (transitive dependencies).
A constraints file separates the two jobs. The requirements file says what to install. The constraints file says which versions are allowed.
What a constraints file does
A constraints file uses the same line syntax as a requirements file, but pip treats it differently. A requirements file tells pip to install a package. A constraints file only limits the versions pip may choose if the package gets installed for some other reason.
# 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.txtHere requirements.txt asks for requests. The constraints file narrows that to exactly 2.32.3. urllib3 is not in the requirements file. It is installed because requests depends on it, and the constraint then keeps it below version 3.
Constraints never add packages
This is the most common point of confusion. Suppose constraints.txt pins requests, and you run this:
python -m pip install -c constraints.txt flaskFlask does not depend on requests, so requests is not installed. The pin sits unused. A constraint is a rule about versions, not a request to install.
That behavior is the point. You can keep one constraints file for a whole project, or for several related projects. Each install uses only the pins that apply to the packages it needs.
Building a constraints file from a working environment
The usual workflow has three steps:
- Write a short
requirements.txtthat lists only your direct dependencies, with loose specifiers. - Install it into a clean environment and test your project.
- Once the tests pass, record every installed version as the constraints file.
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt
# run your tests here
python -m pip freeze --exclude-editable > constraints.txtpip freeze prints each installed package as name==version. The --exclude-editable option leaves out packages installed with pip install -e. Editable entries appear in freeze output as -e lines, and a constraints file cannot contain them.
A new teammate, a CI job, or a deployment can now reproduce your environment:
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt -c constraints.txtWhen you want to upgrade, build a fresh environment from requirements.txt alone, test it, and regenerate constraints.txt. The change then appears in version control as an ordinary diff.
Compared with installing a frozen file directly
The earlier chapter on requirements files showed this pattern:
python -m pip freeze > requirements.txt
python -m pip install -r requirements.txtThat also reproduces versions, and for a simple application it is often enough. The table shows where the two approaches differ.
| Question | Frozen `requirements.txt` | `requirements.txt` plus `constraints.txt` |
|---|---|---|
| Is every version pinned? | Yes | Yes, in the constraints file |
| Can you tell your direct dependencies apart from transitive ones? | No, they are all mixed together | Yes, only direct ones are in `requirements.txt` |
| What happens when you drop a dependency? | Its transitive packages stay in the file until you remove them by hand | They stop being installed because nothing needs them, and the pins sit unused |
| Can one pin list serve several install sets? | Not easily | Yes, with `-c` on each install |
The second row of the table is the main practical benefit. In a frozen file, an orphaned pin gets installed forever. In a constraints file, it is ignored.
Combining constraints with other options
You can pass -c more than once, and you can combine it with -r:
python -m pip install -r requirements.txt -c constraints.txt -c security-pins.txtYou can also set constraints without typing the flag. This is useful in CI:
export PIP_CONSTRAINT=constraints.txt
python -m pip install -r requirements.txtThe same setting can go in a pip configuration file as constraint = constraints.txt under the [global] section. Because it applies to every install in that environment, an unexpected pin can be hard to trace. Prefer the explicit -c flag in project scripts, and use the environment variable when you cannot edit the command.
A requirements file can reference a constraints file with a -c line inside it:
# requirements.txt
-c constraints.txt
requestsNow python -m pip install -r requirements.txt applies the constraints on its own. Relative paths in such a line are resolved from the directory of the file that contains the line, not from where you run the command.
Limits and errors
Constraints files are stricter than requirements files in a few ways.
- No extras. A line such as
requests[socks]==2.32.3is rejected. Writerequests==2.32.3and request the extra in the requirements file instead. - No editable installs. A
-eline is rejected, which is why the freeze command above excludes them. - Conflicts stop the install. If
requirements.txtsaysrequests>=2.33and the constraints file saysrequests==2.32.3, no version satisfies both. pip reports a resolution error that names the conflicting rules. Fix it by changing one of the two files. Do not work around it by installing with--no-deps.
Checking the result
After installing, confirm that the environment matches what you expect:
python -m pip check
python -m pip freeze --exclude-editable | diff - constraints.txt && echo "environment matches constraints"pip check reports installed packages whose declared dependencies are missing or incompatible. The diff command compares the live environment with the constraints file and prints nothing when they are identical. The && prints the message only on success.
The comparison can show differences even when the install worked. For example, packages that you installed by hand but that are not in the constraints file will appear. Treat a difference as a prompt to investigate, not always as an error.
What constraints do not guarantee
Pinned versions give you repeatable versions. They do not give you everything.
- Different platforms may need different packages. A package can depend on another one only on Windows, or only for older Python versions. A constraints file generated on Linux with Python 3.12 will not mention packages that only other platforms need. Those would be installed unpinned elsewhere.
- The files on the index can change. A version number identifies a release, not an exact file. Pip supports hash-checking with
--require-hashesfor stricter reproducibility, but it requires every requirement to be pinned with a hash, which makes it a different workflow from constraints. - Python itself matters. The same pins can behave differently on Python 3.10 and 3.13. Record the Python version your constraints were generated with, for example in the README.
Summary of the workflow
- List direct dependencies in
requirements.txtwith version specifiers that state what you support. - Install them into a fresh environment and run your tests.
- Freeze the result into
constraints.txt, excluding editable installs. - Install elsewhere with
-r requirements.txt -c constraints.txt. - To upgrade, repeat from step 2 in a new environment.
Exercise
Your constraints.txt contains requests==2.32.3 and flask==3.0.3. Your requirements.txt contains only flask. After python -m pip install -r requirements.txt -c constraints.txt, is requests installed? Explain why.
Show answer
Only if Flask or one of its dependencies requires it. Flask does not depend on requests, so requests is not installed. A constraints file limits versions of packages that are installed for another reason. It never adds a package. Flask is installed at 3.0.3 because requirements.txt asks for it and the constraint fixes the version.
Exercise
You run python -m pip freeze > constraints.txt in an environment where your own project is installed with pip install -e ., and the next install with -c constraints.txt fails. What is the likely cause, and what command avoids it?
Show answer
The freeze output contains a -e line for the editable project, and constraints files cannot contain editable requirements. Use python -m pip freeze --exclude-editable > constraints.txt to leave that line out.
Exercise
Why might you prefer keeping a short requirements.txt and a separate constraints.txt over one frozen requirements file?
Show answer
The short file shows which packages you depend on directly. The constraints file holds the pins for all packages. If you later remove a direct dependency, its transitive packages are no longer installed, and their pins in the constraints file are simply unused. In a single frozen file, those packages would stay installed until someone removed the lines by hand.