[英]Get SystemError: Parent module '' not loaded, cannot perform relative import when trying to import numpy in a Cython Extension
I have a cython extension inside a package which is structured like so: 我的包装内有一个cython扩展,其结构如下:
packagename
├── MANIFEST.in
├── packagename
│ ├── __init__.py
│ ├── packagename.py
│ ├── subpackage1
│ │ ├── __init__.py
│ │ ├── subpackage1.py
│ │ └── cythonExt1.pyx
│ ├── subpackage2
│ │ ├── __init__.py
│ │ ├── subpackage2.py
│ │ └── cythonExt2.pyx
│ └── VERSION
├── requirements.txt
└── setup.py
When I try to add a line in cythonExt2.pyx which imports numpy I get the following error: 当我尝试在cythonExt2.pyx中添加一行以导入numpy时,出现以下错误:
-------------------------------------------------------------
SystemError Traceback (most recent call last)
<ipython-input-5-5c4ef4d8efd3> in <module>()
1 # Calling Functions of Interest
----> 2 import pacakge.subpackage2 as thingy
3 import numpy as np
4 import matplotlib.pyplot as plt
/home/user/anaconda2/envs/python3/lib/python3.5/site-packages/pacakage-0.0.3-py3.5-linux-x86_64.egg/package/__init__.py in <module>()
18
19 # the following line imports all the functions from package.py
---> 20 from .package import *
21 import package.subpackage1
22 import package.subpackage2
/home/user/anaconda2/envs/python3/lib/python3.5/site-packages/package-0.0.3-py3.5-linux-x86_64.egg/package/package.py in <module>()
1 from package.subpackage1 import thingy1
----> 2 from package.subpackage2 import thingy2
3 import numpy as _np
4 from multiprocessing import Pool as _Pool
/home/user/anaconda2/envs/python3/lib/python3.5/site-packages/package-0.0.3-py3.5-linux-x86_64.egg/package/subpackage2/subpackage2.py in <module>()
3 import os
----> 4 from cythonExt2 import solve as solve_cython
5 from frange import frange
6
/home/user/anaconda2/envs/python3/lib/python3.5/site-packages/package-0.0.3-py3.5-linux-x86_64.egg/package/subpackage2/cythonExt2.pyx in init package.subpackge2.cythonExt2 (package/subpackage2/cythonExt2.c:6158)()
1 cimport numpy
----> 2 import numpy
3 cimport cython
4
5 def get_z_n(n, z):
SystemError: Parent module '' not loaded, cannot perform relative import
If I just cimport numpy this works and I have access to the numpy C API but I cannot import the numpy python functions which I need to solve a particular problem. 如果我只是导入numpy,这可以工作,并且我可以访问numpy C API,但无法导入解决特定问题所需的numpy python函数。
Why is this and how might I fix it? 为什么会这样,我该如何解决?
I'm wondering if there is an issue with my setup file that is causing this to not work. 我想知道我的安装文件是否存在问题,导致该问题不起作用。 The cython parts of my setup.py file are like so:
我的setup.py文件的cython部分如下所示:
from setuptools import setup
from setuptools.extension import Extension
from Cython.Build import cythonize
from Cython.Build import build_ext
extensions = [Extension(
name="cythonExt1",
sources=["package/subpackage1/cythonExt1.pyx"],
include_dirs=[numpy.get_include()],
),
Extension(
name="cythonExt2",
sources=["package/subpackage2/cythonExt2.pyx"],
include_dirs=[numpy.get_include()],
)
]
setup(name='package',
...
include_package_data=True,
packages=['package',
'package.subpackage1',
'package.subpackage2',
],
ext_modules = cythonize(extensions),
install_requires=requirements,
)
The issue was that numpy was not being included properly with this setup.py file and workflow. 问题是此setup.py文件和工作流未正确包含numpy。 I fixed this by changing it to be such that the cython extension is cythonized in it's own directory with it's own setup.py file into a .c file and that file is then added as a C extension in the package's setup.py file.
我通过将其更改为将cython扩展名与自己的setup.py文件一起在其自己的目录中进行了cython化,将其修复为.c文件,然后将该文件作为C扩展名添加到了包的setup.py文件中。 This also has the benefit that people installing your module don't need cython as you can distribute the generated .c file with the package.
这还具有一个好处,即安装模块的人员不需要cython,因为您可以将生成的.c文件与软件包一起分发。
I have created this complete, but simple, example on my github here . 我在这里的 github上创建了这个完整但简单的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.