作者:虚坏叔叔
博客:
早餐店不会开到晚上,想吃的人早就来了!😄
c++给python传递变量的两种方法
一、传递变量给_main_模块
python代码test.py中添加变量打印,这时变量a需要从c++端传递过来
# c++传递的变量(PyRun_SimpleString("a=888");)print("c++ to python a = ", a)
c++添加变量的定义并传递
// 传递位置要在python代码调用之前// 给python传递 变量、函数、类、模块(读取一个python模块传递给主模块)// 传递变量(只能传给主模块__main__)PyRun_SimpleString("a=888");
运行结果:
二、传递变量给非_main_模块
python代码test.py中添加变量打印,这时变量count需要从c++端传递过来
print("c++ to python count = ", count)
c++添加变量的定义并传递
// 支持传递其他非_main__模块,空间转给python管理PyObject_SetAttrString(m, "count", PyLong_FromLong(777));
运行
三、整体代码
#include <iostream>#include <Python.h>#include <exception>using namespace std;int main(int argc, char*argv[]){cout << "C++ call Python" << endl;// 设置Python的Home路径Py_SetPythonHome(L"./");// Python初始化解释器Py_Initialize();PyObject *m = NULL; // 主模块try{int re = 0;// 执行Python脚本re = PyRun_SimpleString("print('Hello world!')");re = PyRun_SimpleString("print(\"__name__ = \", __name__)");// 获取主模块PyObject *key = PyUnicode_FromString("__main__");m = PyImport_GetModule(key); // 不清理参数,需要手动清理Py_XDECREF(key);// 传递位置要在python代码调用之前// 给python传递 变量、函数、类、模块(读取一个python模块传递给主模块)// 传递变量(只能传给主模块__main__)PyRun_SimpleString("a=888");// 支持传递其他非_main__模块,空间转给python管理PyObject_SetAttrString(m, "count", PyLong_FromLong(777));// 执行Python文件char* filename = "test.py";FILE * fp = fopen(filename, "r");if (!fp){throw exception("open file failed");}PyRun_AnyFile(fp, filename);if (re != 0){PyErr_Print();throw exception("PyRun_AnyFile failed");}// 2-1 调用python的变量 python做配置文件//con = {// "width":1920,// "heigth" : 1080,// "title" : "C++ call Python"//}{// 根据模块和名称获取对象(对象可以是变量、函数和类)PyObject* conf = PyObject_GetAttrString(m, "conf");if (!conf) {throw exception("conf noe find!");}PyObject *key = PyUnicode_FromString("width");int width = PyLong_AsLong(PyDict_GetItem(conf, key));Py_XDECREF(key);key = PyUnicode_FromString("height");int height = PyLong_AsLong(PyDict_GetItem(conf, key));Py_XDECREF(key);key = PyUnicode_FromString("title");wchar_t title[1024] = {0 };int size = PyUnicode_AsWideChar(PyDict_GetItem(conf, key), title, 1023);Py_XDECREF(key);printf("width=%d height=%d \n", width, height);wprintf(L"title=%s\n", title);Py_XDECREF(conf);}{// 获取类PyObject* TypePy = PyObject_GetAttrString(m, "TypePy");if (!TypePy) {throw exception("TypePy noe find!");}// 实例化对象 相当于调用构造函数__init__ 传递构造函数的参数NULLPyObject *obj = PyObject_CallObject(TypePy, NULL);if (!obj) {throw exception("obj not Create!");}// 调用类成员函数 i(int) s(string)PyObject *re = PyObject_CallMethod(obj, "test", "is", 2001, "c Para2");cout << "PyObject_CallMethod return" << PyLong_AsLong(re) << endl;Py_XDECREF(re);// 访问成员变量PyObject* var = PyObject_GetAttrString(obj, "id");cout << "TypePy.id=" << PyLong_AsLong(var) << endl;Py_XDECREF(var);Py_XDECREF(obj);Py_XDECREF(TypePy);}{// C++调用Python函数PyObject* Main = PyObject_GetAttrString(m, "Main");if (Main && PyCallable_Check(Main)) {// 函数对象和参数 返回对象if (!PyObject_CallObject(Main, 0)){throw exception("PyObject_CallObject Failed");}}Py_XDECREF(Main);// c++调用Python的函数 list参数和list返回值PyObject* TestFun = PyObject_GetAttrString(m, "TestFun");if (TestFun && PyCallable_Check(TestFun)) {// 参数准备 参数变量是tuplePyObject *args = PyTuple_New(1); // 参数是元组 只有元组这个一个参数// 传递的list对象PyObject *lis = PyList_New(0);for (int i = 0; i < 5; i++)PyList_Append(lis, PyLong_FromLong(i + 100));// 将list写入元组参数列表中PyTuple_SetItem(args, 0, lis);// 函数对象和参数的返回值PyObject* re = PyObject_CallObject(TestFun, args);Py_XDECREF(args); // lis也在args中销毁// 处理返回值if (re){cout << "PyObject_CallObject return" << endl;int size = PyList_Size(re);for (int i = 0; i < size; i++){PyObject *val = PyList_GetItem(re, i);if (!val)continue;printf("[%d]", PyLong_AsLong(val));}Py_XDECREF(re);}}Py_XDECREF(TestFun);}Py_XDECREF(m);// 清理pythonPy_Finalize();}catch (const std::exception&ex){cout << ex.what() << endl;// 清理pythonPy_XDECREF(m);Py_Finalize();}getchar();return 0;}
四、总结
本文使用c++给python传递变量的两种方法 。如果觉得文章对你有用处,记得点赞
收藏
转发
一波哦,博主也支持为铁粉丝制作专属动态壁纸哦~💬 往期优质文章分享
C++ QT结合FFmpeg实战开发视频播放器-01环境的安装和项目部署解决QT问题:运行qmake:Project ERROR: Cannot run compiler ‘cl‘. Output:解决安装QT后MSVC 64bit配置无编译器和调试器问题Qt中的套件提示no complier set in kit和no debugger,出现黄色感叹号问题解决(MSVC)Python+selenium 自动化 - 实现自动导入、上传外部文件(不弹出windows窗口)🚀 优质教程分享 🚀
🎄如果感觉文章看完了不过瘾,可以来我的其他 专栏 看一下哦~🎄比如以下几个专栏:Python实战微信订餐小程序、Python量化交易实战、C++ QT实战类项目 和 算法学习专栏🎄可以学习更多的关于C++/Python的相关内容哦!直接点击下面颜色字体就可以跳转啦!🚀 资料白嫖,温馨提示🚀
关注下面卡片即刻获取更多编程知识,包括各种语言学习资料,上千套PPT模板和各种游戏源码素材等等资料。更多内容可自行查看哦!
如果觉得《PythonC++相互混合调用编程全面实战-18c++给python传递变量的两种方法》对你有帮助,请点赞、收藏,并留下你的观点哦!