【问题标题】:ImportError: undefined symbol: _Py_ZeroStructImportError:未定义符号:_Py_ZeroStruct
【发布时间】:2021-03-05 23:24:38
【问题描述】:

我正在尝试运行应该导入一些 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)。

知道如何解决这个问题吗?

【问题讨论】:

  • 你是怎么编译的?我怀疑您应该使用 setup.py 文件,而不是尝试在 vscode 中手动进行
  • 我该怎么做?对不起,我还应该提到我对编译 python 代码不是很精通,因为我在这方面还很陌生。
  • 好像成功了,非常感谢!

标签: python cython importerror


【解决方案1】:

所以问题可能是我试图导入在升级之前使用旧版本 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”而没有错误。

【讨论】:

    猜你喜欢
    • 2022-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 2018-02-24
    • 1970-01-01
    • 2022-05-27
    • 2015-12-29
    相关资源
    最近更新 更多