Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  C语言  >  正文 C程序调用Python脚本的例子

C程序调用Python脚本的例子

发布时间:2018-10-05   编辑:www.jquerycn.cn
jquery中文网为您提供C程序调用Python脚本的例子等资源,欢迎您收藏本站,我们将为您提供最新的C程序调用Python脚本的例子资源
下面我们一起来看看关于C程序调用Python脚本的例子,如果你对此有兴趣的希望此教程对各位同学会有所帮助。
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6926')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6926>

#include <python2.7/Python.h>
int main(){
PyObject * g_pModule = NULL;
Py_Initialize(); //使用python之前,要调用Py_Initialize();这个函数进行初始化
if (!Py_IsInitialized())
{
printf("init errorn");
return -1;
}
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
g_pModule =PyImport_ImportModule("test");//这里是要调用的文件名,我们这里是当前目录下test.py
if (!g_pModule) {
printf("Cant open python file!n");
return -2;
}
PyObject * test1 = PyObject_GetAttrString(g_pModule,"test1");//这里是要调用的函数名
PyObject *objResult = PyObject_CallFunction(test1,"i,s",2,e);//调用函数
if (!objResult){
printf("invoke function failn");
}
 
PyObject * test2= PyObject_GetAttrString(g_pModule,"test2");//这里是要调用的函数名
objResult = PyObject_CallFunction(test2,"i",2);//调用函数
char * x = PyString_AsString(objResult);
printf("%sn",x);
Py_Finalize();//调用Py_Finalize,这个跟Py_Initialize相对应的。
}

Python程序test.py

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2813')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2813>

def test1(s,str):
print s str
return 0
def test2(s):
return s

C程序的编译方法

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3590')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3590>

#假设我们的python编译的时候安装在/opt/python里,那么我们可以用这样的命令来编译程序
$gcc -I/opt/python/include -L/opt/python/lib/ -lpython2.7 test.c

注意: 这里要求python编译的时候,需要有动态链接库即加上--enable-shared

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6378')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6378>

./configure --prefix=/opt/python  --enable-shared

您可能感兴趣的文章:
python语言的特点是什么
学python要学django吗
cython与python的不同有哪些
使用Pdb调试Python方法总结
如何用python写脚本
python是一种什么类型的编程语言
Python 基础语法
python语言难学吗
C程序调用Python脚本的例子
Python 环境搭建

[关闭]