Field Notes: Pinning & Lockfiles
Every Python team eventually argues about pins. The rules below settle most of those arguments, because they follow from one distinction: an application is the end of the dependency graph, a library is somewhere in the middle of other people's graphs.
Pin in applications, not in libraries
| Application (a service, a CLI you deploy, a notebook project) | Library (anything others pip install and import) |
|
|---|---|---|
[project] dependencies |
reasonable ranges (requests>=2.31) |
the widest ranges you support (requests>=2.28) |
| Exact versions | in the lockfile (uv.lock, poetry.lock, compiled requirements.txt), committed |
never in published metadata |
Upper bounds (<3) |
fine, you control the whole graph | only for known breakage, not "just in case" |
| Lockfile committed? | yes, it is the deployment | yes, for your own CI, but users never see it |
Why libraries mustn't pin: your users install your library together with everything else they use. requests==2.31.0 in a library's metadata means no user of it can ever have a newer requests, and the first other library that needs requests>=2.32 makes the graph unsolvable, exactly like the conflict lesson. Speculative upper bounds (attrs<25) cause the same thing more slowly.
Test the bottom of your ranges
A lower bound you never test is a guess. uv can resolve to the lowest versions your ranges allow, so a CI job can prove that attrs>=24 really works with attrs 24:
uv init -q --lib tadcalc && cd tadcalc
uv add -q "attrs>=24" "requests>=2.31"
echo "--- normal (highest) resolution"
uv tree --depth 1 2>/dev/null | tail -2
echo "--- lowest-direct: oldest versions of your direct dependencies"
uv lock --resolution lowest-direct 2>&1 | grep -v '^Resolved'
uv tree --depth 1 --resolution lowest-direct 2>/dev/null | tail -2--- normal (highest) resolution
├── attrs v26.1.0
└── requests v2.34.2
--- lowest-direct: oldest versions of your direct dependencies
Ignoring existing lockfile due to change in resolution mode: `highest` vs. `lowest-direct`
Updated attrs v26.1.0 -> v24.2.0
Updated requests v2.34.2 -> v2.31.0
├── attrs v24.2.0
└── requests v2.31.0
Switching the resolution mode re-resolved and rewrote uv.lock, as the "Ignoring existing lockfile" line says. So run this in a separate CI job on a throwaway checkout (uv run --resolution lowest-direct pytest), never committing that lock, next to the normal job that uses uv sync --locked. If the old end fails, raise the lower bound; it was wrong.
Lockfiles in CI
- Install from the lock, and fail if it's stale.
uv sync --locked,poetry install(afterpoetry check --lock),pip install -r requirements.txtfrom a compiled file. Neveruv lock/poetry lockin CI: CI tests the commit, it doesn't change it. - Verify hashes. Lockfiles store SHA-256 hashes; uv and Poetry check them automatically. With pip, compile with
--generate-hashes, and then pip insists that every file match:
python3 -m venv .venv
echo 'idna==3.20' > requirements.in
uv pip compile -q --generate-hashes requirements.in -o requirements.txt
cat requirements.txt
.venv/bin/pip install -q --require-hashes -r requirements.txt && echo "hashes OK"
echo "--- the index now serves a different file under that name (simulated by editing the hash)"
python3 -m venv .venv2
sed -i 's/sha256:\(.\)/sha256:0\1/; s/sha256:\(.\{64\}\)./sha256:\1/' requirements.txt
.venv2/bin/pip install -q --require-hashes -r requirements.txt 2>&1 | sed -n '1,4p'# This file was autogenerated by uv via the following command:
# uv pip compile --generate-hashes requirements.in -o requirements.txt
idna==3.20 \
--hash=sha256:ab7ae7122974553370f0bdb919e1a960b2cd1bc1ef0276416d896db81c14582c
# via -r requirements.in
hashes OK
--- the index now serves a different file under that name (simulated by editing the hash)
ERROR: THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE. If you have updated the package versions, please update the hashes. Otherwise, examine the package contents carefully; someone may have tampered with them.
idna==3.20 from http://127.0.0.1:8080/packages/idna-3.20-py3-none-any.whl#sha256=ab7ae7122974553370f0bdb919e1a960b2cd1bc1ef0276416d896db81c14582c (from -r requirements.txt (line 3)):
Expected sha256 0ab7ae7122974553370f0bdb919e1a960b2cd1bc1ef0276416d896db81c14582
Got ab7ae7122974553370f0bdb919e1a960b2cd1bc1ef0276416d896db81c14582c
- Update in small, reviewed steps. Let a bot (Dependabot, Renovate; both understand
uv.lock,poetry.lockand requirements files) open one PR per update or per group, so CI tests each change and the lockfile diff tells reviewers exactly what moved. - Cache by lockfile hash. CI caches (
~/.cache/uv,~/.cache/pypoetry) keyed onhashFiles('uv.lock')are rebuilt only when dependencies change.
Other pins people forget
| Input | Pin it with |
|---|---|
| The Python version | .python-version + requires-python, or the CI image's Python |
| The project manager itself | required-version = ">=0.13,<0.14" in [tool.uv]; requires-poetry = ">=2.5" in [tool.poetry]; setup-uv with version: in CI |
| Build backends | [build-system] requires = ["hatchling>=1.27"]; add an upper bound only if you've seen breakage |
| "Now" | uv lock --exclude-newer 2026-10-01 resolves as if newer releases didn't exist: great for reproducing an old resolution, or for a cooling-off period against freshly published malicious releases |
When an upstream release breaks you
- Pin the bad version out, not the good one in:
somelib!=4.2.0lets 4.2.1 through when it's fixed. - In an application, a temporary exact pin is fine; add a comment with the issue link and a date.
- In a library, never ship the workaround as an upper bound unless the incompatibility is real and permanent; tell users about a constraints file instead.
Test yourself
Your library needs requests. What goes in its [project] dependencies?
- requests==2.34.2
- requests>=2.28 (the widest range you actually support and test)
- requests<3,>=2.34.2,!=2.33
- Nothing; users install it themselves
What does uv lock --resolution lowest-direct help you test?
- The newest versions
- That the lower bounds of your direct dependencies are correct
- Hash verification
- Python 2 support
Why should CI run uv sync --locked rather than uv lock?
- It's faster to type
- CI must test the committed lockfile and fail if it's stale, not silently produce a different resolution
- uv lock needs a network
- They're identical