我是相当新的用Cython,没有人知道如何编译通过用Cython Python项目(相对低开销),因为我不断收到下面的导入错误:Compling Python包通过用Cython
ImportError: No module named 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文件中的相应路径)你需要移动并没有帮助
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()
谢谢 - 但似乎python不采取编译版本。尝试删除.py文件并仅使用.pyx进行安装,但在开始导入时无法找到模块(尽管安装无误)或者您是否始终需要保存.py文件?你怎么能确定它正在编译版本呢?我看到这个:http://stackoverflow.com/questions/6584457/what-is-the-precedence-of-python-compiled-files-in-imports但无论如何... – schmi
好吧,我没有抓住这个。我刚刚复制了'.pyx'文件的内容。当你“cython化”一个'.py'文件时,'.py'文件不需要保留。你可以简单地删除它。 –