C/C++调用python
来源:互联网 发布:淘宝与京东哪个更可靠 编辑:程序博客网 时间:2024/06/05 03:29
程序配置:
python3.5
VS2012
Python2.x 也可以参考
1、环境配置
参考:http://blog.csdn.net/chunleixiahe/article/details/50410208这篇文章有详细介绍
2、第一个程序(调用无参函数)
使用到的Python程序:
# hello.py# coding:utf-8import numpy as npdef print_arr(): da=np.zeros((2,2)) print(da) return 1
对应的cpp程序:
#include "Python.h"#include <iostream>using namespace std;int main(int argc, char* argv[]){ Py_Initialize(); if (!Py_IsInitialized()) { return -1; } PyObject *pModule = NULL; pModule = PyImport_ImportModule("hello"); //导入hello.py if (!pModule) { printf("not found .py file\n"); } PyObject *pFunc = NULL; PyObject *pValue = NULL; PyObject *pArgs=NULL;//方法一 //直接调用hello.py中的print_arr 其中"()"表示无参数 printf("方法一\n");pValue = PyObject_CallMethod(pModule, "print_arr", "()"); cout << PyLong_AsLong(pValue) << endl; //只能转换一个数值(Py中的long转成C++中的long),如果多于1个数值就会出错//方法二 printf("方法二\n");pFunc = PyObject_GetAttrString(pModule, "print_arr"); //也可以使用该函数得到函数对象pValue = PyObject_CallFunction(pFunc, "()");cout << PyLong_AsLong(pValue) << endl;//方法三printf("方法三\n");pFunc = PyObject_GetAttrString(pModule, "print_arr"); pArgs=PyTuple_New(0);pValue = PyObject_CallObject(pFunc, pArgs);cout << PyLong_AsLong(pValue) << endl;Py_Finalize(); /* 结束Python解释器,释放资源 */system("pause"); return 0;}
运行结果:
结果证明,可行!
3、第二个程序(1个参数)
Python程序:
# hello.py# coding:utf-8import numpy as npdef change(data): da=np.array(data,dtype=float) print(da) return 10
cpp程序:
#include "Python.h"#include <iostream>using namespace std;int main(int argc, char* argv[]){ Py_Initialize(); if (!Py_IsInitialized()) { return -1; } PyObject *pModule = NULL; pModule = PyImport_ImportModule("hello"); //导入hello.py if (!pModule) { printf("not found .py file\n"); } PyObject *pFunc = NULL; PyObject *pValue = NULL; PyObject *pArgs=NULL;//方法一 //直接调用hello.py中的print_arr printf("方法一\n");pValue = PyObject_CallMethod(pModule, "change", "i",1); cout << PyLong_AsLong(pValue) << endl; //只能转换一个数值(Py中的long转成C++中的long),如果多于1个数值就会出错//方法二 printf("方法二\n");pFunc = PyObject_GetAttrString(pModule, "change"); //也可以使用该函数得到函数对象pValue = PyObject_CallFunction(pFunc, "i",1);cout << PyLong_AsLong(pValue) << endl;//方法三printf("方法三\n");pFunc = PyObject_GetAttrString(pModule, "change"); pArgs=PyTuple_New(1);PyTuple_SetItem(pArgs, 0, Py_BuildValue("i", 1)); pValue = PyObject_CallObject(pFunc, pArgs);cout << PyLong_AsLong(pValue) << endl;Py_Finalize(); /* 结束Python解释器,释放资源 */system("pause"); return 0;}
运行结果:
事实胜于雄辩!
3.1、传一个数组做参数
cpp程序:
#include "Python.h"#include <iostream>using namespace std;int main(int argc, char* argv[]){ Py_Initialize(); if (!Py_IsInitialized()) { return -1; } PyObject *pModule = NULL; pModule = PyImport_ImportModule("hello"); //导入hello.py if (!pModule) { printf("not found .py file\n"); } PyObject *pFunc = NULL; PyObject *pValue = NULL; PyObject *pArgs=NULL;//方法一 //直接调用hello.py中的print_arr printf("方法一\n");pValue = PyObject_CallMethod(pModule, "change", "((ii))",1,2); cout << PyLong_AsLong(pValue) << endl; //只能转换一个数值(Py中的long转成C++中的long),如果多于1个数值就会出错//方法二 printf("方法二\n");pFunc = PyObject_GetAttrString(pModule, "change"); //也可以使用该函数得到函数对象pValue = PyObject_CallFunction(pFunc, "[ii]",1,2);cout << PyLong_AsLong(pValue) << endl;//方法三printf("方法三\n");pFunc = PyObject_GetAttrString(pModule, "change"); pArgs=PyTuple_New(1);PyTuple_SetItem(pArgs, 0, Py_BuildValue("[iiii]", 1,2,3,4)); pValue = PyObject_CallObject(pFunc, pArgs);cout << PyLong_AsLong(pValue) << endl;Py_Finalize(); /* 结束Python解释器,释放资源 */system("pause"); return 0;}
运行结果:
3.2、传一个数组做参数(数组长度很长)
cpp程序:
#include "Python.h"#include <iostream>using namespace std;int main(int argc, char* argv[]){ Py_Initialize(); if (!Py_IsInitialized()) { return -1; } PyObject *pModule = NULL; pModule = PyImport_ImportModule("hello"); //导入hello.py if (!pModule) { printf("not found .py file\n"); } PyObject *pFunc = NULL; PyObject *pValue = NULL; PyObject *pArgs=NULL; PyObject *pList=NULL;/* //方法一 //直接调用hello.py中的print_arr 其中"()"表示无参数 printf("方法一\n");pValue = PyObject_CallMethod(pModule, "change", "((ii))",1,2); cout << PyLong_AsLong(pValue) << endl; //只能转换一个数值(Py中的long转成C++中的long),如果多于1个数值就会出错*//*//方法二 printf("方法二\n");pFunc = PyObject_GetAttrString(pModule, "change"); //也可以使用该函数得到函数对象pValue = PyObject_CallFunction(pFunc, "[ii]",1,2);cout << PyLong_AsLong(pValue) << endl;*///方法三printf("方法三\n");pFunc = PyObject_GetAttrString(pModule, "change"); pArgs=PyTuple_New(1);pList=PyList_New(0);//一个空列表for(int i=0;i<10;i++){ PyList_Append(pList,Py_BuildValue("i",i)); // 使用append将值放入列表中}PyTuple_SetItem(pArgs,0,pList);//将列表pList作为参数赋给pArgs//PyTuple_SetItem(pArgs, 0, Py_BuildValue("[iiii]", 1,2,3,4)); pValue = PyObject_CallObject(pFunc, pArgs);cout << PyLong_AsLong(pValue) << endl;Py_Finalize(); /* 结束Python解释器,释放资源 */system("pause"); return 0;}
运行结果:
4、第3个程序(多个参数)
Python程序:
# hello.py# coding:utf-8import numpy as npdef change(data1,data2): # da=np.array(data,dtype=float) print(data1+data2) return (data1+data2)
cpp程序:
#include "Python.h"#include <iostream>using namespace std;int main(int argc, char* argv[]){ Py_Initialize(); if (!Py_IsInitialized()) { return -1; } PyObject *pModule = NULL; pModule = PyImport_ImportModule("hello"); //导入hello.py if (!pModule) { printf("not found .py file\n"); } PyObject *pFunc = NULL; PyObject *pValue = NULL; PyObject *pArgs=NULL; PyObject *pList=NULL;//方法一 //直接调用hello.py中的print_arr 其中"()"表示无参数 printf("方法一\n");pValue = PyObject_CallMethod(pModule, "change", "ii",1,2); cout << PyLong_AsLong(pValue) << endl; //只能转换一个数值(Py中的long转成C++中的long),如果多于1个数值就会出错//方法二 printf("方法二\n");pFunc = PyObject_GetAttrString(pModule, "change"); //也可以使用该函数得到函数对象pValue = PyObject_CallFunction(pFunc, "(ii)",1,2);cout << PyLong_AsLong(pValue) << endl;//方法三printf("方法三\n");pFunc = PyObject_GetAttrString(pModule, "change"); pArgs=PyTuple_New(2);PyTuple_SetItem(pArgs, 0, Py_BuildValue("i", 1)); PyTuple_SetItem(pArgs, 1, Py_BuildValue("i", 2));pValue = PyObject_CallObject(pFunc, pArgs);cout << PyLong_AsLong(pValue) << endl;Py_Finalize(); /* 结束Python解释器,释放资源 */system("pause"); return 0;}
运行结果:
5、总结
- 使用到的.py文件需放置在.exe 目录下,否则会找不到文件
- PyLong_AsLong(pValue) 只能获取到Python中传递的一个数值,如果是数组就会报错,这个问题还没找到合适的解决方法(如果有谁知道可以给我留言,在此感谢!)
- 说明下一些字符代表的意义, s 代表字符串, i 代表整形变量, f 代表浮点数,o 代表一个python对象
Py_BuildValue的使用例子,来自python documentation:
Py_BuildValue(“”) None
Py_BuildValue(“i”, 123) 123
Py_BuildValue(“iii”, 123, 456, 789) (123, 456, 789)
Py_BuildValue(“s”, “hello”) ‘hello’
Py_BuildValue(“ss”, “hello”, “world”) (‘hello’, ‘world’)
Py_BuildValue(“s#”, “hello”, 4) ‘hell’
Py_BuildValue(“()”) ()
Py_BuildValue(“(i)”, 123) (123,)
Py_BuildValue(“(ii)”, 123, 456) (123, 456)
Py_BuildValue(“(i,i)”, 123, 456) (123, 456)
Py_BuildValue(“[i,i]”, 123, 456) [123, 456]
Py_BuildValue(“{s:i,s:i}”, “abc”, 123, “def”, 456) {‘abc’: 123, ‘def’: 456}
Py_BuildValue(“((ii)(ii)) (ii)”, 1, 2, 3, 4, 5, 6) (((1, 2), (3, 4)), (5, 6))不知问什么使用debug模式总是出错,使用Release模式就能运行,所有建议使用Release模式
如果Python是64位的,相应的VS要切换到x64
6、参考文献
- http://blog.csdn.net/chunleixiahe/article/details/50410208
- http://www.360doc.com/content/13/0812/11/9934052_306564761.shtml
- http://blog.csdn.net/taiyang1987912/article/details/44779719
- http://blog.csdn.net/shenwansangz/article/details/44019433
- http://blog.csdn.net/shenwansangz/article/details/50055107
- https://docs.python.org/3.5/extending/index.html#extending-index
- http://blog.csdn.net/chunleixiahe/article/details/50410553
暂时先写这么多,以后还会不定期更新!
- BCB(C++)调用Python
- C中调用PYTHON
- C调用python
- C调用python
- Python 调用 C函数
- python调用c
- Python 调用C
- c中调用python
- Python调用c/c++
- c 调用python
- python 调用 c 函数
- python调用C函数库
- Python调用C
- Python调用C/C++
- Python调用C程序
- python调用c/c++
- python调用c
- c调用python
- 筋斗云接口编程 / 对象型接口
- 二叉树学习(一):二叉树概念及存储结构
- html5新增的语义标签
- Android修改图片颜色-转成灰度图
- 定义和使用EL函数
- C/C++调用python
- 【Objective-C基础】集合类
- GDB多进程调试
- 筋斗云接口编程 / 对象型接口(二)
- 1 maven依赖其他项目的代码,调试的时候进不了依赖项目的源码。2 a项目更改,b项目立即提现
- 04 LINUX ubuntu 启动出错和桥接联网设置
- Joomla(5)登录、注销、注册和更新的Demo
- 日常知识总结之-修改字体大小和颜色
- 412. Fizz Buzz(1行代码)