导入错误:未定义符号:_Py_ZeroStruct

时间:2021-03-05 11:52:44

标签: python cython importerror

我正在尝试运行一个应该导入一些 Cython 代码的 Python 脚本,但出现以下错误:

ImportError: 'path to my .so file': undefined symbol: _Py_ZeroStruct

我读到这可能是由于 Python 版本之间的不匹配造成的。但是,我尝试使用 Python 2.7 和 3.8(在 Visual Studio 代码上)进行编译,但总是遇到相同的错误。我正在尝试运行此 Ubuntu 20.04。我还应该补充一点,在几天前升级之前,代码在旧版本的 Ubuntu 上运行良好(我之前有 Ubuntu 16)。

知道如何解决问题吗?

1 个答案:

答案 0 :(得分:0)

所以问题可能是我试图导入在升级之前用旧版本的 Ubuntu 编译的 .so 文件(可能是初学者的错误!)。

我按照建议的 here 重新编译了我的 .pyx 文件来修复它。我最初收到错误 numpy/arrayobject.h: No such file or directory。我用 include_dirs=[numpy.get_include()] 解决了这个问题。所以最后我编译了我的 .pyx :

from distutils.core import setup
from Cython.Build import cythonize
import numpy

setup(
    ext_modules = cythonize("cython_all_fit.pyx"),
    include_dirs=[numpy.get_include()]
)

我运行 python setup.py build_ext --inplace,它生成了我的 .so 文件。然后我可以在我的主要 python 代码中导入“cython_all_fit”而没有错误。

相关问题