python调用函数报错_c++调用python函数报错 ?报错-问答-阿里云开发者社区-阿里云...

目标:用C++程序调用python模块里面的函数

python 模块: testm.py

函数:testm

具体实现如下:

python 模块代码

def testm(a,b=2):

c=a+b

return c

c++部分代码

#include"Python.h"

#include

#include

using namespace std;

void main()

{

bool c;

int a = 0;

Py_Initialize();

PyRun_SimpleString("print('hellow')");

PyObject*modle = NULL;

PyObject*func = NULL;

PyObject*parm = NULL;

PyObject*result=NULL;

modle = PyImport_ImportModule("testm");

if (!modle)

{

cout << "wrong pyfile" << endl;

system("pause");

}

func = PyObject_GetAttrString(modle, "testm");

if (!func)

{

cout << "wrong func" << endl;

system("pause");

}

parm = Py_BuildValue("i", 10);

result = PyEval_CallObject(func, parm);

PyArg_Parse(result, "i", &a);

cout << a << endl;

Py_DECREF(parm);

Py_DECREF(func);

Py_Finalize();

system("pause");

}

报错内容,a的值还是0,其余部分正常。

问题如下:

第一个,a为什么不是102;

第二个,能不能检验传递参数是否传递成功

第三个,如何传递列表与获得返回列表

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值