国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

C/C++調用python

2019-11-06 08:19:10
字體:
來源:轉載
供稿:網友

程序配置: 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/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

暫時先寫這么多,以后還會不定期更新!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 任丘市| 苏尼特右旗| 马鞍山市| 盐津县| 静安区| 子洲县| 伊宁县| 越西县| 阳谷县| 会宁县| 扶余县| 商南县| 固阳县| 小金县| 朔州市| 湘乡市| 泰宁县| 山西省| 红桥区| 安岳县| 保康县| 叶城县| 昌邑市| 浑源县| 临沭县| 盘山县| 英德市| 宝鸡市| 乐清市| 马关县| 琼结县| 静安区| 泽州县| 永宁县| 绥化市| 镇赉县| 新昌县| 惠东县| 聂拉木县| 唐河县| 夹江县|