C++中嵌入python程序——使用API接口,从函数到类
来源:互联网 发布:淘宝已买到的宝贝 编辑:程序博客网 时间:2024/06/05 09:10
C++中嵌入python程序——使用API接口,从函数到类
上一篇博客介绍了C++中使用命令行模式调用python,但是感觉交互性相当不足,本文介绍使用API接口调用python程序的函数和类对象。
开发环境为win7 64位,vs2010,python2.7.12
首先需要配置环境,创建win32控制台程序,然后对工程进行配置。
项目–>属性–>vc++目录–>包含目录 添加python安装目录中include目录
项目–>属性–>vc++目录–>库目录 添加python安装目录中libs目录
链接器–>输入–>附加依赖项 添加python27.lib
注意,如果C++工程采用debug版本,需要将将 python安装目录里libs文件夹下的python27.lib文件复制,并将名字改成 python27_d.lib
完成上述配置后,我们开始创建一个myclass.py的python文件,文件内容如下:
#!/usr/bin/env python# -*- coding:utf8 -*-class hi_class: def sayHi(self): print 'Hi! I am a python class!'def hi_function(): print 'Hi! I am a python function!'
上面创建了一个类和一个函数,下面我们来编写C++代码
#include <iostream> #include <stdio.h>#include <stdlib.h>#include <Python.h>#include <string.h>using namespace std; int main() { Py_Initialize(); // 初始化,这是必须的,用来初始化python所需的环境 if (!Py_IsInitialized()) return -1; // 导入模块 PyObject* pModule = NULL; PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append('./')"); pModule = PyImport_ImportModule("myclass"); if (!pModule) { printf("Cant open python file!/n"); return -1; } // 模块的字典列表 PyObject* pDict = PyModule_GetDict(pModule); if (!pDict) { printf("Cant find dictionary./n"); return -1; // 演示函数调用 cout<<"calling python function..."<<endl; PyObject* pFunHi = PyDict_GetItemString(pDict, "hi_function"); PyObject_CallFunction(pFunHi, NULL, NULL); Py_DECREF(pFunHi); //演示类调用 cout<<"calling python class..."<<endl; // 演示构造一个Python对象,并调用Class的方法 // 获取hi_class类 PyObject* phi_class = PyDict_GetItemString(pDict, "hi_class"); if (!phi_class ) { printf("Cant find phi_class class./n"); return -1; } //构造hi_class的实例 PyObject* pInstance_hi_class = PyInstance_New(phi_class , NULL, NULL); //如果python类中有初始化参数,那么创建实例的时候需要将初始化参数以元组形式传递进去(亲测可用)PyObject* pInstance_hi_class = PyInstance_New(phi_class , PyObject*类型的元组(需要在C++里创建python元组), NULL); //C++中创建并初始化python元组示例如下两行: //PyObject *pArgs3 = PyTuple_New(1); //PyTuple_SetItem(pArgs3, 0, Py_BuildValue("i", 3)); if (!pInstance_hi_class) { printf("Cant create instance./n"); return -1; } //调用hi_class类实例pInstance_hi_class里面的方法 PyObject_CallMethod(phi_class, "sayHi", "O", pInstance_hi_class ); //释放 Py_DECREF(phi_class); Py_DECREF(pInstance_hi_class ); Py_DECREF(pModule); Py_Finalize(); // 与初始化对应 system("pause"); return 0;}
这样,C++调用python函数和类的基本方法已经介绍完毕。
但是,又有一个问题,如果我想在C++里面存储python类(也就是python class作为C++的中间变量)该怎么实现呢?
如果你仔细看看C++里面的代码,你就会发现,其实C++程序中PyObject* 不正好可以存储吗?所以这个问题不必担心。
我们重新开始吧,对上面的程序重新做一下修改
myclass.py
#!/usr/bin/env python# -*- coding:utf8 -*-class hi_class: def sayHi(self): print 'Hi! I am a python class!'def hi_function(): py_class = hi_class() return py_class
C++程序
#include <iostream> #include <stdio.h>#include <stdlib.h>#include <Python.h>#include <string.h>using namespace std; int main() { Py_Initialize(); // 初始化,这是必须的,用来初始化python所需的环境 if (!Py_IsInitialized()) return -1; // 导入模块 PyObject* pModule = NULL; PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append('./')"); pModule = PyImport_ImportModule("myclass"); if (!pModule) { printf("Cant open python file!/n"); return -1; } // 模块的字典列表 PyObject* pDict = PyModule_GetDict(pModule); if (!pDict) { printf("Cant find dictionary./n"); return -1; // 函数调用 cout<<"calling python program..."<<endl; PyObject* pFunHi = PyDict_GetItemString(pDict, "hi_function"); // 利用python函数返回hi_class类(hi_function函数返回hi_class类,看我python程序) PyObject* phi_class = PyObject_CallFunction(pFunHi, NULL, NULL); if (!phi_class ) { printf("Cant find phi_class class./n"); return -1; } //构造hi_class的实例 PyObject* pInstance_hi_class = PyInstance_New(phi_class , NULL, NULL); if (!pInstance_hi_class) { printf("Cant create instance./n"); return -1; } //调用hi_class类实例pInstance_hi_class里面的方法 PyObject_CallMethod(phi_class, "sayHi", "O", pInstance_hi_class ); //释放 Py_DECREF(phi_class); Py_DECREF(pInstance_hi_class ); Py_DECREF(pFunHi); Py_DECREF(pModule); Py_Finalize(); // 与初始化对应 system("pause"); return 0;}
看,我们已经从python函数返回值获取了python类,我们可以添加我们所需的任何功能了。比如,我们做机器学习,训练好了自己的分类器并保存成了文件,然后我们就可以用python加载并返回给 C++,这样我们就能在C++里面使用这个分类器了,很方便吧。
- C++中嵌入python程序——使用API接口,从函数到类
- 《扩展和嵌入python解释器》1.6 从C中调用Python函数
- python中嵌入C运行时函数
- 译;将Python嵌入到Qt程序中
- C++中嵌入python程序——命令行模式
- C++中嵌入python程序——参数传递
- python嵌入到C++中
- Python/C API使用方法简介 (在C/C++中嵌入Python)
- Python嵌入C中了
- C/C++中嵌入Python
- C/C++中嵌入Python
- C语言中嵌入Python
- C/C++中嵌入Python
- C中嵌入python问题解答
- 使用Java中的JNI技术将C/C++程序嵌入到Java代码中实现Hello World
- 将Python代码嵌入到C++程序
- [网易云课堂]Linux内核分析(四)—— 使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
- c接口导出到lua中使用
- 【转载】CSS魔法堂:你真的理解z-index吗?
- thinkphp php7 Cannot use 'String' as class name as it is reserved
- Spark算子:RDD行动Action操作(2)–take、top、takeOrdered
- linux下c开发
- 一个Java实现的简单的多个客户端聊天程序(未测试)
- C++中嵌入python程序——使用API接口,从函数到类
- Ajax传递给后台值得做法
- 【HDU】1061 - Rightmost Digit(快速幂)
- 45 个非常有用的 Oracle 查询语句(未测试)
- 数组的clone()方法
- (1)HTML标签详解之开篇语
- linux基本操作
- 最严厉的大数据招标评判标准
- JSP取得绝对路径(未测试)