5

要构建一个由我管理的 python 项目,poetry我需要先构建 C 扩展(相当于python setup.py build)。poetry能够根据这个github问题做到这一点。但对我来说,不清楚在pyproject.toml构建时执行 C 扩展构建时要包含什么内容poetry build

4

1 回答 1

4

添加build.py到回购根目录。例如,如果一个人有一个头文件目录和两个源文件:

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}}
    )

添加到pyproject.toml

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

要构建扩展,请执行poetry build.

有关示例,请参阅此 PR

于 2020-02-11T07:37:00.627 回答