Lesson 6 of 9 · 24m
Lockfiles, Applications, and Reproducibility
Why libraries shouldn't ship lockfiles, why applications must, and how hashes and constraints files fit into production deployment.
A lockfile is a frozen answer to dependency resolution: exact versions, often with hashes, for one deployable environment. It is essential for applications and usually wrong for libraries. The distinction matters because libraries and apps have opposite audiences.
Libraries resolve everywhere; applications deploy somewhere
A library is installed into unknown environments. Locking it to exact versions would make it impossible to compose with other packages. An application is deployed to known environments, so reproducibility wins. The library publishes ranges; the application locks the resulting graph.
library pyproject.toml: httpx>=0.26,<1
application lockfile: httpx==0.27.2Requirements files can be lockfiles
pip-compile --generate-hashes --output-file requirements.txt requirements.in
pip-sync requirements.txtrequirements.in contains direct dependencies. The generated file contains the full transitive graph, including entries such as httpx==0.27.2 with one or more --hash=sha256:... lines. Hash checking protects against accidental corruption and some supply-chain attacks; changing the lockfile becomes a reviewable event.
uv and Poetry make locking first-class
uv pip compile requirements.in -o requirements.txt
uv pip sync requirements.txtPoetry's equivalent is poetry lock followed by poetry install --only main. Different tools, same operational principle: the lockfile belongs in version control. Don't generate it during deployment; deployment should install an already-reviewed artifact.
Constraints files are for shared policy
Large organizations often need one more layer. A constraints file doesn't say what to install; it limits what may be selected:
pip install -r requirements.txt -c constraints.txtUse it to enforce security floors (cryptography>=42.0.0) or block known-bad versions across many services without editing every requirements file.
CI needs both ends of the range
For libraries, test:
- lowest supported direct dependencies,
- latest resolver-selected dependencies,
- supported Python versions and platforms.
This catches both the stale lower bound and the new upstream breaking release. A single green CI run on one machine is compatibility theater.
Field notes
- Recreate lockfiles deliberately; blind regeneration turns dependency upgrades into unreviewed noise.
- Lockfile merge conflicts are normal in active repos. Regenerate from the merged inputs instead of hand-editing huge blocks.
- Never commit credentials embedded in index URLs inside lockfiles.
- Reproducibility includes the Python version and platform wheels, not just package versions.
Was this lesson helpful?