gpt4 book ai didi

python - 在 Visual Studio 2019 中运行 python matplotlibcpp

转载 作者:行者123 更新时间:2023-12-05 02:44:26 25 4
gpt4 key购买 nike

我想为 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 systemcodec 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),我通过执行项目,属性进入项目的属性,然后添加 C/C++ 属性,Additionnal include 目录:E:\Python39\include;E:\Python39\Lib\site-packages\numpy\core\include

set additionnal include directories

然后(6),在链接器、通用、Additionnal 库目录中,我添加: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 中选择“release”而不是“debug”(并且一定要在 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/

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