错误,在嵌入在C++程序中的 python 脚本中使用 matplotlib 时
Error, when using matplotlib in a python script embedded in a C++ program
我有一个在C++应用程序中调用的python脚本。当我导入 matplotlib 时,出现以下错误。
列出超出范围的索引。
在我的脚本中,唯一matplotlib
相关的代码是
import matplotlib.pyplot as plt
f = plt.figure()
当我自行调用脚本时,它可以工作。只有当它嵌入到C++程序中时,我才会得到这个异常。当脚本使用 matplotlib 时,我需要提供一些额外的依赖项吗?
当我注释掉matplotlib
函数时,我没有得到异常
更新
C++代码来自python文档链接:1.3。纯嵌入
我没有修改C++代码中的任何内容。只要没有与 matplotlib 相关的代码,它就可以工作。
原因是matplotlib
使用的后端在我的情况下是tk
的。tk
的窗口初始化包含此代码
baseName = os.path.basename(sys.argv[0])
在我的情况下,argv 是空的,这就是list index out of range
错误的原因。
我在这里找到的一个解决方法。我们可以像这样传递虚拟值:
wchar_t const *dummy_args[] = { L"Python", NULL }; // const is needed because literals must not be modified
wchar_t const **argv = dummy_args;
int argc = sizeof(dummy_args) / sizeof(dummy_args[0]) - 1;
PySys_SetArgv(argc, const_cast<wchar_t **>(argv));
相关文章:
- 如何通过cpp程序运行shell脚本
- 在程序中编写脚本来编写和编译代码
- 如何在c++中使用system()来运行包含空格的python脚本
- 在cygwin中运行c++脚本时发生错误
- 如何将真正的字符串从python c-api转换为python脚本
- 用于将C++代码转换为 Web 程序集的脚本未终止
- 如何使用C++初始化向量;脚本化值不是数组、指针或矢量错误
- 在原子中使用犰狳与脚本
- 我适合在我的构建系统中使用 shell 脚本而不是AX_CHECK_COMPILE_FLAG吗?
- CANoe - 无法在 CAPL 脚本中打开.dll
- 在从Qt调用的Python脚本中导入OpenCV崩溃
- 将数组/向量发送到 c++ 脚本的 cython 示例
- 脚本的多重定义::LState
- 从C++应用获取PowerShell脚本输出
- 用户控制从 c++ 到 java 脚本的 Webassembly 访问调用
- 使用 Main() 返回值作为 python 脚本的参数
- 如何从测试脚本中运行WAF编译C++程序?
- bash脚本在使用popen()时不返回代码以调用C ++程序
- 如何注册枚举类型到QJSEngine,以便在脚本环境中使用?
- 错误,在嵌入在C++程序中的 python 脚本中使用 matplotlib 时