简体   繁体   English

获取SystemError:父模块''未加载,尝试在Cython扩展中导入numpy时无法执行相对导入

[英]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.

相关问题 Python:未加载SystemError父模块,无法执行相对导入 - Python: SystemError Parent module not loaded, cannot perform relative import SystemError:未加载父模块“setuptools”,无法执行相对导入,jupyterhub - SystemError: Parent module 'setuptools' not loaded, cannot perform relative import, jupyterhub 我收到SystemError:父模块&#39;&#39;未加载,运行Python脚本时无法执行相对导入错误 - I get SystemError: Parent module '' not loaded, cannot perform relative import Error when I run my Python Script 项目内部的相对导入导致“SystemError:Parent module”未加载,无法执行相对导入“ - Relative import inside project results in “SystemError: Parent module '' not loaded, cannot perform relative import” 从..layers导入输入SystemError:父模块&#39;&#39;未加载,无法执行相对导入 - from ..layers import Input SystemError: Parent module '' not loaded, cannot perform relative import 父模块“”未加载,无法执行相对导入 - Parent module '' not loaded, cannot perform relative import 导入时“父模块&#39;&#39;未加载,无法执行相对导入”。<module> - "Parent module '' not loaded, cannot perform relative import" on importing ..<module> 为 cython 导入 numpy 时发生错误 - Error happens when import numpy for cython 具有cython扩展名的Python包获取导入错误 - Python package with cython extension get import error scons无法导入numpy模块 - scons cannot import numpy module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM