Sub topic 3
Build frontends and backends: what python -m build actually does
This chapter assumes you can read a basic pyproject.toml, create a virtual environment, and run pip. It explains the two programs involved when you turn a source tree into distributable files, and what happens between pressing Enter on python -m build and finding files in dist/.
Two roles, one contract
Packaging tools split the work between two kinds of program.
- A build frontend is the tool you run. It reads the project configuration, prepares an environment, and asks someone else to do the building.
python -m buildandpipare frontends. - A build backend is the library that knows how to produce the artifacts.
setuptools.build_meta,hatchling.build,flit_core.buildapi, andpdm.backendare backends.
The two communicate through a fixed interface defined in PEP 517. PEP 518 defines how the project declares which backend it needs. Because the interface is standard, any frontend can drive any backend, and you can change backends by editing two lines of pyproject.toml.
The [build-system] table
The table has two keys:
[build-system]
requires = ["setuptools>=77"]
build-backend = "setuptools.build_meta"requireslists packages that must be installed before the build starts. This is a list of requirement strings, the same format as dependencies. It should include the backend and anything the backend needs at build time.build-backendis an import path to the object that provides the hooks. The part before the colon (if any) is the module, and the part after is an attribute on it.
The requires list is not your library's runtime dependency list. Those belong in [project].dependencies. A user who installs your wheel never installs setuptools because of this table, since the wheel is already built.
In the spine's example, setuptools>=77 is there for a reason. The SPDX-style license = "MIT" string and license-files in [project] are only understood by recent setuptools. With an older backend, the build fails or ignores those fields. The backend version is part of what your metadata is allowed to say.
What the frontend does, step by step
When you run python -m build in the project root, the frontend does the following:
- Reads
pyproject.tomland finds[build-system]. - Creates a temporary virtual environment. This is called build isolation.
- Installs the
requirespackages into that environment. - Calls backend hooks inside that environment, usually through a small subprocess.
- Collects the files the backend wrote and puts them in
dist/.
Note the order. By default build makes the sdist first, unpacks it into a temporary directory, and builds the wheel from that unpacked copy. This catches a common bug: a file that exists in your working tree but is missing from the sdist. If the wheel build needs that file, it now fails on your machine instead of on a user's.
The hooks the backend must provide
The PEP 517 interface is a handful of functions. Two are mandatory:
| Hook | Purpose | Returns |
|---|---|---|
| `build_wheel(wheel_directory, config_settings=None, metadata_directory=None)` | Build a wheel into the given directory | The wheel's file name |
| `build_sdist(sdist_directory, config_settings=None)` | Build a source distribution | The sdist's file name |
Several are optional:
| Hook | Purpose |
|---|---|
| `get_requires_for_build_wheel` | Extra packages needed only for this build, beyond `requires` |
| `get_requires_for_build_sdist` | The same, for the sdist |
| `prepare_metadata_for_build_wheel` | Produce only the `.dist-info` metadata, without compiling anything |
| `build_editable` | Build a wheel for editable installs (PEP 660) |
The metadata-only hook is why pip can often work out a package's dependencies without a full build. The get_requires_* hooks let a backend defer choosing build dependencies until it has looked at the project.
Calling a hook yourself
The hooks are ordinary Python functions, which means you can run one directly. This script, run from the project root with setuptools installed, does what the frontend does in step 4:
import tempfile
from setuptools import build_meta
with tempfile.TemporaryDirectory() as out_dir:
name = build_meta.build_wheel(out_dir)
print("backend produced:", name)The wheel is built in a temporary directory and deleted when the block ends, but the backend may leave build/ and *.egg-info directories in your project. This is why frontends use isolation and temporary directories. You would not normally call hooks by hand, but doing it once shows there is no hidden step: the frontend is orchestration, and the backend writes the files.
Running the build and controlling it
python -m pip install build
python -m build # sdist, then wheel from the sdist
python -m build --sdist # sdist only
python -m build --wheel # wheel only, built from the source tree
python -m build --outdir out/ # write somewhere other than dist/
python -m build --no-isolation # use the current environmentTwo details matter here. First, --wheel on its own builds directly from the source tree, skipping the sdist round trip, so it no longer checks that the sdist is complete. Second, --no-isolation skips the temporary environment and uses whatever is installed. It is faster and works offline, but your build can then pass because of a package that happens to be in your environment and is not listed in requires. Use it for distro packaging or CI where you control the environment, and not for your release build.
pip install . uses the same interface. pip is also a frontend: it calls build_wheel (or build_editable for pip install -e .), then installs the result. So a project that builds with python -m build will also install from source with pip.
Common errors
- No
[build-system]table. Frontends fall back to a legacy default ofsetuptoolswithsetuptools.build_meta:__legacy__andwheel. The build may work, but you have not pinned a backend version. Always write the table. BackendUnavailable: Cannot import 'setuptools.build_meta'. Therequireslist does not contain the backend, orbuild-backendis misspelled. Thebuild-backendpath must match the backend's documentation exactly, for examplehatchling.buildand nothatchling.- Missing build-time dependency. If your backend plugin (for example a version plugin) is not in
requires, isolation hides it and the build fails with an import error. Add it torequires. Do not fix this with--no-isolation. - Old backend and new metadata. An SPDX
licensestring orlicense-fileswith a backend that predates them gives errors or silently dropped fields. Raise the lower bound inrequires. - Running
python setup.py sdistorbdist_wheelinstead. Callingsetup.pydirectly is deprecated. It bypasses the isolation and the standard interface, and recent setuptools no longer supports some of its commands.
Choosing a backend
For a pure-Python library, the choice rarely changes what users see, because the artifacts follow the same standards. Setuptools is the long-standing default and the best supported for projects with C extensions. Hatchling and flit-core have smaller configuration surfaces and are popular for pure-Python code. Pick one, pin a minimum version in requires, and keep the metadata in [project], which all of them read in the same way. That keeps a later change of backend to a two-line edit.
Exercise
You run python -m build --wheel and it succeeds, but a teammate's python -m build fails with a FileNotFoundError for data/words.txt, which your code reads at build time. What is the most likely cause, and which build step exposed it?
Show answer
The file exists in your working tree but is not included in the sdist. With --wheel the build reads directly from the source tree, so it finds the file. A plain python -m build builds the sdist first, unpacks it, and builds the wheel from that copy, where the file is missing. The fix is to include the file in the sdist through the backend's configuration (for setuptools, a MANIFEST.in entry or package-data settings), then rebuild without --wheel.