How To Import Depending Cython Modules From Parent Folder In Python
Solution 1:
"include_path
" is for C includes. It has no influence on where Python looks for pxd files.
You usually want one setup file, at the top level. A better way of doing it is probably:
setup.py
project/
run.py
__init__.pxd
Settings/
__init__.pxd
Settings.pyx/pxd
Helper/
__init__.pxd
Helper.pyx/pxd
This will create a single module called "package" which will all be built at once and installed at once. Once installed your user would be able to do from project import whatever
.
You'll notice I've added some __init__.pxd
files. These serve basically the same purpose as __init__.py
files, except they identify folders as (sub)packages for Cython's cimport mechanism.
Helper .pxd then starts:
from project.Settings.Settings cimport Settings, PySettings
I've used a full rather than relative import because cimport
and relative imports seems a bit unreliable.
Setup.py is as follows:
from setuptools.extensionimportExtensionfrom setuptools import setup
fromCython.Buildimport cythonize
ext_modules = cythonize("project/*/*.pyx")
setup(ext_modules=ext_modules)
You are welcome to explicitly use Extension
but for something simple it seemed easier to send the file pattern directly to cythonize.
I've only tested the Cythonize stage of this since I don't have the C++ files needed to compile and run it.
Post a Comment for "How To Import Depending Cython Modules From Parent Folder In Python"