Sub topic 3
Build frontends and backends: what python -m build actually does
This chapter assumes you can read a basic pyproject.toml and have used pip in a virtual environment. It explains the two roles involved in turning source into installable files, and it covers the errors you are most likely to hit.
Two roles, one contract
Packaging tools split the work between two kinds of program.
- A build frontend is the tool you run. It reads
pyproject.toml, prepares an isolated environment, and asks a backend to do the work.python -m buildandpipare frontends. - A build backend is the library that builds the files. It is named in
[build-system]. Setuptools, Hatchling, Flit-core, and PDM-backend are backends.
The two communicate through a standard interface defined in PEP 517. Any frontend can drive any backend, so you can change backends without changing how you build.
[build-system]
requires = ["setuptools>=77"]
build-backend = "setuptools.build_meta"requires lists what must be installed before the build starts. build-backend names the Python object the frontend will call, in the form module.path.
What happens when you run it
python -m pip install build
python -m buildThe frontend does the following:
- Reads
[build-system]frompyproject.toml. - Creates a temporary virtual environment.
- Installs everything in
requiresinto that environment. - Imports the module named by
build-backendand calls its hooks. - Writes the results to
dist/.
The hooks are ordinary functions. The two that matter most here are build_sdist, which produces a source distribution, and build_wheel, which produces a wheel. By default python -m build builds the sdist first, then builds the wheel from that sdist. This catches files that exist in your working tree but are missing from the sdist.
Why isolation matters
The temporary environment contains only what requires lists. If your build quietly depends on a package that happens to be installed on your machine, the isolated build fails. That failure is useful, because it shows what a stranger's machine would experience.
You can select one artifact when you need to:
python -m build --sdist
python -m build --wheelAvoid --no-isolation unless you are packaging for a distribution that manages build dependencies itself. It brings back the "works on my machine" problem.
Common errors
| Symptom | Likely cause |
|---|---|
| `ModuleNotFoundError` during build | A package your build imports is missing from `requires` |
| `Cannot import 'setuptools.build_meta'` | A typo in `build-backend`, or a missing `requires` entry |
| Wheel is missing files that exist locally | The files were not included in the sdist, since the wheel is built from it |
| Build works with pip but not `build` | Different environments; check `requires` first |
Exercise
You run python -m build and it fails with ModuleNotFoundError: No module named 'wheel_helper'. The package is installed in your active virtual environment. Why does the build fail, and where do you fix it?
Show answer
The build runs in a temporary isolated environment that ignores your active environment. Only packages listed in requires under [build-system] are installed there. Add wheel_helper to requires.