Reputation: 278
Following these examples (https://github.com/cython/cython/wiki/PackageHierarchy , https://groups.google.com/forum/#!msg/cython-users/6trL0V1bLx4/7bxhj0xCK50J and Cython: ImportError: No module named 'myModule': how to call a cython module containing a cimport to another cython nodule?), I have created the following cython package setup:
test_outside.py
cython_pckg/
__init__.py
setup.py
test_inside.py
Code/
__init__.py
worker/
__init__.py
worker_1.pyx
worker_1.pxd
worker_2.pyx
worker_2.pxd
worker_3.pyx
worker_3.pxd
My setup.py
file looks as follows:
from distutils.core import setup
from distutils.extension import Extension
import numpy
from Cython.Distutils import build_ext
ext_modules = [
Extension("Code.worker.worker_1", ["Code/worker/worker_1.pyx"], include_dirs=[".", numpy.get_include()]),
Extension("Code.worker.worker_2", ["Code/worker/worker_2.pyx"], include_dirs=["."]),
Extension("Code.worker.worker_3", ["Code/worker/worker_3.pyx"], include_dirs=[".","./Code/worker/", numpy.get_include()])
]
setup(name="C_Extensions",
cmdclass={"build_ext": build_ext},
ext_modules=ext_modules,
script_args=["build_ext"],
options={'build_ext':{'inplace':True, 'force':True}}
)
Please note, that worker_3
imports worker_2
. If I try to import these modules in test_inside.py
, everything works fine. However, importing the modules into test_outside.py
throws an ImportError: No module named Code.worker.worker_2
for the file worker_3.pyx
. On the top level __init__.py
, I import everything.
from Code.worker.worker_1 import *
from Code.worker.worker_2 import *
from Code.worker.worker_3 import *
How can I make this work?
Upvotes: 2
Views: 4049
Reputation: 5270
In worker_3.pyx
:
cimport worker_2
Note - not cimport Code.<..>
Cython only looks at available .pxd
files when cimport
is used.
It has no knowledge of module level name spacing, meaning the module name defined in setup.py's Extension
.
Reference - Cython docs.
Upvotes: 1