Sub topic 5
Lock files: freezing the full dependency tree
This chapter assumes you can create a virtual environment, run pip install, and read a requirements.txt. It also assumes you have trained a model with a framework such as PyTorch or scikit-learn. Earlier chapters covered why environments drift and the difference between declared and resolved dependencies. Here we focus on the artifact that records the resolved set: the lock file.
What a lock file is
A lock file is a machine-generated file that lists every package an environment needs, each at one exact version. It covers your direct dependencies and everything they pull in, which are called transitive dependencies. Installing from the file gives the same package set each time, whatever has been published since.
A declared file such as requirements.in or the dependencies list in pyproject.toml states intent. For example, torch>=2.1 means "any acceptable PyTorch". The lock file states the outcome: torch==2.3.1, plus the exact versions of numpy, sympy, networkx, filelock and every other package that resolution selected.
Without a lock, the resolver runs again on every install. It picks the newest versions that satisfy your constraints at that moment. Two installs a month apart can produce different trees even though nobody edited a file.
Why transitive packages matter in ML
Most ML failures from drift do not come from the packages you named. They come from packages underneath them. A minor release of numpy can change the result of a reduction. A new protobuf or typing-extensions can break an import in a library you never touch directly. Pinning only your top-level packages leaves these free to move.
Consider a project that declares scikit-learn and pandas. A resolved tree might include:
| Package | Declared by you | In the resolved tree |
|---|---|---|
| scikit-learn | yes | 1.4.2 |
| pandas | yes | 2.2.2 |
| numpy | no | 1.26.4 |
| scipy | no | 1.13.0 |
| joblib | no | 1.4.2 |
| threadpoolctl | no | 3.5.0 |
Four of the six rows are transitive. A lock file records all six.
What a lock file contains
Formats differ by tool, but a useful lock file records these things for each package:
- The exact version.
- The source index or URL it came from.
- One or more hashes of the downloadable files, covered in a later chapter.
- Environment markers, such as
python_version < "3.11"orsys_platform == "win32", that say when an entry applies.
Here is a short pip-compile --generate-hashes output. The hashes are shortened to keep the example readable.
numpy==1.26.4 \
--hash=sha256:0f1c2a... \
--hash=sha256:9d7e31...
# via
# pandas
# scikit-learn
# scipy
scipy==1.13.0 \
--hash=sha256:4b8a90... \
--hash=sha256:c61f02...
# via scikit-learnEach package appears with several hashes because one release ships many files: one per Python version, operating system and CPU architecture. The # via comments show which package required it, which helps when you ask why something is installed.
Two styles of lock
Tools take one of two approaches.
A flat, single-target lock is what pip-compile produces. It resolves for the Python version and platform of the machine that ran the command. The file is simple and installs with plain pip. The cost is that a lock made on macOS may not install on Linux, because some packages depend on the platform.
A universal lock is what uv lock produces. It resolves once for all supported platforms and Python versions declared in pyproject.toml, and uses environment markers to select entries at install time. One file then serves a laptop and a Linux CI runner.
For ML work this difference matters. Many teams develop on a Mac and train on Linux with CUDA. A flat lock generated on the laptop will not describe the training machine. Generate the lock on the platform you deploy to, or use a tool with universal resolution and check that it handles your GPU wheels.
The lock lifecycle
The lock file is never edited by hand. The loop is: edit the declared file, regenerate the lock, review the diff, commit both.
Commit the declared file and the lock file in the same change. If they drift apart, a reviewer cannot tell whether the lock reflects the current intent.
Installing: sync versus install
How you install matters as much as having a lock.
# pip: install exactly what the lock lists, and nothing else is resolved
pip install --require-hashes -r requirements.lock
# uv: install from uv.lock, and fail if it is stale
uv sync --lockedpip install -r requirements.lock on an existing environment only adds or upgrades packages. It does not remove packages that the lock no longer lists. An environment built up over months can therefore contain leftovers that make code work locally and fail on a fresh machine. uv sync makes the environment match the lock exactly, removing extras. With pip, build a fresh virtual environment, or use pip-sync from pip-tools, which also removes extras.
uv sync --locked fails when uv.lock does not match pyproject.toml. This is the behavior you want in CI. The related flag --frozen skips that check and installs whatever the lock says, so a stale lock goes unnoticed. Prefer --locked unless you have a specific reason.
Common errors
- Locking on one platform, installing on another. The install fails on a missing wheel, or silently resolves to a different build. Lock on the target platform or use a universal lock.
- Treating
pip freezeas a lock.pip freezelists installed versions without hashes, markers or the reason a package is present. It is a snapshot of one environment, not a resolved specification you can regenerate. - Forgetting the lock after changing a dependency. The declared file says one thing and the lock says another. Check this automatically in CI.
- Putting the lock in
.gitignore. A lock that is not in version control cannot be reviewed or reproduced. - Ignoring the Python version. A lock resolved under Python 3.11 can contain versions that do not exist for 3.9. Record the Python version, as the next chapters show.
Reading a lock diff
Review the lock diff the way you review code. After you change one dependency, the diff should be small and explainable. If a one-line change to requirements.in produces hundreds of changed lines, ask why. Typical causes are a new transitive upgrade pulled in by a looser constraint, or a lock regenerated with a different Python version.
Pay particular attention to changes in numpy, torch, scipy, protobuf and any CUDA-related wheels such as nvidia-cudnn-cu12. These have the most effect on numerical results and compatibility.
Exercise
A teammate runs pip install -r requirements.lock in a virtual environment they have used for six months. The code runs for them but fails on a fresh CI machine with an ImportError. Give two reasons the lock file did not prevent this, and one way to avoid it.
Show answer
First, pip install -r does not remove packages that the lock does not list, so the old environment may contain a leftover package that the code imports but the project never declared. CI, built fresh, lacks it. Second, the lock may have been generated on a different platform or Python version than CI, so CI resolves to different files or fails on a missing wheel. To avoid this, install into a freshly created environment, or use uv sync --locked or pip-sync, which make the environment match the lock exactly, and generate the lock for the same platform and Python version as CI.
Exercise
What is the difference between uv sync --locked and uv sync --frozen, and which belongs in CI?
Show answer
--locked checks that uv.lock is consistent with pyproject.toml and fails if it is stale. --frozen installs from the lock without that check. CI should use --locked, so that a lock file out of date with the declared dependencies fails the build.