gpt4 book ai didi

python - Matplotlibcpp - 导入 Python 模块的问题

转载 作者:行者123 更新时间:2023-12-01 14:58:54 24 4
gpt4 key购买 nike

我已经看到 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),它完美地工作:

Importing pyplot in terminal

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

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

这些是我的路径用户:

environmental variables

这些是我的路径系统:

environmental variables

我在系统中也有一个 PYTHONPATH:

environmental variables

最佳答案

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


确保在用户级别定义了以下环境变量。
python 之家 C:\Anaconda3
python 路径 C:\Anaconda3
QT_QPA_PLATFORM_PLUGIN_PATH %PYTHONHOME%库/插件/平台

关于python - Matplotlibcpp - 导入 Python 模块的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54982492/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com