3

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.

Giswok
  • 559
  • 5
  • 16

1 Answers1

4

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.

J.J. Hakala
  • 6,136
  • 6
  • 27
  • 61
  • Ah, they have to be separate extensions - thanks. That explains a couple of things – Giswok Jan 27 '16 at 16:21
  • J.J. Hakala, any chance you could add a bit more detail on why exactly these need to be defined as separate extensions please? (rather than trying to combine several source files into a single extensions as I was before) – Giswok Jan 28 '16 at 15:03
  • 1
    @Giswok I added some explanation – J.J. Hakala Jan 28 '16 at 20:06