Sub topic 7
Understanding uv.lock
This chapter assumes you can add dependencies with uv add and know that pyproject.toml lists the packages your project needs.
What the lockfile records
pyproject.toml states what you accept, such as httpx>=0.27. That range allows many versions. uv.lock records the exact versions uv chose, so every machine installs the same ones.
uv writes uv.lock next to pyproject.toml. It is a TOML file, but you should not edit it by hand. Commit it to version control.
[[package]]
name = "httpx"
version = "0.27.2"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "anyio" },
{ name = "certifi" },
]Each [[package]] entry holds a name, an exact version, a source, its dependencies, and file hashes. The hashes let uv detect a tampered or changed download.
The lock is universal. uv resolves for all platforms and supported Python versions at once, so a lockfile made on macOS also works on Linux and Windows.
When the lock changes
Commands such as uv add, uv remove, uv lock, uv sync, and uv run all check the lock first. When it is stale, they update it automatically.
When uv re-resolves, it keeps existing locked versions where it can. Adding a new package does not upgrade your other packages.
Upgrading
Locked versions stay fixed until you ask for a change.
uv lock --upgrade
uv lock --upgrade-package httpx
uv lock --upgrade-package "httpx==0.27.0"The first command re-resolves everything to the newest allowed versions. The second upgrades only httpx. The third moves httpx to a specific version. In every case the new versions must still satisfy the ranges in pyproject.toml.
Checking the lock
uv lock --check
uv sync --locked
uv sync --frozenuv lock --checkexits with an error ifuv.lockis out of date. It never changes the file.uv sync --lockedinstalls from the lock and fails if the lock does not matchpyproject.toml.uv sync --frozeninstalls from the lock without checking it againstpyproject.toml.
Common errors
- Not committing
uv.lock. Teammates and CI then resolve on their own and can get different versions. - Editing
pyproject.tomland forgetting to lock.uv sync --lockedfails with a message that the lockfile needs updating. Runuv lockand commit the result. - Using
--frozento hide a stale lock. The install succeeds, but it may not match whatpyproject.tomldeclares.
Exercise
A teammate changes a dependency range in pyproject.toml but forgets to run uv lock. CI runs uv sync --locked. What happens, and how do you fix it?
Show answer
The command fails because uv.lock no longer matches pyproject.toml. Run uv lock locally to regenerate the lockfile, then commit both files.