C++调用Python,,最近在学习Pytho


最近在学习Python,主要是学习脚本语言,可以跨平台,轻量级,拥有做一些测试的工具。当然也是因为,看Boost书的时候,里面说C++可以调用Python,所以就花了一段时间学习了下。现在开始学习C++调用Python.

安装完Python后,安装目录下有头文件include和libs,VS可以通过设置这两个来引用Python.


项目设置很简单,主要项目头文件目录设置为include,库目录设置为libs即可。

使用时只需要引用“Python.h”头文件。

注意:1.debug下,python27.lib需要复制一份命名为python27_d.lib

//CallPython.cpp:定义控制台应用程序的入口点。//#include"stdafx.h"#include"Python.h"#include<iostream>usingnamespacestd;voidTestCallFunc();voidTestCallFuncDict();voidTestClass();int_tmain(intargc,_TCHAR*argv[]){TestCallFunc();TestCallFuncDict();TestClass();getchar();return0;}voidTestCallFunc(){Py_Initialize();//使用Python之前,要调用Py_Initialize初始化PyObject*pModule=NULL;PyObject*pFunc=NULL;PyObject*pAddFunc=NULL;pModule=PyImport_ImportModule("testPython");//加载模块,加载py文件if(pModule){//加载函数,HelloWorld,无参数pFunc=PyObject_GetAttrString(pModule,"HelloWorld");//获取模块的函数if(pFunc){PyEval_CallObject(pFunc,NULL);//调用函数,NULL表示参数为空//Py_DECREF(pFunc);}else{cout<<"pFunc=NULL"<<endl;}//加载函数,Add,两个参数pAddFunc=PyObject_GetAttrString(pModule,"add");if(pAddFunc){//创建参数PyObject*pArgs=PyTuple_New(2);//函数调用的参数传递均是以元组的形式PyTuple_SetItem(pArgs,0,Py_BuildValue("i",5));//0--序号,i表示创建int型变量PyTuple_SetItem(pArgs,1,Py_BuildValue("i",7));//1--序号//返回值PyObject*pReturn=NULL;//pReturn=PyEval_CallObject(pAddFunc,pArgs);//传入多个参数//pReturn=PyEval_CallFunction(pAddFunc,"ii",5,7);pReturn=PyObject_CallFunction(pAddFunc,"ii",5,7);//将返回值转为int类型intresult;PyArg_Parse(pReturn,"i",&result);cout<<"5+7="<<result<<endl;//Py_DECREF(pAddFunc);}else{cout<<"pAddFunc=NULL"<<endl;}}else{cout<<"pModule=NULL"<<endl;}//Py_DECREF(pModule);Py_Finalize();//释放}voidTestCallFuncDict(){Py_Initialize();PyObject*pModule=NULL;PyObject*pFunc=NULL;pModule=PyImport_ImportModule("testPython");if(pModule){pFunc=PyObject_GetAttrString(pModule,"TestDict");if(pFunc){//设置参数字典PyObject*pArgs=PyTuple_New(1);PyObject*pDict=PyDict_New();PyDict_SetItemString(pDict,"Name",Py_BuildValue("s","Longma"));PyDict_SetItemString(pDict,"Age",Py_BuildValue("i",25));PyTuple_SetItem(pArgs,0,pDict);PyObject*pReturn=PyEval_CallObject(pFunc,pArgs);//处理返回值intsize=PyDict_Size(pReturn);cout<<"返回字典的大小为:"<<size<<endl;PyObject*pNewAge=PyDict_GetItemString(pReturn,"Age");intnewAge=_PyInt_AsInt(pNewAge);//PyArg_Parse(pNewAge,"i",&newAge);cout<<"TrueAge:"<<newAge<<endl;PyObject*pNewName=PyDict_GetItemString(pReturn,"Name");char*pName=PyString_AsString(pNewName);cout<<"TrueName:"<<pName<<endl;//Py_DECREF(pFunc);}}Py_Finalize();}voidTestClass(){Py_Initialize();PyObject*pModule=NULL;pModule=PyImport_ImportModule("testPython");if(pModule){//获取Person类PyObject*pClassPerson=PyObject_GetAttrString(pModule,"Person");//创建Person类实例PyObject*pInstancePerson=PyInstance_New(pClassPerson,NULL,NULL);//调用方法greet方法PyObject_CallMethod(pInstancePerson,"greet","s","HelloKitty");//传入多个参数//PyObject_CallMethod(pInstancePerson,"greet","si","HelloKitty",10);}Py_Finalize();}


Python代码

testPython.py

defHelloWorld():print"HelloWorld"defadd(a,b):returna+bdefTestDict(dict):printdictdict["Age"]=17dict["Name"]="Royma"returndictclassPerson:defgreet(self,greetStr):printgreetStr


C++调用Python

评论关闭