TExcelTAD
Home › Python Packaging: pip, venv, Poetry & uv › Build Backends: setuptools vs hatchling

Build Backends: setuptools vs hatchling

The previous lesson showed that a backend is any module implementing PEP 517's hooks. In practice you choose among a handful. They all read the same [project] table and all produce standard wheels; they differ in how they decide which files go in, how they get the version, whether they can compile extensions, and how much legacy they carry.

Backend build-backend Strengths Watch out for
setuptools setuptools.build_meta the original; compiles C/C++ extensions; supports every old project (setup.py, setup.cfg) package auto-discovery rules, MANIFEST.in, *.egg-info folders, lots of output
hatchling hatchling.build modern defaults, quiet, plugins (version from VCS, build hooks), the default of hatch new no compiled extensions without a plugin
flit-core flit_core.buildapi minimal, zero dependencies, pure-Python only few knobs by design
poetry-core poetry.core.masonry.api what Poetry projects use; reads [tool.poetry] too Poetry-specific extras
uv_build uv_build very fast, strict, the default of uv init --lib pure Python only, expects the src/<name> layout
scikit-build-core, meson-python, maturin C/C++ (CMake), Meson, and Rust extensions

The same project, two backends

Here is one small src-layout project with a README and a tests/ folder, built once with setuptools and once with hatchling. Only [build-system] differs:

bash · try it yourself
python3 -m venv .venv && . .venv/bin/activate
pip install -q build
make_project() {
  mkdir -p "$1/src/tally" "$1/tests" && cd "$1"
  printf '[build-system]\nrequires = ["%s"]\nbuild-backend = "%s"\n\n' "$2" "$3" > pyproject.toml
  printf '[project]\nname = "tally"\nversion = "1.0.0"\nreadme = "README.md"\nrequires-python = ">=3.13"\n' >> pyproject.toml
  echo 'def count(xs): return len(xs)' > src/tally/__init__.py
  echo '# tally' > README.md
  echo 'def test_ok(): pass' > tests/test_tally.py
  python -m build -q 2>&1 | tail -1
  echo "wheel:";  unzip -Z1 dist/*.whl | sed 's/^/  /'
  echo "sdist:";  tar tzf dist/*.tar.gz | grep -v '/
  

 | sort | sed 's/^/  /'
  cd ..
}
echo "=== setuptools"; make_project st "setuptools>=77" setuptools.build_meta
echo "=== hatchling";  make_project ha hatchling hatchling.build
=== setuptools
Successfully built tally-1.0.0.tar.gz and tally-1.0.0-py3-none-any.whl
wheel:
  tally/__init__.py
  tally-1.0.0.dist-info/METADATA
  tally-1.0.0.dist-info/WHEEL
  tally-1.0.0.dist-info/top_level.txt
  tally-1.0.0.dist-info/RECORD
sdist:
  tally-1.0.0/PKG-INFO
  tally-1.0.0/README.md
  tally-1.0.0/pyproject.toml
  tally-1.0.0/setup.cfg
  tally-1.0.0/src/tally.egg-info/PKG-INFO
  tally-1.0.0/src/tally.egg-info/SOURCES.txt
  tally-1.0.0/src/tally.egg-info/dependency_links.txt
  tally-1.0.0/src/tally.egg-info/top_level.txt
  tally-1.0.0/src/tally/__init__.py
  tally-1.0.0/tests/test_tally.py
=== hatchling
Successfully built tally-1.0.0.tar.gz and tally-1.0.0-py3-none-any.whl
wheel:
  tally/__init__.py
  tally-1.0.0.dist-info/METADATA
  tally-1.0.0.dist-info/WHEEL
  tally-1.0.0.dist-info/RECORD
sdist:
  tally-1.0.0/PKG-INFO
  tally-1.0.0/README.md
  tally-1.0.0/pyproject.toml
  tally-1.0.0/src/tally/__init__.py
  tally-1.0.0/tests/test_tally.py

The wheels are nearly identical; setuptools adds top_level.txt, a leftover from the egg era. The sdists show the real difference in style: setuptools writes a setup.cfg and a whole tally.egg-info/ folder into the sdist, while hatchling ships the source tree and PKG-INFO and nothing else. Both include tests/ in the sdist (that's normal and useful: distributors run the tests) but not in the wheel.

Package discovery

A backend has to decide which directories are importable packages. Both handle the src layout (src/<name>/) without configuration, which is one reason to prefer it. With a flat layout (packages directly in the project root), setuptools guesses, and refuses when the guess is ambiguous:

bash · try it yourself
python3 -m venv .venv && . .venv/bin/activate
pip install -q build
mkdir -p flat/tally flat/helpers && cd flat
touch tally/__init__.py helpers/__init__.py
printf '[build-system]\nrequires = ["setuptools>=77"]\nbuild-backend = "setuptools.build_meta"\n\n[project]\nname = "tally"\nversion = "1.0.0"\n' > pyproject.toml
python -m build --wheel 2>&1 | grep -A3 'error:'
error: Multiple top-level packages discovered in a flat-layout: ['tally', 'helpers'].

To avoid accidental inclusion of unwanted files or directories,
setuptools will not proceed with this build.

The fix is to say what you mean:

toml
# setuptools
[tool.setuptools.packages.find]
include = ["tally*"]

# hatchling
[tool.hatch.build.targets.wheel]
packages = ["src/tally"]

Hatchling's default is simpler: it looks for a package named after the project (tally/ or src/tally/) and fails with a clear message if there isn't one.

Tip: Use the src layout. It prevents a subtle bug, too: with a flat layout, running pytest from the project root imports your source tree instead of the installed package, so tests pass even when the wheel is missing files.

Choosing

  • New pure-Python library or app: hatchling or uv_build (or whatever your project manager defaults to: Poetry uses poetry-core, uv uses uv_build). Any of them is fine; the choice is reversible because [project] is portable.
  • C/C++/Rust extensions: setuptools (simple C), scikit-build-core (CMake), meson-python, or maturin (Rust/PyO3).
  • An existing setuptools project: keep setuptools, but move metadata from setup.py into [project] and delete as much of setup.py as you can. A setup.py that only calls setup() can go entirely.

Test yourself

What changes in pyproject.toml when you switch a pure-Python project from setuptools to hatchling?

  1. The whole [project] table
  2. Mainly [build-system] (and any backend-specific [tool.*] config)
  3. Nothing, the backend is chosen by pip
  4. The dependencies

Why did setuptools refuse to build the flat-layout project?

  1. The version was missing
  2. It found two top-level packages (tally and helpers) and wouldn't guess which to ship
  3. hatchling was required
  4. Flat layouts are not supported at all

Which backend would you choose for a package with a Rust extension?

  1. flit-core
  2. hatchling
  3. maturin
  4. poetry-core