12

To build a python project managed with poetry I need to build C extensions first (an equivalent to python setup.py build). poetry is able to do this according to this github issue. But to me it's not clear what to include into pyproject.toml that the C extension build is executed when building with poetry build?

2
  • 1
    According to the link in the question, add build = 'build.py' into the [tool.poetry] of pyproject.toml. Edit the question to show the contents of build.py and pyproject.toml.
    – sinoroc
    Feb 5, 2020 at 11:21
  • 2
    The accepted answer is correct (+/- some modifications), so no need for a new one. However, I find examples useful, too, so started a repo for minimal C/C++ extensions built with poetry. Maybe it is useful in getting started :) github.com/FirefoxMetzger/mini-extension Jul 30, 2022 at 18:28

1 Answer 1

8

Add build.py to the repo-root. E.g. if one has one header file directory and 2 source files:

from distutils.command.build_ext import build_ext


ext_modules = [
    Extension("<module-path-imported-into-python>",
              include_dirs=["<header-file-directory>"],
              sources=["<source-file-0>", "<source-file-1>"],
             ),
]


class BuildFailed(Exception):
    pass


class ExtBuilder(build_ext):

    def run(self):
        try:
            build_ext.run(self)
        except (DistutilsPlatformError, FileNotFoundError):
            raise BuildFailed('File not found. Could not compile C extension.')

    def build_extension(self, ext):
        try:
            build_ext.build_extension(self, ext)
        except (CCompilerError, DistutilsExecError, DistutilsPlatformError, ValueError):
            raise BuildFailed('Could not compile C extension.')


def build(setup_kwargs):
    """
    This function is mandatory in order to build the extensions.
    """
    setup_kwargs.update(
        {"ext_modules": ext_modules, "cmdclass": {"build_ext": ExtBuilder}}
    )

Add to pyproject.toml:

[tool.poetry]
build = "build.py"

To build the extension execute poetry build.

For an example refer to this PR.

4
  • 1
    I would like to note, that build.py is an unstable feature, that might be replaced by a more sophisticated build system for C-extensions: github.com/python-poetry/poetry/issues/2740
    – Padix Key
    Nov 25, 2020 at 8:37
  • 2
    It's missing an import statement for the errors it supposedly should raise in case extension building fails from distutils.errors import DistutilsPlatformError, CCompilerError, DistutilsExecError, DistutilsPlatformError
    – avaj_wen12
    Feb 15, 2022 at 14:14
  • Note that the pyproject.toml preferred syntax changed several years ago. Use this now: [tool.poetry.build] script = "build.py" generate-setup-file = true Aug 3 at 20:05
  • 1
    Also PSA: distutils is unmaintained and deprecated. Import everything from setuptools now instead: from setuptools.command import build_ext Aug 3 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.