Sub topic 3
Build, publish, and extend a uv project
Assume you know Python packages, virtual environments, pip, and basic terminal commands. This chapter focuses on packaging and workflows around an existing uv project.
Build backends and `uv build`
A build backend converts your source tree into distributions such as a wheel and source archive. The [build-system] table selects that backend.
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"uv build creates these files in dist/. The backend controls how package files are discovered and assembled. uv is the build frontend: it creates an isolated build environment and calls the backend.
Reproducible installs in CI
Commit uv.lock. In continuous integration (CI), use it as a verification boundary:
uv sync --locked
uv run pytest--locked fails when pyproject.toml and uv.lock disagree. --frozen uses the lockfile without attempting to update it. Both prevent CI from silently resolving different versions.
Sources and workspaces
[tool.uv.sources] selects where a dependency comes from:
[tool.uv.sources]
shared = { path = "../shared", editable = true }
client = { git = "https://github.com/example/client.git" }A source can also select a configured package index. A workspace combines related packages:
[tool.uv.workspace]
members = ["packages/*"]A member dependency can use { workspace = true }. This lets several packages share one lockfile while retaining separate package metadata.
Tools and single-file scripts
Run a command-line tool without adding it to the project:
uvx ruff check .For a one-file script, declare inline metadata:
# /// script
# requires-python = ">=3.12"
# dependencies = ["httpx"]
# ///
import httpx
print(httpx.get("https://example.com").status_code)Run it with uv run script.py. uv creates a temporary environment from that metadata.
Publishing and existing workflows
Build first, inspect dist/, then publish with credentials supplied through environment variables:
uv build
uv publishThe uv pip interface helps migrate gradually:
uv pip install -r requirements.txtFor a full migration, initialize a project, add the requirements as project dependencies, review the generated pyproject.toml, then run uv lock and replace the old installation command with uv sync.
Exercise
Which command should CI use to reject an out-of-date lockfile?
Show answer
uv sync --locked.