Choosing a Build Backend for a Python Library
Since PEP 517 and PEP 518, the tool that turns your source tree into a wheel and an sdist is pluggable. You name it in the [build-system] table of pyproject.toml, and installers such as pip call it for you. For a pure-Python package the main candidates all produce standard artefacts; they differ in how much configuration they want and what they do by default.
The options side by side
| setuptools | Hatchling | Flit (flit-core) | poetry-core | |
|---|---|---|---|---|
PEP 621 [project] metadata | Yes (recent versions) | Yes | Yes | Yes in Poetry 2; older versions used [tool.poetry] |
| C extensions | Yes, mature support | Via plugins | No, pure Python only | Limited |
| File selection default | Package discovery rules plus MANIFEST.in for sdists | VCS-aware, respects .gitignore | The single package or module it finds | Package discovery from config |
| Dynamic version from VCS tags | With setuptools-scm | With hatch-vcs | Reads __version__ | Through plugins |
| Typical fit | Anything, especially compiled code | Modern pure-Python projects | Simple single-package libraries | Projects already managed by Poetry |
Feature support moves between releases, so confirm details in each project's own documentation before relying on a row.
What the configuration looks like
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "package-a"
version = "0.4.0"
requires-python = ">=3.9"
dependencies = ["attrs>=23"]
Swapping backends for a pure-Python package with PEP 621 metadata usually means changing only the [build-system] table and moving any backend-specific settings. The [project] table stays the same, which is the main practical benefit of the standard.
How to read the comparison
Compiled code decides it quickly. If you ship C, C++ or Cython extensions, setuptools is the long-established path, and newer options such as scikit-build-core or meson-python exist for CMake and Meson builds. Flit is out of scope for that case by design.
For pure Python, pick on defaults you can live with. The most common release bug is not the backend's fault but its file selection: a data file missing from the wheel or test fixtures leaking into the sdist. Whatever you choose, unpack the built artefacts and look, as described in testing the built package before publishing.
Do not choose a backend to get a workflow. Environment management, lockfiles and publishing commands belong to front-end tools. You can build with Hatchling and publish with twine or a CI workflow using PyPI Trusted Publishing; the trusted publishing setup works the same for every backend.
A short decision list
- Compiled extensions? Use setuptools or a CMake/Meson-based backend.
- One pure-Python package, no extras? Flit-core keeps config minimal.
- Pure Python with data files, VCS versioning or plugins? Hatchling or setuptools.
- Team already standardised on Poetry? poetry-core avoids a second toolchain.
The Python Packaging User Guide at packaging.python.org has a neutral tutorial that shows each of these side by side, and the src layout guide covers the directory structure that every backend handles well.