Cython相对导入错误,即使执行绝对导入也是如此 [英] Cython relative import error, even when doing absolute import
问题描述
我在Cython(使用Python 3.5)中遇到麻烦,无法在单个软件包中的模块之间导入。
I'm having trouble in Cython (with Python 3.5) with importing between modules in a single package.
我遇到的错误是 SystemError:父模块未加载,无法执行相对导入
,即使我显然在进行绝对导入。
The error I'm getting is SystemError: Parent module '' not loaded, cannot perform relative import
, even when I'm apparently doing absolute imports.
下面是我正在使用的简单测试设置。使用下面的纯Python版本( .py
而不是 .pyx
并且没有编译),这可以很好地工作,但无法通过Cython进行编译。请注意,在下面的示例中,我实际上并没有使用任何Cython语言功能,只是编译。
Below is a simple test setup I'm using. This works fine using a pure-Python version of the below (.py
rather than .pyx
and no compilation), but not compiled through Cython. Note I'm not actually using any Cython language features in the below example, just the compilation.
似乎我的结构有些不正确吗?但我只是不知道如何使它正常工作。
It seems there's something about the structure I'm not getting quite right? But I just can't figure out how get this working properly.
文件结构:
PackageMain
|
+--__init__.py (this is empty)
+--PackageMain.pyx
+--SomeModule.pyx
+--setup.py
文件 PackageMain.pyx
from PackageMain.SomeModule import SomeClass2 # get same error with relative import, ie just .SomeModule
class SomeClass1:
def __init__(self):
foo = SomeClass2()
文件 SomeModule .pyx
:
class SomeClass2:
def __init__(self):
print("Whoop!")
文件 setup.py
:
from distutils.core import setup, Extension
from Cython.Build import cythonize
extensions = [ Extension(language="c++", name="PackageMain",
sources=["PackageMain.pyx", "SomeModule.pyx"])]
setup( name = 'Some name',
ext_modules = cythonize(extensions) )
使用Cython生成的 .pyd
文件从PackageMain导入来运行PackageMain
然后会导致上述错误。
Running from PackageMain import PackageMain
using the .pyd
file produced by Cython then results in the above error.
推荐答案
使用以下代码/定义,
>>> from mytest import MainModule
>>> dir(MainModule)
['SomeClass1', 'SomeClass2', '__builtins__', '__doc__',
'__file__', '__loader__', '__name__', '__package__', '__spec__', '__test__']
setup.py
from distutils.core import setup, Extension
from Cython.Build import cythonize
extensions = [ Extension(language="c++",
name="mytest.MainModule", sources=["mytest.MainModule.pyx"]),
Extension(language="c++",
name="mytest.SomeModule", sources=["mytest.SomeModule.pyx"])]
setup( name = 'mytest',
ext_modules = cythonize(extensions) )
mytest.MainModule.pyx
class SomeClass1:
def __init__(self):
foo = SomeClass2()
mytest.SomeModule.pyx
class SomeClass2:
def __init__(self):
print("Whoop!")
当python加载扩展模块 ABC
,它期望找到名为 initABC
的函数,该函数将被调用以初始化模块。如果将cython生成的两个C ++文件编译并放入同一个共享库,则不会调用另一个基于Cython的模块的init函数,也不会找到该模块。
When python loads an extension module ABC
, it expects to find a function named initABC
that will be called to initialize the module. If two C++ files generated by cython are compiled and put to the same shared library, the init function of the other Cython-based module will not be called, and the module will not be found.
这篇关于Cython相对导入错误,即使执行绝对导入也是如此的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!