我对 cython 相当陌生,有谁知道如何通过 Cython 编译 python 项目(开销相对较低),因为我不断收到以下导入错误:
ImportError:没有名为 CythonRelated.testSource.MyClassObject 的模块
我的测试项目结构是这样的:
CythonRelated/
setup.py
testSource/
MainCythonTest.py
MyClassObject.py
MainCythonTest.pyx
MyClassObject.pyx
MainCythonTest 从 MyClassObject 模块导入类(通过
from CythonRelated.testSource.MyClassObject import myCustomObj
),初始化一个对象并调用一个对象方法。
我的 setup.py 看起来像这样:
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"]
)
我错过了什么?
将 setup.py 放在 CythonRelated 之外(显然在 cythonize 中更新 *.pyx 文件的相应路径)也无济于事
我的类对象.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()