Imagine the following folder structure:
/project
run.py
/Helper
Helper.pxd
Helper.pyx
setup.py
/Settings
Settings.pxd
Settings.pyx
setup.py
Helper uses the types Settings and PySettings defined in Settings.pxd. Therefor in Helper.pxd I'm doing:
from Settings cimport Settings, PySettings
In the setup.py in the Helper directory I put
include_path=[".", "../Settings/"]
in the cythonize command. Thus Helper knows about Settings and everything compiles.
In run.py I'd like to import Settings as well as Helper. The Settings import works fine but when doing
import Helper.Helper
I get the following error:
Traceback (most recent call last):
File "run.py", line 1, in <module>
import Helper.Helper
File "../Settings/Settings.pxd", line 6, in init Helper
AttributeError: module 'Settings' has no attribute 'PySettings'
This issue disappears as soon as everything is in the same directory.
For the sake of completeness you'll find the whole code below:
/project/run.py
import Settings.Settings as S
import Helper.Helper as H
settings = S.PySettings()
settings.doSomething()
H.PyMyFunction(settings)
/project/Helper/Helper.pxd
from Settings cimport Settings, PySettings
cdef extern from "../../cppCode/Helper/Helper.h":
void myFunction(Settings settings)
/project/Helper/Helper.pyx
#distutils: sources = [../../cppCode/Helper/Helper.cpp, ../../cppCode/Settings/Settings.cpp]
#distutils: language = c++
def PyMyFunction(PySettings settings):
myFunction(settings.c_settings)
/project/Helper/setup.py
from distutils.core import setup
from Cython.Build import cythonize
from setuptools.extension import Extension
extensions = [
Extension("Helper", ["Helper.pyx"])
]
setup(ext_modules=cythonize(extensions, include_path=[".", "../Settings/"]))
/project/Settings/Settings.pxd
cdef extern from "../../cppCode/Settings/Settings.h":
cdef cppclass Settings:
Settings() except +
void doSomething()
cdef class PySettings:
cdef Settings c_settings
/project/Settings/Settings.pyx
#distutils: sources = ../../cppCode/Settings/Settings.cpp
#distutils: language = c++
cdef class PySettings:
def __cinit__(self):
self.c_settings = Settings()
def doSomething(self):
self.c_settings.doSomething()
/project/Settings/setup.py
from distutils.core import setup
from Cython.Build import cythonize
from setuptools.extension import Extension
extensions = [
Extension("Settings", ["Settings.pyx"])
]
setup(ext_modules=cythonize(extensions))
See Question&Answers more detail:
os