Sub topic 7
Understanding uv.lock
This chapter assumes you can read a pyproject.toml, know what uv add and uv sync do, and understand version specifiers such as httpx>=0.27. It explains what uv.lock records, when uv rewrites it, and how to update it on purpose.
Why a lockfile exists
Your pyproject.toml states what the project accepts. A line like httpx>=0.27 allows hundreds of releases, and it says nothing about the packages httpx itself needs. Two installs a month apart can therefore produce different environments.
uv.lock records the exact result of one resolution. Resolution is the step where uv picks a single version of every direct and transitive dependency so that all constraints hold. The lockfile stores that answer so later installs repeat it.
| File | Written by | Describes |
|---|---|---|
| `pyproject.toml` | You | Allowed ranges and project metadata |
| `uv.lock` | uv | The exact versions chosen, with file hashes |
What the file contains
uv.lock is a TOML file in the project root. Here is an abbreviated excerpt for the weather-cli project from earlier chapters. Hashes are shortened.
version = 1
requires-python = ">=3.12"
[[package]]
name = "httpx"
version = "0.27.2"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "anyio" },
{ name = "certifi" },
{ name = "httpcore" },
{ name = "idna" },
{ name = "sniffio" },
]
sdist = { url = "https://files.pythonhosted.org/.../httpx-0.27.2.tar.gz", hash = "sha256:..." }
wheels = [
{ url = "https://files.pythonhosted.org/.../httpx-0.27.2-py3-none-any.whl", hash = "sha256:..." },
]
[[package]]
name = "weather-cli"
version = "0.1.0"
source = { editable = "." }
dependencies = [
{ name = "httpx" },
{ name = "typer" },
]
[package.dev-dependencies]
dev = [
{ name = "pytest" },
]Each [[package]] entry holds the following fields:
nameandversion: the pinned release.source: where the package comes from. Possible values include a registry, a git URL, a local path, oreditablefor the project itself.dependencies: the names of the packages it needs. Those packages have their own entries.sdistandwheels: the downloadable files, each with a SHA-256 hash. uv checks every download against its hash, so a tampered or replaced file fails the install.
The project appears in its own lockfile. Its direct dependencies and its dependency groups are listed under it, and that is how uv knows which packages are roots.
Universal resolution
Some dependencies apply only on certain platforms. For example, a package might require colorama only on Windows, or a different release on Python 3.12 than on 3.13.
uv resolves for all platforms and all Python versions allowed by requires-python, and the result is one lockfile. Packages that apply only in some situations carry markers (conditions such as sys_platform == 'win32') inside their dependency entries. A Linux CI runner and a macOS laptop can therefore install from the same file.
This is why requires-python matters. A wider range such as >=3.9 forces uv to find versions that work across more interpreters. That can pin you to older releases of some packages.
When uv writes the lockfile
You rarely run uv lock by hand. These commands refresh the lockfile first if it is missing or stale:
uv addanduv removechangepyproject.toml, then re-lock.uv runanduv synccheck that the lockfile matchespyproject.tomland re-lock if it does not.uv lockonly resolves and writes the file. It does not touch the environment.
An important behavior is that uv keeps existing pins when it re-locks. If you add rich to a project, httpx stays at its locked version even if a newer one exists. Versions change only when you ask or when a constraint forces them.
uv lock --upgrade # re-resolve everything to the newest allowed versions
uv lock --upgrade-package httpx # upgrade only httpx (and what it requires)
uv lock --upgrade-package "httpx==0.27.0" # move one package to a specific version
uv lock --check # exit non-zero if uv.lock is out of date
uv tree # show the locked dependency treeCommit it, but do not edit it
For an application or a CLI like weather-cli, commit uv.lock to version control. Teammates and CI then install the same versions you tested. Diffs to the file in a pull request show exactly which packages changed.
Do not edit it by hand. A hand edit can break the hash checks and the internal consistency uv relies on. Change pyproject.toml and let uv regenerate the file.
For a library you publish, the lockfile still helps your own development and CI. Consumers of your library never see it, because it is not included in the built distribution. They resolve your dependency ranges themselves. Keep your ranges honest, because the lockfile only proves your project works with the pinned versions.
Lockfile and pyproject.toml must agree
If you edit pyproject.toml directly, for example by changing httpx>=0.27 to httpx>=0.28, the lockfile becomes stale. The next uv run fixes it automatically. In CI you often want the opposite, a failure, so a forgotten re-lock does not pass unnoticed.
| Command | Lockfile stale | Lockfile current |
|---|---|---|
| `uv sync` | Re-locks, then installs | Installs |
| `uv sync --locked` | Fails with an error | Installs |
| `uv sync --frozen` | Installs from the file anyway, without checking | Installs |
| `uv lock --check` | Fails | Succeeds |
--locked asserts that the lockfile is up to date. --frozen skips the check and trusts the file as it is. Use --locked in CI to catch drift. Use --frozen only when you cannot afford any resolution step, such as in a container build where pyproject.toml has been partly copied. The next chapters on environments and CI build on this.
Common errors
- Lockfile missing from the repository. Someone's
.gitignoreexcludesuv.lock. Each machine then resolves on its own and results drift. - Unsatisfiable constraints. If two dependencies need incompatible versions of a third,
uv lockstops with an error that names the conflicting requirements. Loosen one range or upgrade the package that is too strict. - Surprise upgrades. A plain
uv lockdoes not upgrade. If versions moved, someone ran--upgrade, or a new constraint forced a change. Theuv.lockdiff shows which. - Expecting
uv pip installto use it. Theuv pipinterface does not read or writeuv.lock. Only the project commands do. - Needing a requirements file. Some deployment tools want
requirements.txt.uv export --format requirements-txtwrites one from the lockfile, with hashes.
Exercise
Your teammate changed httpx>=0.27 to httpx>=0.28 in pyproject.toml but forgot to run any uv command, then pushed. CI runs uv sync --locked. What happens, and what should the teammate do?
Show answer
CI fails, because --locked requires uv.lock to match pyproject.toml, and the constraint change made the lockfile stale. The teammate should run uv lock (or uv sync) locally, check the uv.lock diff, and commit the updated lockfile alongside the pyproject.toml change.
Exercise
You want only typer upgraded to its newest allowed release, leaving every other package at its locked version. Which command do you run, and where do you look to confirm the result?
Show answer
Run uv lock --upgrade-package typer. Then check the uv.lock diff, or run uv tree, to confirm that typer (and anything it newly requires) changed while the other pins did not.