python - 在 Visual Studio 2019 中运行 python matplotlibcpp

标签 python c++ windows c++14

我想为 C++ 程序安装绘图功能。我选择了 matplotlibcpp 因为它的简单性和在线评论。但是,我无法生成图并出现许多神秘错误(我是 C++ 新手)。更新 VS 2019 的包含路径后,我能够在编译和链接时没有错误,但执行时列出了​​许多错误。

下面是一个产生错误的简单代码片段。

#include <matplotlibcpp.h>
#include <string>
using namespace std;
namespace plt = matplotlibcpp;


int main()
{
    plt::plot({ 1,2,2,4 });
    plt::show();
}

我在输出窗口中得到以下输出:

Fatal Python error: initfsencoding: unable to load the file system codec ModuleNotFoundError: No module named 'encodings'

Current thread 0x000018c0 (most recent call first)

关于如何让 matplotlibcpp 在 VS 2019 中运行有什么想法吗?

在 Windows 10 x64 上运行 VS 2019。

最佳答案

我很难找到一个不错的 C++ 绘图库。我决定尝试 matplotlibcpp,过了一段时间,我成功地使其在 Windows 10、Visual Studio 2019、Python 3.9 下运行。

首先,我试图让它与 conda 一起工作。由于 PYTHONHOME 变量设置有问题,我放弃了。因此,我继续进行 python (3.9) 的经典安装。

第一 (1),必须确保可以通过命令提示符或 powershell 访问 python,方法是键入:

python --version

第二(2),我已经安装了 numpy 和 matplolib :

python -m pip install matplotlib
python -m pip install numpy

然后(3)我在 cpp 中创建了一个新项目,并将这些简单的行( https://github.com/lava/matplotlib-cpp )粘贴到新的 cpp 文件中:

#include <iostream>
#include "matplotlibcpp.h"
#include <vector>

namespace plt = matplotlibcpp;
int main() {
    std::vector<double> y = { 1, 3, 2, 4 };
    plt::plot(y);

    plt::show();

}

然后(4),我下载了 matplotlibcpp.h ( https://github.com/lava/matplotlib-cpp/blob/master/matplotlibcpp.h ) 并将其添加到我的项目中 One .cpp and one .h

然后(5),我通过执行project、properties进入项目的属性,然后添加C/C++属性,附加包含目录: E:\Python39\include;E:\Python39\Lib\site-packages\numpy\core\include

set additionnal include directories

然后(6),在链接器、常规、附加库目录中,我添加了: E:\Python39\libs

Additionnal library directories

然后(7)在链接器、输入、附加依赖项中,我添加了: E:\Python39\libs\python39.lib 确保不要删除“%(AdditionalDependencies)”。 AdditionalDependencies

然后,我编译并得到两个不同的错误,这些错误是经典的且不难解决:

(8) 其中一条与 matplotlibcpp.h 中的 4 行相关:这四行上方的注释(“健全性检查”)告诉您如果出现问题,应该对它们进行注释。我做到了并且有效:错误不再出现:

those 4 lines needed to be commented

(9) 编译器还提示一些与 C++ 标准相关的问题。我遵循网上找到的指南,只需进入属性、C/C++、语言并选择 iso C++ 17 标准。然后第二个错误就解决了。

choose iso C++ 17

(10) 然后我再次编译,我记得我也提示过 python39_d.lib 或类似的东西。我只需要在 Visual Studio 中选择“发布”而不是“调试”(并且还要确保在 X64 中)。

(11)然后,我在生成.exe的X64/release文件夹中添加了python39.dll(我还添加了python3.dll,但我认为它没用;只有python39(如果有不同版本则为3x)应该是需要的)。

dont forget to add the dll

(12) 然后就可以了。

working configuration

关于python - 在 Visual Studio 2019 中运行 python matplotlibcpp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66507618/

相关文章:

c++ - 比较网络摄像头帧时,cvNorm() 总是返回 0

c++ - MSVC 上的虚拟表布局-类型信息在哪里?

windows - Windows文件系统是如何工作的?

python - Windows中没有PRIMARY选择剪贴板吗

python - 谁能解释这种奇怪的 python 乌龟事件?

python - 在控制台中重写多行

python - 使用 Python 编辑 .html 文件?

python - 请求 Simpy 资源永远不会成功

c++声明一个属于另一个类的类型的数组

python - PyWin32 使用 MakePy 实用程序和 win32com 获取网络统计信息