程序配置: 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中傳遞的一個數值,如果是數組就會報錯,這個問題還沒找到合適的解決方法(如果有誰知道可以給我留言,在此感謝?。┱f明下一些字符代表的意義, 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暫時先寫這么多,以后還會不定期更新!
新聞熱點
疑難解答