C/C++调用python

49 篇文章 0 订阅
3 篇文章 0 订阅

程序配置:
python3.5
VS2012


Python2.x 也可以参考

1、环境配置
参考:http://blog.csdn.net/chunleixiahe/article/details/50410208这篇文章有详细介绍

2、第一个程序(调用无参函数)

使用到的Python程序:

# hello.py
# coding:utf-8

import numpy as np

def 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-8
import numpy as np

def 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-8
import numpy as np

def 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、总结

暂时先写这么多,以后还会不定期更新!

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值