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

setuptoolsHatchlingFlit (flit-core)poetry-core
PEP 621 [project] metadataYes (recent versions)YesYesYes in Poetry 2; older versions used [tool.poetry]
C extensionsYes, mature supportVia pluginsNo, pure Python onlyLimited
File selection defaultPackage discovery rules plus MANIFEST.in for sdistsVCS-aware, respects .gitignoreThe single package or module it findsPackage discovery from config
Dynamic version from VCS tagsWith setuptools-scmWith hatch-vcsReads __version__Through plugins
Typical fitAnything, especially compiled codeModern pure-Python projectsSimple single-package librariesProjects 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

  1. Compiled extensions? Use setuptools or a CMake/Meson-based backend.
  2. One pure-Python package, no extras? Flit-core keeps config minimal.
  3. Pure Python with data files, VCS versioning or plugins? Hatchling or setuptools.
  4. 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.