i am fairly new to cython, does anyone know how to compile a python project via Cython (with relative low overhead), since I keep getting the following import error:
ImportError: No module named CythonRelated.testSource.MyClassObject
My test project structure is like this:
CythonRelated/
setup.py
testSource/
MainCythonTest.py
MyClassObject.py
MainCythonTest.pyx
MyClassObject.pyx
Where MainCythonTest imports class from MyClassObject module ( via
from CythonRelated.testSource.MyClassObject import myCustomObj
), initializes an object and calls an object method.
My setup.py looks like this:
from distutils.core import setup
from Cython.Build import cythonize
from distutils.extension import Extension
setup(
name = "My cython test app",
ext_modules = cythonize("testSource/*.pyx", include_path = ["."]),
packages = ["CythonRelated", "CythonRelated.testSource"]
)
What am I missing?
Taking setup.py outside CythonRelated (and obviously updating the corresponding path of *.pyx files in cythonize) didn't help either
MyClassObject.py
import sys
print sys.version_info
class myCustomObj():
def __init__(self, value):
self.value_ = value
def classInfo(self):
print "calling class {0} object with value of {1}".format(self.__class__.__name__,
self.value_)
MainCythonTest.py
import pyximport; pyximport.install()
from CythonRelated.testSource.MyClassObject import myCustomObj
def myFunc():
aObj = myCustomObj(12)
bObj = myCustomObj(21)
bObj.classInfo()
aObj.classInfo()
print "myFunc called"
myFunc()
You need to move the setup.py
file anyway, as it cannot be part of a project that it "compiles".
Then, the main issue is that you lack __init__.py
files in CythonRelated
and CythonRelated/testSource
. Without this file, the directory is not an importable Python module. With these two changes, I could pip install --user -e .
the package and also run the program MainCythonTest.py
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.