brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: pip install -c caps versions without installing anything extra

Pip
~/tips/tip-pip-constraints-file

urllib3<2

Limit which version a dependency may get, without adding it to the project.

A dependency of a dependency releases a bad version and your install breaks. You don’t use that package directly, so adding it to your requirements feels wrong. A constraints file is made for this.

It looks like a requirements file:

# constraints.txt
urllib3<2

Pass it with -c next to your normal install:

python3 -m pip install -r requirements.txt -c constraints.txt

The rule only applies if something else asks for urllib3. A constraint never causes a package to be installed on its own, which is the whole difference with -r.

I like it as a record of workarounds. Add a comment above each line saying why the cap is there, and you know when it’s safe to delete.

You can also set it for a shell session or a CI job with the PIP_CONSTRAINT environment variable, handy when you can’t change the command itself.

The catch is that constraints can make a resolve impossible. If two packages need ranges that don’t overlap with your cap, pip stops with a conflict error and you have to loosen something. Old caps also keep you away from security fixes, so check the file now and then.

Further reading: pip user guide: constraints files

Going deeper: Guide: let git bisect find the commit that broke your code

More Pip tips python3 -m pip download -r requirements.txt -d ./wheels python3 -m pip config set global.timeout 60 python3 -m pip index versions requests python3 -m pip download requests==X.Y.Z --no-deps -d ./pkgs python3 -m pip install --only-binary=:all: numpy All Pip tips (9) All topics

Link