开发者社区> 问答> 正文

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;
第二个,能不能检验传递参数是否传递成功
第三个,如何传递列表与获得返回列表

展开
收起
爱吃鱼的程序员 2020-06-23 01:22:06 1173 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    http://blog.csdn.net/marising/article/details/2845339

    http://www.cnblogs.com/linxr/archive/2011/07/22/2113843.html

    2020-06-23 01:22:22
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载