【问题标题】:Matplotlibcpp - Issue importing Python modulesMatplotlibcpp - 导入 Python 模块的问题
【发布时间】:2019-07-25 17:18:37
【问题描述】:

我已经看到 mac OS 用户出现了这个问题,但我目前在 windows10 上,我不知道如何解决这个问题。

我使用的是 Visual Studio 2017 和 Python 2.7。但是,我的 PC 中也有 Python 3.7 和 Anaconda,所以我希望这不会弄乱正确库的链接器。

我正在使用这个库https://github.com/lava/matplotlib-cpp

在 matplotlibcpp.h 中,第 137 行。我遇到了一个异常:

Excepción producida en 0x00007FFFCF28E78A (ntdll.dll) en ConsoleApplication2.exe: 0xC0000008: An invalid handle was specified.

这是失败的代码部分(第 137 行是对 pyplotname 的 PY_DECREF 调用)

PyObject* matplotlibname = PyString_FromString("matplotlib");
    PyObject* pyplotname = PyString_FromString("matplotlib.pyplot");
    PyObject* cmname  = PyString_FromString("matplotlib.cm");
    PyObject* pylabname  = PyString_FromString("pylab");
    if (!pyplotname || !pylabname || !matplotlibname || !cmname) {
      throw std::runtime_error("couldnt create string");
    }

    PyObject* matplotlib = PyImport_Import(matplotlibname);
    Py_DECREF(matplotlibname);
    if (!matplotlib) { throw std::runtime_error("Error loading module matplotlib!"); }

    // matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
    // or matplotlib.backends is imported for the first time
    if (!s_backend.empty()) {
        PyObject_CallMethod(matplotlib, const_cast<char*>("use"), const_cast<char*>("s"), s_backend.c_str());
    }

    PyObject* pymod = PyImport_Import(pyplotname);
    Py_DECREF(pyplotname);
    if (!pymod) { throw std::runtime_error("Error loading module matplotlib.pyplot!"); }

当我在终端中输入import matplotlib.pyplot(使用python2.7)时,它可以完美运行:

如果我在第 119 行操作名称,所以我写的不是“matplotlib.pyplot”,而是“matplotlib/pyplot”,我到达第 138 行,它按预期弹出错误消息。

所以我猜它正在寻找模块,但是一旦他打开它,就会出现错误。有任何想法吗?我在 Mac 上看到了很多关于此的帖子,但在 Windows 上没有看到。

这些是我的路径用户:

这些是我的路径系统:

我在系统中也有一个 PYTHONPATH:

【问题讨论】:

  • 您使用的是虚拟环境吗?
  • 不,我不是。 @杰克穆迪
  • 请将您的代码发布为formatted text,而不是截图
  • 真的有必要吗?代码可以在 github 文件中找到,我截取了屏幕截图以显示视觉参考,但是好的,我会做@ForceBru

标签: python c++ visual-studio matplotlib


【解决方案1】:

我有同样的错误。我正在使用 anakonda python 发行版。 我通过重新安装 matplotlib 修复了错误。 pip 卸载 matplotlib pip install matplotlib

确保在用户级别定义了以下环境变量。 PYTHONHOME C:\Anaconda3 PYTHONPATH C:\Anaconda3 QT_QPA_PLATFORM_PLUGIN_PATH %PYTHONHOME%Library/plugins/platforms

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多