DEV Community

Alex Chen
Alex Chen

Posted on

Quick Tip: Pin Any Python Dependency Set with uv's `--constraint` Flag (No Lockfile Needed)

Quick Tip

pip install -r requirements.txt in five repos, and a transitive dep ships a breaking release — suddenly only some of your services break, depending on when each last installed. Lockfiles fix it per-repo, but keeping five lockfiles in sync is its own job.

uv supports a lighter primitive most people skip: constraints. A constraint file pins ceilings, not exact versions:

# constraints.txt — shared across all repos
pydantic<2.12
httpx<0.29
fastapi<0.120

# in each repo
uv pip install -r requirements.txt --constraint ../constraints.txt
Enter fullscreen mode Exit fullscreen mode

Unlike -r, a --constraint entry only applies if the package gets installed — so one shared file works even when repos have different dependency sets.

I replaced a nightly "did anything break?" CI job with this. Diffing constraints.txt in one place beats diffing five lockfiles after the fact.

What's your cross-repo version strategy — lockfiles per service, a constraints file, or a private index?

Powered by MonkeyCode — the free, open-source AI coding assistant: https://ly.cyberserval.tech/iIETXiF

Top comments (0)