我正在为带有一些 Cython 扩展模块的项目创建一个 setup.py
文件。
我已经让这个工作了:
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
这样做的正确方法是什么?我需要在 setup_requires
步骤运行后以某种方式导入 Cython
,但我需要 Cython
才能指定 ext_modules
值(value)观。
最佳答案
从 18.0 开始setuptools
发布(2015-06-23 发布)可以在 setup_requires
中指定 Cython
并通过 *.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'],
),
],
)
https://stackoverflow.com/questions/37471313/