setup_requires 与 Cython?

2024-01-09

我正在创建一个setup.py具有一些 Cython 扩展模块的项目的文件。

我已经让它工作了:

from setuptools import setup, Extension
from Cython.Build import cythonize

setup(
    name=...,
    ...,
    ext_modules=cythonize([ ... ]),
)

这安装得很好。但是,这假设已安装 Cython。如果没有安装怎么办?我明白这就是setup_requires参数用于:

from setuptools import setup, Extension
from Cython.Build import cythonize

setup(
    name=...,
    ...,
    setup_requires=['Cython'],
    ...,
    ext_modules=cythonize([ ... ]),
)

但是,如果 Cython 尚未安装,这当然会失败:

$ python setup.py install
Traceback (most recent call last):
  File "setup.py", line 2, in <module>
    from Cython.Build import cythonize
ImportError: No module named Cython.Build

执行此操作的正确方法是什么?我需要以某种方式导入Cython仅在之后setup_requires步骤运行,但我需要Cython为了指定ext_modules values.


从...开始18.0 https://github.com/pypa/setuptools/blob/master/CHANGES.rst#180释放setuptools(发布于2015-06-23)可以指定Cython in setup_requires并通过*.pyx常规模块源setuptools.Extension:

from setuptools import setup, Extension

setup(
    # ...
    setup_requires=[
        # Setuptools 18.0 properly handles Cython extensions.
        'setuptools>=18.0',
        'cython',
    ],
    ext_modules=[
        Extension(
            'mylib',
            sources=['src/mylib.pyx'],
        ),
    ],
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

setup_requires 与 Cython? 的相关文章

随机推荐