【问题标题】:Mix cython and cmake extensions in python setuptools在 python setuptools 中混合 cython 和 cmake 扩展
【发布时间】:2021-03-17 17:29:48
【问题描述】:

我有一个带有以下 setup.py 的 python 包:

from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext


class CMakeExtension(Extension):
    # ...


class CMakeBuild(build_ext):
    # ...


extensions = [
    CMakeExtension("cmake_extension", "path-to-sources"),
    Extension("cython_extension", ["file.pyx"]),
]


setup(
    # ...
    ext_modules=extensions,
    # ...
)

我想知道我是否可以致电python setup.py build_ext --inplace 并使用适当的构建器构建每个扩展。

我知道 cmdclass 设置函数参数,但没有找到指定 build_ext 应用于 cython 扩展和 CMakeBuild 用于 cmake 的方法。

请注意,使用正确的构建器类(和 cmdclass 参数)可以正常构建每个扩展。

谢谢!

【问题讨论】:

    标签: python cmake setuptools


    【解决方案1】:

    最好的办法是确保CMakeBuild 检测到扩展类型,如果扩展不是CMakeExtension 的实例,则恢复为原始build_ext

    【讨论】:

      【解决方案2】:

      我找到的解决方法:

      from setuptools import setup, Extension
      from setuptools.command.build_ext import build_ext
      
      
      class CMakeExtension(Extension):
          # ...
      
      
      class CMakeBuild(build_ext):
          # ...
      
      
      if sys.argv[1] == "build_ext":
          c_ext = [Extension(...)]
      elif sys.argv[1] == "build_cmk":
          c_ext = [CMakeExtension(...)]
      else:
          raise NotImplementedError
      
      
      setup(
          # ...
          ext_modules=cythonize(c_ext),
          cmdclass={"build_ext": build_ext, "build_cmk": CMakeBuild},
          # ...
      )
      

      然后运行:

      python setup.py build_ext --inplace
      python setup.py build_cmk --inplace
      

      【讨论】:

        猜你喜欢
        • 2017-07-23
        • 2018-12-29
        • 1970-01-01
        • 2011-09-11
        • 2018-05-25
        • 2022-10-06
        • 1970-01-01
        • 1970-01-01
        • 2022-10-25
        相关资源
        最近更新 更多