在Windows 8上为Python 2.7构建多个pyx文件的cython

1 投票
1 回答
595 浏览
提问于 2025-04-18 17:54

我用 distutils 来构建:

python setup.py build_ext --inplace

构建一个简单的 pyx 文件是可以的(setup.py):

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

setup(
    ext_modules = cythonize('test.pyx')
)

构建多个文件(setup.py):

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

# This is the new part...
extensions = [
    Extension('test', ['test.pyx', 'test2.pyx'])
]

setup(
    ext_modules = cythonize(extensions)
)

test2.pyx:

def say_hello_to2(name):
    print("Hello %s!" % name)

上面的构建过程运行得很好,我看到编译和链接都成功完成了,但似乎在生成的二进制文件中没有找到方法 say_hello_to2。启动 Python 后,运行下面的代码显示模块中只有 test.pyx 的方法:

>>> import test
>>> dir(test)
['InheritedClass', 'TestClass', '__builtins__', '__doc__', '__file__', '__name__
', '__package__', '__test__', 'fib', 'fib_no_type', 'primes', 'say_hello_to', 's
in']
>>>

是否可以将多个 pyx 文件添加到扩展构建中?

1 个回答

1

你可以传递多个扩展名,比如:

extensions = [Extension('test', ['test.pyx']),
              Extension('test2', ['test2.pyx'])]

撰写回答