问题描述
我正在尝试将Cython模块集成到我的项目中,但无法使其正确编译。我已将问题追溯到这个最小的示例:
I'm trying to integrate a Cython module into my project and I'm having trouble getting it to compile correctly. I have traced my problem to this minimal example:
说我有两个文件 a.py
和 b.pyx
位于同一目录中,如果我在 a.py
中执行以下操作:
Say I have two files a.py
and b.pyx
located in the same directory, if I then do the following in a.py
:
import pyximport; pyximport.install()
import b
然后一切正常, b.pyx
已成功编译并导入。
Then everything works fine, b.pyx
is compiled and imported successfully.
但是,如果我改为在 a.py
But if I instead do this in a.py
, assuming that a.py
and b.pyx
are located in dir1/dir2
:
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
import pyximport; pyximport.install()
import dir1.dir2.b
此示例,但说明了阻止我从模块层次结构其他位置导入a的原因),我收到以下异常:
(which is somewhat nonsensical in this example but illustrates what's preventing me from importing a from elsewhere in my module hierarchy), I get the following exception:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 888, in _find_spec
AttributeError: 'PyxImporter' object has no attribute 'find_spec'
这是预期的行为还是我的安装有问题? (我在Windows 10上)
Is this expected behaviour or is something wrong with my installation? (I'm on Windows 10)
注意:SO上还有一个标题非常相似的问题,根本无法回答我的问题。
NOTE: there is another question with a very similar title on SO which does not answer my question at all.
推荐答案
我收到了此错误消息(尽管路径不一定相同),并且通过将Cython从0.28.5升级到0.29来解决。
I had this error message (though not necessarily the same path situation) and it was resolved by upgrading Cython from 0.28.5 to 0.29.
这篇关于Cython:“ PyxImporter”对象没有属性“ find_spec”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!