Building extension modules

Building Extension Modules #

Warning

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.

  1. 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.

    Note
    You must assume that only Python built-ins are available by default in a build environment. This means, if you need even packages like setuptools, it must be explicitly declared.

    The necessary build dependencies must be added to the build-system.requires section of your pyproject.toml file.

    [build-system]
    requires = ["poetry-core", "setuptools", "cython"]
    build-backend = "poetry.core.masonry.api"
    
    Note
    It is recommended that you consider specifying version constraints to all entries in build-system.requires in order to avoid surprises if one of the packages introduce a breaking change. For example, you can set cython to cython>=3.0.11,<4.0.0 to ensure no major version upgrades are used.
    Note

    If 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
    
  2. 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.

    Note
    The 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 your pyproject.toml file.
    [tool.poetry.build]
    script = "relative/path/to/build-extension.py"
    
    Note
    The name of the build script is arbitrary. Common practice has been to name it build.py, however this is not mandatory. You should consider using a subdirectory if feasible.
  3. Specify Distribution Files

    Warning
    The 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.

    1. Include your build outputs in your wheel.
    2. Exclude your build inputs from your wheel.
    3. 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.

  1. When poetry install is run, it is executed prior to installing the project’s root package.
  2. When poetry build is run, it is executed prior to building distributions.
  3. 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"