Cython relative import error, even when doing abso

2019-07-13 12:09发布

问题:

I'm having trouble in Cython (with Python 3.5) with importing between modules in a single package.

The error I'm getting is SystemError: Parent module '' not loaded, cannot perform relative import, even when I'm apparently doing absolute imports.

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.

File structure:

PackageMain
 |
 +--__init__.py  (this is empty)
 +--PackageMain.pyx
 +--SomeModule.pyx
 +--setup.py

File PackageMain.pyx

from PackageMain.SomeModule import SomeClass2  # get same error with relative import, ie just .SomeModule

class SomeClass1:
      def __init__(self):
          foo = SomeClass2()

File SomeModule.pyx:

class SomeClass2:
      def __init__(self):
          print("Whoop!")

File 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) )

Running from PackageMain import PackageMain using the .pyd file produced by Cython then results in the above error.

回答1:

With the following code / definitions,

>>> 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!")

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.