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

时间:2017-10-20 11:22:24

标签: python numpy cython

我在一个包中有一个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

当我尝试在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

如果我只是cimport numpy这工作,我可以访问numpy C API,但我无法导入我需要解决特定问题的numpy python函数。

为什么会这样,我该如何解决?

我想知道我的安装文件是否存在导致此操作无效的问题。我的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,                                                              
)

1 个答案:

答案 0 :(得分:0)

问题是这个setup.py文件和工作流程没有正确包含numpy。我通过更改它来修复此问题,以便将cython扩展程序在其自己的目录中进行cython化,并将其自己的setup.py文件转换为.c文件,然后将该文件作为C扩展名添加到包的setup.py文件。这也有一个好处,即安装模块的人不需要cython,因为您可以使用包分发生成的.c文件。

我已经在我的github here上创建了这个完整而简单的示例。