简体   繁体   中英

How to compile .pyx using cythonize inside setup.py

Given a directory structure:

projectroot/
    docs/
    project/
        __init__.py
        core/
            __init__.py
            another.pyx
            anotherone.pyx
            lib/
                __init__.py
                something.pyx
    tests/
        mytest.py
    setup.py

And setup.py containing the following:

from Cython.Build import cythonize

try:
    from setuptools import Extension, setup, find_packages
except ImportError:
    from distutils.core import Extension, setup, find_packages

if __name__ == '__main__':
    setup(
        name='myproject',
        version='1.0.0',
        packages=find_packages(),
        ext_modules=cythonize([
             Extension('*', ['project/core/lib/*.pyx']),
             Extension('*', ['project/core/*.pyx'])
        ])
    )

It compiles properly. The only problem is that when I open the python interpreter from the root directory , it raises an ImportError .

(test) [root@mico projectroot]# python
>>> from project.core.lib.another import AnotherClass
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named another

But when I open the interpreter inside any other directory, it works.

(test) [root@mico projectroot/project]# python
>>> from project.core.lib.another import AnotherClass
# no error

How do I fix this?

When you are in the root directory, python tries to import from the local path first, ie it goes looking inside project/core.

I think you probably installed the package globally (in /usr/lib/pythonx.y/dist-packages ), so it will work everywhere except in the root directory because the .so will not be present in project/core .

With any other current directory, python will go get the .so from its global folder in /usr/lib/pythonx.y/dist-packages , where you installed it. To use it locally you need to do for instance python setup.py build_ext --inplace , or pip install -e . in the root directory as Nils Werner mentioned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM