Sub topic 6
Creating lock files with pip-tools and uv
Assume you know basic Python, pip, virtual environments, and Git. This chapter uses a lock file to record the complete dependency set that an ML project installs.
From direct requirements to a lock file
A project usually declares only its direct dependencies. For example, requirements.in might contain:
numpy
scikit-learn
torchThese names are not a complete environment. Each package can depend on other packages, and an unconstrained install may choose different versions later.
A lock file records the resolved dependency tree. Resolution is the process of selecting versions that work together. A locked installation uses those exact selections instead of resolving them again.
| File | Purpose |
|---|---|
| `requirements.in` | Human-maintained direct dependencies |
| `requirements.lock` | Fully resolved packages and versions |
| `pyproject.toml` | Project metadata and dependencies for tools such as `uv` |
| `uv.lock` | `uv`'s resolved dependency graph |
Create a lock file with pip-tools
Install pip-tools in an isolated environment or a controlled developer environment. Then compile the direct requirements:
python -m pip install pip-tools
pip-compile \
--generate-hashes \
--output-file requirements.lock \
requirements.in
python -m pip install \
--require-hashes \
--requirement requirements.lockpip-compile resolves both the direct packages and their transitive dependencies, which are dependencies of those direct packages. The --generate-hashes option records hashes for the package artifacts. A hash is a fingerprint of a file. With --require-hashes, pip rejects an artifact that is missing or has a different hash.
Commit both input and output files:
git add requirements.in requirements.lock
git commit -m "Lock Python dependencies"When you intentionally change a direct dependency, edit requirements.in, run pip-compile again, inspect the diff, and test the result. Do not edit generated entries in the lock file as a normal update method.
Create a lock file with uv
uv uses pyproject.toml as the project input. A minimal project can be initialized with:
uv init --python 3.11
uv add numpy scikit-learn torch
uv lock
uv sync --lockedThe uv add command updates the project metadata and resolves dependencies. uv lock writes or refreshes uv.lock. The uv sync --locked command installs from the lock file and fails if the lock file is stale compared with pyproject.toml.
Use uv lock --upgrade-package PACKAGE when you want to update one package deliberately. Review all changed versions, run the model tests, and commit the updated pyproject.toml and uv.lock together.
Choosing between the tools
pip-tools fits projects that already use requirements.txt workflows and need hash-pinned installation. uv provides project management, resolution, virtual-environment synchronization, and locking in one workflow. Choose one as the project standard so developers and CI do not resolve dependencies differently.
Exercise
What command should you run after changing requirements.in, and what should you inspect?
Show answer
Run pip-compile again, inspect the lock-file diff, and run the project's tests before committing both files.