Sub topic 10
Build backends and uv build
This chapter assumes you can create a project with uv init, read a pyproject.toml, and run uv sync and uv run. It covers what a build backend is, how the [build-system] table selects one, 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. A frontend such as uv build or pip reads [build-system], installs the backend in an isolated environment, and calls it through a standard interface (PEP 517). The frontend never needs to know how the backend works.
Two kinds of distribution come out of this process:
- A source distribution (sdist) is a
.tar.gzarchive of your source files plus metadata. A consumer must build it before installing. - A wheel is a
.whlfile, a zip archive with a fixed layout. It installs by copying files, so it is fast and runs no build code.
Publishing both is normal. Installers prefer a wheel when one matches the platform and fall back to the sdist.
The [build-system] table
The table has two required keys.
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"requireslists the packages installed into the temporary build environment.build-backendnames the Python object the frontend calls, inmodule.attributeform.
These packages are separate from your runtime dependencies. Your project's httpx is not installed while building, and hatchling is not installed when users run your code.
Choosing a backend
uv does not ship a backend that you must use. It builds with whatever you declare. Common choices:
| Backend | `requires` | `build-backend` | Typical use |
|---|---|---|---|
| Hatchling | `hatchling` | `hatchling.build` | General pure-Python projects, flexible file selection |
| uv_build | `uv_build>=0.8,<0.9` | `uv_build` | Pure-Python projects that want the fastest builds with little configuration |
| setuptools | `setuptools>=61` | `setuptools.build_meta` | Existing projects, C extensions |
| Flit | `flit_core>=3.2` | `flit_core.buildapi` | Very simple pure-Python packages |
| Maturin | `maturin>=1.0` | `maturin` | Rust extensions |
Check the current version bounds in each backend's documentation, because the numbers above change over time. Pure-Python projects work with any of the first three. Pick a backend that supports compiled code only if you need it.
What happens when there is no [build-system]
If pyproject.toml has no [build-system] table, tools fall back to legacy setuptools behavior. uv takes a different approach for projects. It treats a project without a build system as an application and does not install it into the environment. It installs only the dependencies. This is why uv init without --package gives you a project you can run with uv run main.py but that cannot be imported by name or built into a wheel.
uv init --package adds a [build-system] table, a src/ layout, and makes the project installable. With a build system present, uv sync installs your project into .venv in editable mode, so code changes take effect immediately.
Telling the backend which files to include
Backends decide which files go in the wheel. Hatchling detects the src/weather_cli layout automatically when the directory name matches the project name with dashes replaced by underscores. If your package directory has a different name, set it explicitly:
[tool.hatch.build.targets.wheel]
packages = ["src/weather_tools"]A frequent error is a wheel that builds without complaint but contains no code, because the backend could not find the package. Inspect the wheel before publishing, as shown below.
Running uv build
From the project root:
uv buildBy default, uv builds an sdist first, then builds a wheel from that sdist. Building the wheel from the sdist catches a common bug: files that exist in your working tree but are missing from the sdist. Both files go to dist/:
dist/
weather_cli-0.1.0.tar.gz
weather_cli-0.1.0-py3-none-any.whlThe wheel name encodes the tags py3-none-any. This means it works with any Python 3 version, requires no specific ABI, and runs on any platform. Wheels with compiled code have more specific tags, such as cp312-cp312-manylinux_2_28_x86_64.
Useful options
| Command | Effect |
|---|---|
| `uv build --sdist` | Build only the source distribution |
| `uv build --wheel` | Build only the wheel, directly from the source tree |
| `uv build --out-dir build-output` | Write to a directory other than `dist/` |
| `uv build packages/core-lib` | Build a different project by path |
| `uv build --package core-lib` | Build one member of a workspace |
| `uv build --all-packages` | Build every workspace member |
| `uv build --no-build-logs` | Hide backend output |
The version in the file names comes from [project] version. Change it there before each release.
The build pipeline
Verifying the output
A wheel is a zip file, so you can list its contents with the standard library. This script prints every file in each wheel in dist/:
import zipfile
from pathlib import Path
for wheel in sorted(Path("dist").glob("*.whl")):
print(wheel.name)
with zipfile.ZipFile(wheel) as zf:
for name in zf.namelist():
print(" ", name)Run it with uv run python inspect_dist.py. For weather-cli you should see weather_cli/__init__.py and a weather_cli-0.1.0.dist-info/ directory containing METADATA, WHEEL, and entry_points.txt. The last file is how the weather console script from [project.scripts] reaches users. If weather_cli/__init__.py is missing, the backend did not find your package.
To test the wheel the way a user would, install it into a throwaway environment:
uv venv /tmp/check-env
uv pip install --python /tmp/check-env dist/weather_cli-0.1.0-py3-none-any.whl
/tmp/check-env/bin/weatherOn Windows the script lives in Scripts\weather.exe. This test confirms that the package imports and the entry point resolves without relying on your editable install.
Isolation and common errors
The build environment is isolated, so the backend sees only what requires lists. Typical failures:
ModuleNotFoundErrorduring build. Yoursetup.pyor backend hook imports a package that is not inrequires. Add it there.- Backend not found. A typo in
build-backend, for examplehatchling.builds. The module path must match the backend's documentation exactly. - Missing files in the sdist. Data files, templates, or a
README.mdnamed inreadmeare not included. Check the backend's include rules. Because uv builds the wheel from the sdist, this surfaces as an error or a missing file in the wheel. - Wheel contains
tests/or other unwanted directories. Restrictpackagesor add exclude rules. - A version that does not change.
uv buildreads the version frompyproject.toml, or from a dynamic source if you declaredynamic = ["version"]and configure the backend to supply it.
Build constraints and reproducibility
Build requirements such as hatchling are not recorded in uv.lock. Two builds made months apart may use different backend versions. To make builds repeatable, pin the backend in requires, for example hatchling==1.27.0. uv also has build-constraint-dependencies under [tool.uv], which constrains versions of build requirements without editing [build-system]. This matters most when you build dependencies from source, since they use their own backends.
Exercise
You run uv build on a project and the wheel in dist/ contains only a .dist-info directory and no Python modules. Name two likely causes and how you would confirm and fix each.
Show answer
Likely causes: (1) the backend could not locate the package because the directory name under src/ does not match the project name with dashes changed to underscores, or (2) the project has no package directory at all. Confirm by listing the wheel contents with zipfile or unzip -l. Fix the first by setting the backend's package setting, such as [tool.hatch.build.targets.wheel] packages = ["src/your_package"], or by renaming the directory. Fix the second by creating the package directory with an __init__.py. Then delete the old dist/ files and run uv build again.
Exercise
Why does uv build build the wheel from the sdist by default, and which flag skips that step?
Show answer
Building from the sdist verifies that the sdist contains everything needed to build the wheel, so a file that exists only in your working tree causes a failure instead of a broken release. uv build --wheel builds the wheel directly from the source tree and skips the sdist.