怎样把Python代码嵌入到C程序

Python013

怎样把Python代码嵌入到C程序,第1张

步骤1:安装Python开发包

由于需要访问Python/C API,首先安装Python开发包。

在Debian,Ubuntu或Linux Mint中:

在CentOS,Fedora或RHEL中:

安装成功后,Python头文件在/usr/include/python2.7。根据Linux发行版的不同,确切的路径可能是不相同的。例如,CentOS 6中是/usr/include/python2.6。

步骤2:初始化解释器并设置路径

C中嵌入Python的第一步是初始化Python解释器,这可以用以下C函数完成。

初始化解释器后,需要设置你的C程序中要导入的Python模块的路径。例如,比如你的Python模块位于/usr/local/modules。然后使用以下C函数调用来设置路径。

步骤3:数据转换

C中嵌入Python最重要的方面之一是数据转换。从C中传递数据到Python函数,需要首先将数据从C数据类型转换到Python数据类型。Python/C API提供各种函数来实现这。例如,转换C字符串到Python字符串,使用PyString_FromString函数。

另外一个类似函数PyInt_FromLong,将C中long数据类型转换为Python int。每个Python/C API函数返回一个PyObject类型的引用。

步骤4:定义一个Python模块

当你想嵌入Python代码到另一种语言如C,该代码需要被写成Python模块,然后用另一种语言“导入”。所以让我们来看看如何在C中导入Python模块。

为了进行说明,我们实现一个简单的Python模块例子如下:

以上的Python函数有一个字符串作为参数并返回两个重复的字符串。例如,如果输入字符串是“cyberpersons”,该函数返回'cyberpersonscyberpersons'。此模块文件命名为“printData.py”并将它放在前面声明的Python模块目录中(/usr/local/modules)。

步骤5:加载一个Python模块

现在你已经定义了Python模块,是时候在C程序中加载它了。导入模块的C代码看起来像这样:

步骤6:构建函数的参数

当加载一个模块时,可以调用模块中定义的Python函数。通常,我们需要传递一个或多个参数到一个Python函数。我们必须构建一个Python元组对象,它包括Python函数中的参数。

在我们的例子中,printData函数定义带一个参数的模块。因此,我们构建一个大小是一的Python元组对象如下。我们可以使用PyTuple_SetItem设置元组对象的每个项。

我们已经成功构建一个参数传递到函数调用,是时候从C程序调用python函数了。

步骤7:调用Python函数

一旦成功创建Python元组对象作为函数参数,我们可以调用一个带参数的Python函数。为此,通过使用PyObject_GetAttrString首先获得模块中定义的函数的引用,然后使用PyObject_CallObject调用该函数。例如:

步骤8:错误检查

避免运行时错误的常见方法是检查函数的返回值并根据返回值采取适当的行动。类似于C程序中的全局变量errno,Python/C API提供一个全局指示符,它报告最后发生的错误。当Python/C API函数失败,全局指示符设置为指示错误,并且PyErr_Print可以用于显示相应的人类可读的trackback。例如:

在你的应用程序中,你可以轻松地将各种错误检查。

这里是完整的C程序,它如本教程描述的嵌入Python代码。

步骤9:编译和执行

保存以上代码到finalCode.c,并且链接Python库(-lpython2.7)编译该代码。根据发行版的不同,可能使用不同的版本(例如,-lpython2.6)。

嵌入

与python的扩展相对,嵌入是把Python解释器包装到C的程序中。这样做可以给大型的,单一的,要求严格的,私有的并且(或者)极其重要的应用程序内嵌Python解释器的能力。一旦内嵌了Python,世界完全不一样了。

C调用python中的函数:

hw.py:

#coding=utf8

def hw_hs(canshu):

return canshu

if __name__ == "__main__":

ccss = "I am hw"

print hw_hs(ccss)

helloWorld.py:

#coding=utf8

import hw

def hello():

ccss = "I am helloWorld"

return hw.hw_hs(ccss)

if __name__ == "__main__":

print hello()

testcpypy.c:

//#include "testcpypy.h"

#include <Python.h>

#include <stdio.h>

#include <stdlib.h>

int main()

