Sub topic 5
Lock files: freezing the full dependency tree
This chapter assumes basic Python and pip usage, command-line comfort, and familiarity with training an ML model. You should also know basic Git so you can review a lock-file change.
Why a dependency list is not enough
A dependency declaration names the packages your project needs directly:
# requirements.in
numpy
scikit-learn
torchThis list does not fully describe the environment. Each package can require other packages. For example, scikit-learn may require joblib, scipy, and threadpoolctl. Those packages can have dependencies of their own.
A lock file records the resolved dependency tree. It contains the exact version selected for each package, and often the hashes of the package files that may be installed.
| File | Purpose |
|---|---|
| `requirements.in` | Human-maintained direct dependencies |
| `requirements.lock` | Exact resolved packages and versions |
| `pyproject.toml` | Project metadata and dependency declarations |
| `uv.lock` | Exact resolution produced by `uv` |
The resolver chooses versions that satisfy all declared constraints. Installing from the lock file repeats that choice instead of resolving the tree again.
flowchart TD
A[Direct dependencies] --> B[Dependency resolver]
B --> C[Transitive dependencies]
C --> D[Lock file]
D --> E[Repeatable installation]Creating and using a lock file
With pip-tools, put direct requirements in requirements.in, then generate a locked file:
python -m pip install pip-tools
pip-compile --generate-hashes \
--output-file requirements.lock \
requirements.in
python -m pip install --require-hashes -r requirements.lockpip-compile resolves both direct and transitive packages. --generate-hashes adds checksums for allowed distribution files. --require-hashes makes installation fail if a package is missing a permitted hash or has been modified.
With uv, dependencies normally live in pyproject.toml:
uv lock
uv sync --lockedThe --locked option tells uv not to silently update the lock file. It fails when the project definition and lock file disagree. This makes a stale lock file visible during local development or CI.
Common errors
- Editing the lock file by hand. Change the direct dependency declaration and regenerate it.
- Installing with ordinary
pip installafter creating a lock file. This can resolve newer versions outside the lock. - Forgetting to commit the lock file. The lock file is part of the project’s build input.
- Treating a lock file as universal across platforms. Some packages publish different artifacts for Linux, macOS, Windows, or different CPU architectures.
- Updating every package without review. A broad update can change model behavior or introduce a breaking release.
A practical workflow is to regenerate the lock file in a separate change, inspect the package and hash differences, run tests, and then merge the update. Keep the old lock file available through version control so the previous environment can be rebuilt.
Exercise
Your project declares pandas but the lock file also contains numpy, python-dateutil, and six. Why are the additional packages present, and what should you edit when upgrading pandas?
Show answer
They are transitive dependencies selected by the resolver. Edit the direct dependency declaration, such as requirements.in or pyproject.toml, then regenerate the lock file rather than editing the lock file manually.