Sub topic 10
Build backends and uv build
This chapter assumes you can create a uv project, read a pyproject.toml, and know that a package has a src layout. It covers the [build-system] table and what uv build produces.
What a build backend does
A build backend is a Python package that turns your source tree into distribution files. uv does not build packages itself. It reads [build-system], installs the backend in an isolated environment, and asks the backend to produce the files.
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"requireslists the packages needed at build time. These are separate from your runtime dependencies.build-backendnames the Python object that implements the build interface defined by PEP 517.
Common backends include hatchling, setuptools, flit-core, and uv_build. For a pure-Python project, any of them works. Pick one and keep it. Changing it later rarely affects users.
Running uv build
Run this from the project root:
uv builduv creates two files in dist/:
| File | Kind | Purpose |
|---|---|---|
| `weather_cli-0.1.0.tar.gz` | sdist (source distribution) | Source plus metadata. Installers can rebuild from it. |
| `weather_cli-0.1.0-py3-none-any.whl` | wheel | A prebuilt archive. Installers unpack it without running build code. |
The wheel name encodes the Python tag, ABI tag, and platform tag. py3-none-any means pure Python that runs on any platform. Installers prefer wheels because installing one is fast and runs no build code.
By default uv builds the sdist first, then builds the wheel from that sdist. This catches files that exist in your checkout but are missing from the sdist. Use uv build --wheel or uv build --sdist to build only one.
flowchart TD
A[pyproject.toml and src/] --> B[uv build]
B --> C[Backend in isolated environment]
C --> D[dist/*.tar.gz]
D --> E[dist/*.whl]Why a project may not be a package
uv init --package writes a [build-system] table. A plain uv init project (an application) has none. Without one, uv treats the project as a collection of dependencies and does not install your own code into the environment. It also cannot build the project. Running uv build there fails or builds with default settings, so add a [build-system] table before you build.
Common errors
- Missing
[build-system]. Console scripts under[project.scripts]are not installed. Add the table. - Wrong module path. With a
srclayout,hatchlingfindssrc/weather_clifrom the project name. If your package directory has another name, set[tool.hatch.build.targets.wheel] packages = ["src/other_name"]. - Backend not found. A typo in
build-backend, for examplehatchling.builds, gives an import error during the build. - Stale
dist/. Old files stay there. Clean it before you publish.
Exercise
You run uv build in a project and the build succeeds, but after installing the wheel, import weather_cli fails. Name one likely cause and where you would fix it.
Show answer
The backend did not include your package. This happens when the package directory name differs from the project name. Fix it in pyproject.toml by setting the backend's package setting, for example [tool.hatch.build.targets.wheel] packages = ["src/your_package"]. Then rebuild and inspect the wheel with unzip -l.