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.