程序配置: 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 10cpp程序:
#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/50410208http://www.360doc.com/content/13/0812/11/9934052_306564761.shtmlhttp://blog.csdn.net/taiyang1987912/article/details/44779719http://blog.csdn.net/shenwansangz/article/details/44019433http://blog.csdn.net/shenwansangz/article/details/50055107https://docs.python.org/3.5/extending/index.html#extending-indexhttp://blog.csdn.net/chunleixiahe/article/details/50410553暂时先写这么多,以后还会不定期更新!
新闻热点
疑难解答