Sub topic 6
Creating lock files with pip-tools and uv
This chapter shows how to turn a short list of direct dependencies into a fully pinned lock file with two tools, pip-tools and uv. It assumes you can create a virtual environment, install packages with pip, and read a requirements.txt. It also assumes you know why declared and resolved dependencies differ, as covered in earlier chapters.
The job of a lock tool
You write down only what your code imports, such as numpy, scikit-learn and torch. A lock tool runs a dependency resolver, which picks one concrete version of every package, including transitive dependencies you never named. It writes the result to a file that can be committed and installed later.
A good lock tool does three things:
- It records exact versions for the whole tree.
- It records a hash for each downloadable file, so a changed or tampered artifact is rejected.
- It can tell you whether the lock still matches what you declared.
pip-tools
pip-tools adds two commands to pip: pip-compile, which resolves, and pip-sync, which makes an environment match a lock file exactly. You keep the input in requirements.in, one direct dependency per line, optionally with loose constraints such as scikit-learn>=1.4.
# requirements.in lists only the direct dependencies, for example:
# numpy
# scikit-learn
# torch
pip install pip-tools
pip-compile --generate-hashes -o requirements.lock requirements.in
pip install --require-hashes -r requirements.lockThe output file lists every package as name==version with one or more --hash=sha256:... lines. It also has # via comments that show which package pulled each dependency in. Those comments help when you ask why a package is installed at all.
pip's --require-hashes mode is strict. Every requirement must be pinned with == and carry a hash, and pip rejects the install otherwise. It also does not resolve dependencies at install time, so nothing unlisted can slip in.
Platform and Python version
pip-compile resolves for the interpreter and operating system it runs on. A lock built on macOS with Python 3.12 can lack Linux wheels, or contain packages that only apply to macOS. Compile the lock in the same environment where you will install it, or inside a container that matches production. The Docker chapters come back to this.
Useful pip-compile options
| Goal | Command |
|---|---|
| Refresh everything to the newest allowed versions | `pip-compile --upgrade --generate-hashes -o requirements.lock requirements.in` |
| Upgrade one package only | `pip-compile --upgrade-package numpy --generate-hashes -o requirements.lock requirements.in` |
| Make an environment match the lock exactly, removing extras | `pip-sync requirements.lock` |
If you compile with hashes, keep passing --generate-hashes on every run. When you leave it out, the next compile writes a lock without hashes, and --require-hashes installs then fail.
uv
uv is a Python package manager written in Rust. It covers resolving, locking, installing and environment management in one tool. Its project workflow reads the declared dependencies from pyproject.toml.
[project]
name = "ml-train"
version = "0.1.0"
requires-python = ">=3.11,<3.12"
dependencies = [
"numpy",
"scikit-learn",
"torch",
]Then lock and install:
uv lock
uv sync --lockeduv lock writes uv.lock, which records versions, source URLs and hashes for every package. You do not need a flag to turn hashes on. uv sync --locked installs the project into .venv from that file. It fails if uv.lock no longer matches pyproject.toml, so a stale lock cannot pass unnoticed.
Differences from pip-compile
- Universal resolution.
uv.lockis resolved for several platforms and Python versions in one file, within the range set byrequires-python. You can lock on a laptop and install on Linux. - Declared Python range matters. If
requires-pythonis wider than you test, the resolver must find versions that work across the whole range. Narrow it to what you actually run. - Three install modes. Each reacts differently to a stale lock.
| Command | Behavior when `uv.lock` is out of date |
|---|---|
| `uv sync` | Silently re-locks, then installs |
| `uv sync --locked` | Fails with an error |
| `uv sync --frozen` | Installs the lock as written and does not check it against `pyproject.toml` |
Use --locked in CI and Docker builds, because you want a mismatch to stop the build. Use --frozen only when you deliberately want the file as it is.
To check a lock without installing, run uv lock --check. To upgrade a single package, run uv lock --upgrade-package numpy.
Keeping a requirements-style workflow
If your team or Dockerfile still depends on a requirements file, uv offers a drop-in replacement for pip-tools:
uv pip compile requirements.in --generate-hashes -o requirements.lock
uv pip sync requirements.lockThis works like pip-compile and produces a per-platform file. The next chapters use requirements.lock in the Dockerfile for this reason.
Choosing between them
| Question | pip-tools | uv project workflow |
|---|---|---|
| Where are direct dependencies declared? | `requirements.in` | `pyproject.toml` |
| Is the lock cross-platform? | No, per interpreter and OS | Yes, within `requires-python` |
| Are hashes included? | Only with `--generate-hashes` | Always |
| Does a stale lock fail the install? | Not by itself; you must compare in CI | Yes, with `--locked` |
| Format | Plain text, readable by pip alone | `uv.lock`, read by uv |
Pick pip-tools when the install side must work with plain pip, for example on a platform where you cannot add another tool. Pick uv when you want one tool to manage the environment and a lock that survives a change of machine.
Gotchas with ML packages
- PyTorch and CUDA wheels. The default PyPI wheel for
torchon Linux bundles a specific CUDA runtime. To get a different build, you point the tool at PyTorch's own package index. With pip-compile, add--extra-index-urlfor that index. With uv, declare the index inpyproject.toml. Check the PyTorch install page for the current URL for your CUDA version. Then confirm the lock records the build you wanted. - Two indexes can mix sources. If a package name exists on both indexes, an extra index can supply the wrong one. Prefer a setup in which each package comes from one named index, and read the lock after the first compile.
- Direct dependencies stay loose on purpose. Do not copy exact versions into
requirements.in. The lock is the pin, and the input says what you need. - Never edit a lock by hand. Change the input and recompile, so hashes and
viainformation stay correct. - Commit the lock. A lock that lives only on one laptop gives no reproducibility.
Exercise
A CI job runs uv sync --locked and fails after a teammate added pandas to pyproject.toml but did not commit a new uv.lock. What happened, and what should the teammate do?
Show answer
uv sync --locked compares uv.lock with pyproject.toml and fails when they disagree, which is the intended protection against a stale lock. The teammate should run uv lock locally, check that uv.lock now includes pandas and its dependencies, and commit both files together.
Exercise
You run pip-compile without --generate-hashes, and then your Dockerfile runs pip install --require-hashes -r requirements.lock. What is the outcome, and how do you fix it?
Show answer
The install fails, because --require-hashes demands a hash for every requirement and the lock has none. Recompile with pip-compile --generate-hashes -o requirements.lock requirements.in and commit the result.