Building extension modules
Building Extension Modules #
While this feature has been around since almost the beginning of the Poetry project and has needed minimal changes, it is still considered unstable. You can participate in the discussions about stabilizing this feature here.
And as always, your contributions towards the goal of improving this feature are also welcome.
Poetry allows a project developer to introduce support for, build and distribute native extensions within their project. In order to achieve this, at the highest level, the following steps are required.
-
Add Build Dependencies
The build dependencies, in this context, refer to those Python packages that are required in order to successfully execute your build script. Common examples include
cython
,meson
,maturin
,setuptools
etc., depending on how your extension is built.NoteYou must assume that only Python built-ins are available by default in a build environment. This means, if you need even packages likesetuptools
, it must be explicitly declared.The necessary build dependencies must be added to the
build-system.requires
section of yourpyproject.toml
file.[build-system] requires = ["poetry-core", "setuptools", "cython"] build-backend = "poetry.core.masonry.api"
NoteIt is recommended that you consider specifying version constraints to all entries inbuild-system.requires
in order to avoid surprises if one of the packages introduce a breaking change. For example, you can setcython
tocython>=3.0.11,<4.0.0
to ensure no major version upgrades are used.NoteIf you wish to develop the build script within your project’s virtual environment, then you must also add the dependencies to your project explicitly to a dependency group - the name of which is not important.
poetry add --group=build setuptools cython
-
Add Build Script
The build script can be a free-form Python script that uses any dependency specified in the previous step. This can be named as needed, but must be located within the project root directory (or a subdirectory) and also must be included in your source distribution. You can see the example snippets section for inspiration.
NoteThe build script is always executed from the project root. And it is expected to move files around to their destinations as expected by Poetry as per yourpyproject.toml
file.[tool.poetry.build] script = "relative/path/to/build-extension.py"
NoteThe name of the build script is arbitrary. Common practice has been to name itbuild.py
, however this is not mandatory. You should consider using a subdirectory if feasible. -
Specify Distribution Files
WarningThe following is an example, and should not be considered as complete.[tool.poetry] ... include = [ { path = "package/**/*.so", format = "wheel" }, # sources must be present in sdist, can be ignored if you only have *.pyx sources { path = "package/**/*.c", format = "sdist" }, ]
The key takeaway here should be the following. You can refer to the
pyproject.toml
documentation for information on each of the relevant sections.- Include your build outputs in your wheel.
- Exclude your build inputs from your wheel.
- Include your build inputs to your source distribution.
Example Snippets #
Cython #
[build-system]
requires = ["poetry-core", "cython", "setuptools"]
build-backend = "poetry.core.masonry.api"
[tool.poetry]
...
packages = [
{ include = "package", from = "src"},
]
include = [
{ path = "src/package/**/*.so", format = "wheel" },
]
# if not already excluded via .gitignore
exclude = [
"**/*.c"
]
[tool.poetry.build]
script = "scripts/build-extension.py"
Meson #
[tool.poetry.build]
script = "build-extension.py"
[build-system]
requires = ["poetry-core", "meson"]
build-backend = "poetry.core.masonry.api"
Maturin #
[tool.poetry.build]
script = "build-extension.py"
[build-system]
requires = ["poetry-core", "maturin"]
build-backend = "poetry.core.masonry.api"
FAQ #
When is my build script executed? #
If your project uses a build script, it is run implicitly in the following scenarios.
- When
poetry install
is run, it is executed prior to installing the project’s root package. - When
poetry build
is run, it is executed prior to building distributions. - When a PEP 517 build is triggered from source or sdist by another build frontend.
How does Poetry ensure my build script’s dependencies are met? #
Prior to executing the build script, Poetry creates a temporary virtual environment with your project’s active Python
version and then installs all dependencies specified under build-system.requires
into this environment. It should be
noted that no packages will be present in this environment at the time of creation.
Can I store the build script in a subdirectory? #
Yes you can. If storing the script in a subdirectory, your pyproject.toml
might look something like this.
[tool.poetry]
...
packages = [
{ include = "package", from = "src"}
]
include = [
{ path = "src/package/**/*.so", format = "wheel" },
]
# exclude any intermediate source files
exclude = [
"**/*.c"
]
[tool.poetry.build]
script = "scripts/build-extension.py"