| 1 |
################ |
| 2 |
Build Backends |
| 3 |
################ |
| 4 |
|
| 5 |
This document explains what build backends are, how they work with build, and how to choose the right one for your |
| 6 |
project. |
| 7 |
|
| 8 |
************************** |
| 9 |
What is a Build Backend? |
| 10 |
************************** |
| 11 |
|
| 12 |
A **build backend** is a Python package that knows how to build your project into `distributable formats |
| 13 |
<https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_ (`source distributions |
| 14 |
<https://packaging.python.org/en/latest/specifications/source-distribution-format/>`_ and `wheels |
| 15 |
<https://packaging.python.org/en/latest/specifications/binary-distribution-format/>`_). The backend implements a |
| 16 |
standardized interface defined in :PEP:`517`, which build (the **frontend**) uses to create your package distributions. |
| 17 |
|
| 18 |
When you run ``python -m build``, the frontend (build) reads your `pyproject.toml |
| 19 |
<https://packaging.python.org/en/latest/specifications/pyproject-toml/>`_ to determine which backend to use, then |
| 20 |
invokes that backend to perform the actual building. The separation between frontend and backend allows different |
| 21 |
projects to use different build systems (like `setuptools <https://setuptools.pypa.io/>`_, `hatchling |
| 22 |
<https://hatch.pypa.io/latest/>`_, or `flit <https://flit.pypa.io/>`_) while all working with the same frontend tool. |
| 23 |
|
| 24 |
****************************** |
| 25 |
How Backends Work with build |
| 26 |
****************************** |
| 27 |
|
| 28 |
The relationship between build and backends follows a clear contract: |
| 29 |
|
| 30 |
.. mermaid:: |
| 31 |
|
| 32 |
%%{init: {'theme':'base', 'themeVariables': { 'primaryColor':'#4051b5','primaryTextColor':'#fff','primaryBorderColor':'#2c3e8f','lineColor':'#5468c4','secondaryColor':'#7c8fd6','tertiaryColor':'#e8eaf6'}}}%% |
| 33 |
flowchart TD |
| 34 |
subgraph Frontend["Build Frontend (build)"] |
| 35 |
A[CLI Interface] |
| 36 |
B[Read pyproject.toml] |
| 37 |
C[Create Isolation] |
| 38 |
D[Invoke Hooks] |
| 39 |
end |
| 40 |
|
| 41 |
subgraph Backend["Build Backend (setuptools/hatchling/flit/...)"] |
| 42 |
E[Package Discovery] |
| 43 |
F[File Inclusion] |
| 44 |
G[Metadata Generation] |
| 45 |
H[Create Distributions] |
| 46 |
end |
| 47 |
|
| 48 |
A --> B |
| 49 |
B --> C |
| 50 |
C --> D |
| 51 |
D -->|PEP 517 Hooks| E |
| 52 |
E --> F |
| 53 |
F --> G |
| 54 |
G --> H |
| 55 |
H -->|sdist & wheel| I[dist/] |
| 56 |
|
| 57 |
style Frontend fill:#e8eaf6,stroke:#4051b5,color:#000 |
| 58 |
style Backend fill:#fff3e0,stroke:#f57c00,color:#000 |
| 59 |
style I fill:#2e7d32,stroke:#1b5e20,color:#fff |
| 60 |
|
| 61 |
Your ``pyproject.toml`` specifies which backend to use in the ``[build-system]`` section. For example, using hatchling |
| 62 |
as the backend looks like this: |
| 63 |
|
| 64 |
.. code-block:: toml |
| 65 |
|
| 66 |
[build-system] |
| 67 |
requires = ["hatchling"] |
| 68 |
build-backend = "hatchling.build" |
| 69 |
|
| 70 |
The ``requires`` field lists the packages needed to perform a build, which build installs in an isolated environment. |
| 71 |
The ``build-backend`` field specifies the Python import path to the backend object that implements the PEP 517 |
| 72 |
interface. |
| 73 |
|
| 74 |
When building, build creates an isolated environment, installs the required packages, imports the backend module, and |
| 75 |
calls standardized hook functions like ``build_wheel()`` or ``build_sdist()``. The backend then creates the distribution |
| 76 |
files and returns their filenames. |
| 77 |
|
| 78 |
************************** |
| 79 |
Available Build Backends |
| 80 |
************************** |
| 81 |
|
| 82 |
Several build backends are available, each with different features and philosophies. |
| 83 |
|
| 84 |
setuptools |
| 85 |
========== |
| 86 |
|
| 87 |
setuptools is the traditional and most widely used build backend. It has extensive features and backwards compatibility |
| 88 |
with older packaging standards, making it suitable for complex projects and projects that need to maintain compatibility |
| 89 |
with older tooling. |
| 90 |
|
| 91 |
To use setuptools, configure your ``pyproject.toml`` like this: |
| 92 |
|
| 93 |
.. code-block:: toml |
| 94 |
|
| 95 |
[build-system] |
| 96 |
requires = ["setuptools>=61.0", "wheel"] |
| 97 |
build-backend = "setuptools.build_meta" |
| 98 |
|
| 99 |
setuptools excels at handling complex build requirements including C extensions, namespace packages, and entry points. |
| 100 |
However, it can be complex to configure correctly and has many legacy features that may be confusing. |
| 101 |
|
| 102 |
See the `setuptools documentation <https://setuptools.pypa.io/>`_ for details. |
| 103 |
|
| 104 |
hatchling |
| 105 |
========= |
| 106 |
|
| 107 |
hatchling is the build backend from the Hatch project. It provides a modern, user-friendly interface with sensible |
| 108 |
defaults and good performance. |
| 109 |
|
| 110 |
To use hatchling: |
| 111 |
|
| 112 |
.. code-block:: toml |
| 113 |
|
| 114 |
[build-system] |
| 115 |
requires = ["hatchling"] |
| 116 |
build-backend = "hatchling.build" |
| 117 |
|
| 118 |
hatchling works well for pure Python packages and has excellent plugin support for extending functionality. It |
| 119 |
emphasizes convention over configuration and includes built-in support for common patterns like src-layout and editable |
| 120 |
installs. The main limitation is that it's newer and may have less community documentation than setuptools. |
| 121 |
|
| 122 |
See the `hatchling documentation <https://hatch.pypa.io/latest/config/build/>`_ for details. |
| 123 |
|
| 124 |
flit-core |
| 125 |
========= |
| 126 |
|
| 127 |
flit-core is a minimalist build backend that focuses on simplicity for pure Python packages. |
| 128 |
|
| 129 |
To use flit-core: |
| 130 |
|
| 131 |
.. code-block:: toml |
| 132 |
|
| 133 |
[build-system] |
| 134 |
requires = ["flit-core>=3.2"] |
| 135 |
build-backend = "flit_core.buildapi" |
| 136 |
|
| 137 |
flit-core works best for simple, pure Python packages without complex build requirements. It has minimal configuration |
| 138 |
and fast build times. By default, it includes all files tracked by git in the source distribution. The trade-off is |
| 139 |
limited support for complex scenarios like C extensions or non-standard package layouts. |
| 140 |
|
| 141 |
See the `flit documentation <https://flit.pypa.io/>`_ for details. |
| 142 |
|
| 143 |
pdm-backend |
| 144 |
=========== |
| 145 |
|
| 146 |
pdm-backend is the build backend from the PDM project, offering modern features and good integration with PEP 621 |
| 147 |
metadata. |
| 148 |
|
| 149 |
To use pdm-backend: |
| 150 |
|
| 151 |
.. code-block:: toml |
| 152 |
|
| 153 |
[build-system] |
| 154 |
requires = ["pdm-backend"] |
| 155 |
build-backend = "pdm.backend" |
| 156 |
|
| 157 |
pdm-backend provides strong PEP 621 support, good handling of dynamic metadata, and support for both pure Python and |
| 158 |
extension modules. It's actively developed and includes features like SCM versioning and custom build hooks. |
| 159 |
|
| 160 |
See the `pdm-backend documentation <https://pdm-backend.fming.dev/>`_ for details. |
| 161 |
|
| 162 |
poetry-core |
| 163 |
=========== |
| 164 |
|
| 165 |
poetry-core is the build backend extracted from Poetry. It's designed primarily for use with the Poetry tool but can be |
| 166 |
used standalone. |
| 167 |
|
| 168 |
To use poetry-core: |
| 169 |
|
| 170 |
.. code-block:: toml |
| 171 |
|
| 172 |
[build-system] |
| 173 |
requires = ["poetry-core"] |
| 174 |
build-backend = "poetry.core.masonry.api" |
| 175 |
|
| 176 |
poetry-core handles pure Python packages well and integrates tightly with Poetry's ecosystem. However, configuration is |
| 177 |
done through Poetry's custom ``[tool.poetry]`` section rather than standard ``[project]`` metadata, which can reduce |
| 178 |
interoperability with other tools. |
| 179 |
|
| 180 |
See the `poetry documentation <https://python-poetry.org/>`_ for details. |
| 181 |
|
| 182 |
scikit-build-core |
| 183 |
================= |
| 184 |
|
| 185 |
scikit-build-core is a build backend specifically designed for projects with CMake-based C/C++ extensions. |
| 186 |
|
| 187 |
To use scikit-build-core: |
| 188 |
|
| 189 |
.. code-block:: toml |
| 190 |
|
| 191 |
[build-system] |
| 192 |
requires = ["scikit-build-core"] |
| 193 |
build-backend = "scikit_build_core.build" |
| 194 |
|
| 195 |
scikit-build-core provides excellent CMake integration, cross-platform compilation support, and modern PEP 621 metadata |
| 196 |
support. It's the recommended backend for projects using CMake for extension modules, replacing the older scikit-build. |
| 197 |
|
| 198 |
See the `scikit-build-core documentation <https://scikit-build-core.readthedocs.io/>`_ for details. |
| 199 |
|
| 200 |
meson-python |
| 201 |
============ |
| 202 |
|
| 203 |
meson-python is a build backend for projects using the Meson build system, particularly useful for compiled extensions. |
| 204 |
|
| 205 |
To use meson-python: |
| 206 |
|
| 207 |
.. code-block:: toml |
| 208 |
|
| 209 |
[build-system] |
| 210 |
requires = ["meson-python"] |
| 211 |
build-backend = "mesonpy" |
| 212 |
|
| 213 |
meson-python offers fast builds, excellent cross-platform support, and good integration with Meson's dependency |
| 214 |
management. It's especially strong for projects with complex compiled components and is increasingly popular in the |
| 215 |
scientific Python ecosystem. |
| 216 |
|
| 217 |
See the `meson-python documentation <https://meson-python.readthedocs.io/>`_ for details. |
| 218 |
|
| 219 |
******************** |
| 220 |
Choosing a Backend |
| 221 |
******************** |
| 222 |
|
| 223 |
The right backend depends on your project's needs. For simple pure Python packages, flit-core or hatchling provide |
| 224 |
straightforward configuration and fast builds. Projects that need extensive customization or have complex requirements |
| 225 |
often benefit from setuptools' mature feature set and extensive documentation. |
| 226 |
|
| 227 |
If your project includes C or C++ extensions built with CMake, scikit-build-core is the natural choice. For projects |
| 228 |
using Meson for compilation, meson-python provides the best integration. Projects already using Poetry might prefer |
| 229 |
poetry-core for consistency, though this limits interoperability with other tools. |
| 230 |
|
| 231 |
New projects without special requirements should consider hatchling or flit-core for their modern approach and |
| 232 |
simplicity. Existing projects using setuptools can continue doing so, as it remains well-maintained and |
| 233 |
feature-complete. |
| 234 |
|
| 235 |
********************** |
| 236 |
Configuring Backends |
| 237 |
********************** |
| 238 |
|
| 239 |
Each backend has its own configuration approach. Modern backends use the ``[project]`` section of ``pyproject.toml`` for |
| 240 |
metadata following PEP 621. Backend-specific settings go in a ``[tool.backend-name]`` section. |
| 241 |
|
| 242 |
For example, with hatchling you might have: |
| 243 |
|
| 244 |
.. code-block:: toml |
| 245 |
|
| 246 |
[project] |
| 247 |
name = "mypackage" |
| 248 |
version = "1.0.0" |
| 249 |
|
| 250 |
[tool.hatch.build] |
| 251 |
include = ["src/**/*.py"] |
| 252 |
|
| 253 |
With setuptools: |
| 254 |
|
| 255 |
.. code-block:: toml |
| 256 |
|
| 257 |
[project] |
| 258 |
name = "mypackage" |
| 259 |
version = "1.0.0" |
| 260 |
|
| 261 |
[tool.setuptools.packages.find] |
| 262 |
where = ["src"] |
| 263 |
|
| 264 |
Always consult your backend's documentation for configuration options. Common configuration needs include specifying |
| 265 |
which files to include, where to find packages, how to handle version numbers, and how to process extension modules. |
| 266 |
|
| 267 |
***************************** |
| 268 |
Passing Options to Backends |
| 269 |
***************************** |
| 270 |
|
| 271 |
build allows passing configuration settings to backends via the ``--config-setting`` or ``-C`` flag. The syntax and |
| 272 |
available options depend entirely on the backend. |
| 273 |
|
| 274 |
For example, with setuptools you might pass build options: |
| 275 |
|
| 276 |
.. code-block:: console |
| 277 |
|
| 278 |
$ python -m build -C--build-option=--build-number=123 |
| 279 |
|
| 280 |
With scikit-build-core, you can configure CMake: |
| 281 |
|
| 282 |
.. code-block:: console |
| 283 |
|
| 284 |
$ python -m build -Ccmake.define.BUILD_TESTING=OFF |
| 285 |
|
| 286 |
Each backend interprets these settings differently. See :doc:`../how-to/config-settings` for backend-specific examples. |
| 287 |
|
| 288 |
Config settings map keys to values, so every key needs a value. Drop the ``=VALUE`` and the key gets an empty value, |
| 289 |
which suits backends that act on a flag's presence alone. build accepts this shorthand but warns about it, because pip |
| 290 |
only takes the explicit ``KEY=VALUE`` form. Write ``KEY=`` for an empty value and your command runs under both tools. |
| 291 |
|
| 292 |
****************** |
| 293 |
Dynamic Metadata |
| 294 |
****************** |
| 295 |
|
| 296 |
Some backends support generating metadata dynamically at build time. Common use cases include reading version numbers |
| 297 |
from git tags, generating file lists from directory contents, or computing dependencies based on environment. |
| 298 |
|
| 299 |
Most backends support version from SCM (source control management). For example, with hatchling using hatch-vcs: |
| 300 |
|
| 301 |
.. code-block:: toml |
| 302 |
|
| 303 |
[build-system] |
| 304 |
requires = ["hatchling", "hatch-vcs"] |
| 305 |
build-backend = "hatchling.build" |
| 306 |
|
| 307 |
[tool.hatch.version] |
| 308 |
source = "vcs" |
| 309 |
|
| 310 |
The backend reads version information from git tags during the build. This keeps version numbers in sync between git and |
| 311 |
your package without manual updates. |
| 312 |
|
| 313 |
Dynamic metadata is powerful but has trade-offs. It can make builds less reproducible if not carefully managed, and it |
| 314 |
may prevent installation from sdists if the dynamic information source (like git) isn't available. |
| 315 |
|
| 316 |
******************************** |
| 317 |
Backend Extensions and Plugins |
| 318 |
******************************** |
| 319 |
|
| 320 |
Many backends support plugins to extend functionality. setuptools has an extensive plugin ecosystem including plugins |
| 321 |
for namespace packages, entry points, and custom build steps. hatchling has a plugin system for custom build hooks, |
| 322 |
version sources, and metadata providers. |
| 323 |
|
| 324 |
When choosing a backend, consider whether you need extensibility through plugins. If your project has unique build |
| 325 |
requirements, a backend with a strong plugin system may be beneficial even if it's more complex to configure initially. |
| 326 |
|
| 327 |
******************** |
| 328 |
Switching Backends |
| 329 |
******************** |
| 330 |
|
| 331 |
Switching from one backend to another is possible but requires care. The main steps involve updating your |
| 332 |
``[build-system]`` section in ``pyproject.toml``, migrating configuration from the old backend's format to the new one, |
| 333 |
and testing that the built distributions are equivalent. |
| 334 |
|
| 335 |
When migrating, always build and inspect both sdist and wheel to verify all files are included correctly. Test |
| 336 |
installation from both distribution types. Check that metadata (version, dependencies, entry points) is correctly |
| 337 |
preserved. |
| 338 |
|
| 339 |
Most migrations involve moving from setuptools to a modern backend. The new backend's documentation typically includes |
| 340 |
migration guides. Common challenges include differences in how package discovery works, how data files are included, and |
| 341 |
how version numbers are specified. |
| 342 |
|
| 343 |
********************* |
| 344 |
Backend Development |
| 345 |
********************* |
| 346 |
|
| 347 |
For most users, using an existing backend is the right choice. However, if you have unique requirements not met by any |
| 348 |
existing backend, you can implement your own PEP 517 backend. |
| 349 |
|
| 350 |
A minimal backend must implement these functions in a Python module: |
| 351 |
|
| 352 |
.. code-block:: python |
| 353 |
|
| 354 |
def build_wheel(wheel_directory, config_settings=None, metadata_directory=None): |
| 355 |
# Build wheel, return filename |
| 356 |
pass |
| 357 |
|
| 358 |
|
| 359 |
def build_sdist(sdist_directory, config_settings=None): |
| 360 |
# Build sdist, return filename |
| 361 |
pass |
| 362 |
|
| 363 |
Implementing a full-featured backend requires handling metadata extraction, dependency specification, editable installs, |
| 364 |
and error handling. Unless you have very specialized needs, contributing to an existing backend or using its plugin |
| 365 |
system is usually more practical than creating a new one. |
| 366 |
|
| 367 |
*********************************************** |
| 368 |
Why setuptools commands don't work with build |
| 369 |
*********************************************** |
| 370 |
|
| 371 |
If you're migrating from ``setup.py``, you may be used to running commands like: |
| 372 |
|
| 373 |
.. code-block:: console |
| 374 |
|
| 375 |
$ python setup.py clean |
| 376 |
$ python setup.py test |
| 377 |
$ python setup.py develop |
| 378 |
|
| 379 |
**These commands do not work with build**: build uses the PEP 517 interface, which only defines hooks for building |
| 380 |
distributions (``build_wheel`` and ``build_sdist``). It does not support arbitrary setuptools commands. |
| 381 |
|
| 382 |
**Modern alternatives**: |
| 383 |
|
| 384 |
- **clean**: Remove the ``dist/`` directory manually or let your CI do it |
| 385 |
- **test**: Use ``pytest`` directly or via ``tox`` |
| 386 |
- **develop**: Use ``pip install --editable .`` for editable installs |
| 387 |
- **sdist/bdist_wheel**: Use ``python -m build`` |
| 388 |
|
| 389 |
If you need custom build steps, implement them in your build backend's hooks or use a plugin system. For example, |
| 390 |
setuptools supports ``setup.py`` with custom build steps that run during the PEP 517 build process. |
| 391 |
|
| 392 |
********** |
| 393 |
See also |
| 394 |
********** |
| 395 |
|
| 396 |
- :doc:`how-it-works` for how build invokes backends |
| 397 |
- :doc:`../how-to/config-settings` for backend-specific configuration examples |
| 398 |
- `PEP 517 <https://peps.python.org/pep-0517/>`_ for the backend interface specification |
| 399 |
- `PEP 621 <https://peps.python.org/pep-0621/>`_ for standard project metadata |