几个.pyx的Cython setup.py

时间:2014-02-17 10:11:12

标签: python compilation installation cython setup.py

我想更快地进行cythonize。一个.pyx的代码是

from distutils.core import setup
from Cython.Build import cythonize

setup(
    ext_modules = cythonize("MyFile.pyx")
)

如果我想要cythonize

怎么办?
  • 使用ext .pyx的几个文件,我将按名称调用

  • 文件夹中的所有.pyx文件

两种情况下setup.py的python代码是什么?

2 个答案:

答案 0 :(得分:21)

来自:https://github.com/cython/cython/wiki/enhancements-distutils_preprocessing

# several files with ext .pyx, that i will call by their name
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules=[
    Extension("primes",       ["primes.pyx"]),
    Extension("spam",         ["spam.pyx"]),
    ...
]

setup(
  name = 'MyProject',
  cmdclass = {'build_ext': build_ext},
  ext_modules = ext_modules,
)


# all .pyx files in a folder
from distutils.core import setup
from Cython.Build import cythonize

setup(
  name = 'MyProject',
  ext_modules = cythonize(["*.pyx"]),
)

答案 1 :(得分:13)

上面的答案对我来说并不完全清楚。要对不同目录中的两个文件进行cython化,只需在cythonize(...)函数中列出它们:

from distutils.core import setup
from Cython.Build import cythonize

setup(
    ext_modules = cythonize(["folder1/file1.pyx", "folder2/file2.pyx"])
)