{

//初始化Python

Py_Initialize()

if (!Py_IsInitialized()) {

printf("Py_Initialize")

getchar()

return -1

}

//执行python语句

PyRun_SimpleString("import sys")

PyRun_SimpleString("sys.path.append('./')")

PyObject *pModule = NULL

PyObject *pFunc = NULL

PyObject *reslt =NULL

//载入python模块

if(!(pModule = PyImport_ImportModule("helloWorld"))) {

printf("PyImport_ImportModule")

getchar()

return -1

}

//查找函数

pFunc = PyObject_GetAttrString(pModule, "hello")

if ( !pFunc || !PyCallable_Check(pFunc) )

{

printf("can't find function [hello]")

getchar()

return -1

}

//调用python中的函数

reslt = (PyObject*)PyEval_CallObject(pFunc, NULL)

//printf("function return value : %d\r\n", PyInt_AsLong(reslt))

//将python返回的对象转换为C的字符串

char *resltc=NULL

int res

res = PyArg_Parse(reslt, "s", &resltc)

if (!res) {

printf("PyArg_Parse")

getchar()

return -1

}

printf("resltc is %s", resltc)

getchar()

//释放内存

Py_DECREF(reslt)

Py_DECREF(pFunc)

Py_DECREF(pModule)

//关闭python

Py_Finalize()

return 0

}

编译:

gcc -o testcpypy testcpypy.c -IC:\Python27\include -LC:\Python27\libs -lpython27---C:\Python27为python安装目录

或:

gcc -c testcpypy.c -IC:\Python27\include

gcc -o testcpypy.exe testcpypy.o -LC:\Python27\libs -lpython27

执行结果:

带参数的情况:

#include "callpydll.h"

#include "Python.h"

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <stdarg.h>

int callhello(char *instr, char *outstr)

{

PyObject *pModule = NULL

PyObject *pFunc = NULL

PyObject *reslt = NULL

PyObject *pParm = NULL

char *resltc = NULL

int resltn

int res

char *helloWorld = "TestIM_ProtocBuf"

char *im_account = "aaaa"

char *auth_code = "aaaa"

char *im_uid = "aaaa"

char *proxy_topic = ""

//初始化Python

Py_Initialize()

if (!Py_IsInitialized()) {

printf("Py_Initialize")

getchar()

return -1

}

//执行python语句

PyRun_SimpleString("import sys")

PyRun_SimpleString("sys.path.append('./')")

//载入python模块

if(!(pModule = PyImport_ImportModule(helloWorld))) {

printf("PyImport_ImportModule")

getchar()

return -2

}

//查找函数

pFunc = PyObject_GetAttrString(pModule, "login_proxy_body_serialize")

if ( !pFunc || !PyCallable_Check(pFunc) )

{

printf("can't find function [hello]")

getchar()

return -3

}

//参数转换C -->python, 参数必须是元组(一个参数也是,否则会失败!!!坑啊)

pParm = Py_BuildValue("(ssss)", im_account, auth_code, im_uid, proxy_topic)

//调用python中的函数

reslt = (PyObject*)PyEval_CallObject(pFunc, pParm)

//将python返回的对象转换为C的字符串

res = PyArg_ParseTuple(reslt, "si", &resltc, &resltn)

if (!res) {

printf("PyArg_Parse")

getchar()

return -4

}

printf("resltn is %d", resltn)

memcpy(outstr, resltc, strlen(resltc)+1)

//释放内存

Py_DECREF(reslt)

Py_DECREF(pFunc)

Py_DECREF(pModule)

Py_DECREF(pParm)

//关闭python

Py_Finalize()

return 0

}

int main() {

int i

char *dais = "iammain"

char res[10240]

memset(res,'\0',sizeof(res))

i = callhello(dais, res)

if(0 != i) {

printf("Notify:error")

getchar()

return -1

}

printf("result is %s", res)

getchar()

return 0

}

1. 安装Python开发包 由于需要访问Python/C API,首先安装Python开发包。 在Debian,Ubuntu或Linux Mint中: 在CentOS,Fedora或RHEL中: 安装成功后,Python头

2. 初始化解释器并设置路径 C中嵌入Python的第一步是初始化Python解释器,这可以用以下C函数完成。 初始化解释器后,需要设置你的C程序中要导入的Python模块

3. 数据转换 C中嵌入Python最重要的方面之一是数据转换。