Great question! While setuptools
and the build
module provide the basic functionality for creating distribution artifacts (sdist and wheel), more modern build backends like hatchling
(used by Hatch) and flit-core
(used by Flit) offer several advantages, including better user experience, enhanced features, and improved performance. Here’s a breakdown of their added value:
pyproject.toml
-centric: Both hatchling
and flit
rely almost entirely on pyproject.toml
, reducing or eliminating the need for setup.py
or setup.cfg
.
Less boilerplate: They require fewer configurations for common cases (e.g., automatic package discovery, version management).
Dynamic versioning: Easily inject versions from git tags
or other sources without manual updates.
Version management: Supports dynamic versioning (e.g., pulling from git
tags).
Environment management: Comes with isolated build environments by default.
Plugin system: Extensible with plugins for docs generation, publishing, etc.
Metadata hooks: Automatically inject build-time metadata (like dates, git hashes).
Build reproducibility: Better control over dependencies and build isolation.
Simplicity: Designed for pure-Python packages with minimal configuration.
Publishing integration: Built-in flit publish
command for uploading to PyPI.
Automatic docstring extraction: Can generate long_description
from __doc__
.
Faster builds: hatchling
and flit
are generally faster than setuptools
for simple projects.
Better dependency handling: More precise control over build-time vs. runtime dependencies.
No setup.py
required: Fully declarative builds (no need for imperative scripts).
Source transformations: Yes! Both can dynamically modify files (e.g., inject version numbers).
Conditional builds: Easily handle platform-specific or feature-dependent builds.
Custom build hooks: Run scripts before/after building (e.g., generate docs, compile assets).
Hatch: Includes built-in test runners, coverage, and linting integration.
Flit: Simpler but integrates well with external CI/CD tools.
Neither directly provides CI hosting (like GitHub Actions or Travis CI), but they make CI easier by:
Reducing configuration complexity.
Supporting deterministic builds.
Enabling dynamic versioning in CI.
Auto-generated docs: Some backends (like Hatch) can integrate with Sphinx or MkDocs.
Better handling of extras (optional-dependencies
): More intuitive syntax than setuptools
.
setuptools
?You need legacy compatibility (e.g., setup.py
-based workflows).
You have complex C extensions (though hatchling
is catching up here).
You rely on very specific setuptools
plugins.
Use hatchling
if you want a modern, feature-rich backend with plugins.
Use flit
if you want the simplest setup for pure-Python packages.
Stick with setuptools
only if you need legacy support or complex builds.
Would you like a deeper dive into any of these features?