Python building cython extension with setup creates subfolder when __init__.py exists

a 夏天 提交于 2019-12-01 17:57:10
m00am

As mentioned in the comments, the setup.py should not live inside your package. As far as I know the build_ext commands has no option (apart from --inplace) to specify a target path. You can find some documentation here. Also this question deals with a similar topic.

To adapts the required package structure your package would have to look like:

c_ext/
    setup.py
    myfile.py
    verifier/
        __init__.py
        verifier_c.pyx

You will get an extension that lives in the verifier package:

me@machine:~/c_ext/$ python setup.py build_ext --inplace

c_ext/
    setup.py
    myfile.py
    verifier/
        __init__.py
        verifier_c.pyx
        verifier_c.so

You can then import verifier_c from the verifier package. For example from myfile.py this would look like:

from verifier import verifier_c
...

You can manage a separate package (and folder) for each Cython extension or create one sub folder that contains all of them. You have to pass the other modules to cythonize as well. It can handle a glob pattern, list of glob patterns or a list of Distutils.Extensions objects. The latter can be handy to specify cython compiler directives

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

extensions = [
    Extension("verifier_c", ["verifier/verifier_c.pyx"]),
    Extension("something_else", ["foobar/something_else.pyx"] compiler_directives={'embedsignature': True}),
    ]

setup(
    ext_modules=cythonize(extensions),
)

I hope this help :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!