making one pyd for a set of files with cython

2019-05-15 17:02发布

问题:

I have multiple .py files in one package

packageA
    \__init__.py
    \mod1.py
    \mod2.py
    \mod3.py

can I config cython to compile and then packing them all in one packageA.pyd ?

回答1:

Personally, I would better turn all the .py files into .pyx, then include them into the main .pyx of the Cython extension:

packageA.pyx:

include "mod1.pyx" 
include "mod2.pyx" 
include "mod3.pyx" 

Then, compile using a setup.py looking like:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

setup(
    cmdclass = {'build_ext': build_ext},
    ext_modules = [
        Extension("packageA", sources=["packageA.pyx"])
    ]
)                       

Running this would generate an all in one packageA.pyd binary file. Of course, this will output a single module named packageA, and I don't know if this is acceptable for you, or if you really need distinct modules in your package. But there might be other ways that better fit your question...



标签: cython