0
我有一個封裝內的用Cython擴展,它的結構像這樣導入numpy的時候不能進行相對進口:獲取關於SystemError:父模塊'不加載,試圖在用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,
)
看起來像你的線「20」是錯誤的。一個點可能只出現在兩個真實姓名之間,而不是一開始。只需刪除點並將父目錄添加到PYTHONPATH。 – guidot
這很奇怪,因爲代碼工作正常,沒有cythonExt2.pyx文件中的導入numpy行,我也在我編寫的其他一些包中執行此操作,並且在那些包中工作正常。我會盡力改變它,看看它是否解決了我的問題。 – SomeRandomPhysicist
我這樣做是爲了導入package.py目錄的內容,以便當我在python中導入包時,我可以執行package.function。當我將目錄添加到路徑並執行導入包時,我只是得到一個沒有導入函數的遞歸package.package.package.package。 – SomeRandomPhysicist