Exception TypeError: 'argument list must be a tuple'

Exception TypeError: 'argument list must be a tuple'

我正在尝试从 cpp 调用一个简单的 python 函数。

下面是cpp代码:

#include <iostream>
#include <Python.h>
#include <numpy/arrayobject.h>
using namespace std;

void init_numpy()
{
    import_array();
}

int main()
{
char dir[500];
cout << "Enter directory: \n";
cin >> dir;

Py_Initialize();

const char *scriptDirectoryName = dir; 
PyObject *sysPath = PySys_GetObject("path");
PyObject *path = PyString_FromString(scriptDirectoryName);
int result = PyList_Insert(sysPath, 0, path);
PyObject *pModule = PyImport_ImportModule("mytest");

init_numpy();
double getItem[2] ;

getItem[0] = getItem[1] = 2;

npy_intp dims = 2 ;

PyObject* arrayToPass =  PyArray_SimpleNewFromData(1, &dims, NPY_DOUBLE , (void *)getItem);

PyObject* myFunction = PyObject_GetAttrString(pModule,(char*)"stuff");

//PyObject* args = PyTuple_Pack(1,PyFloat_FromDouble(getItem));
PyObject* myResult = PyObject_CallObject(myFunction, arrayToPass);
//double resultasd = PyFloat_AsDouble(myResult);
//cout << resultasd << endl;

    Py_Finalize();
    return 0;
}

这是我的 python 代码: mytest.py

def stuff(a):
   x = a[1]
   return x

这只是对我正在编写的更大补丁的测试,但方法是相同的。

我收到这个错误:

Exception TypeError: 'argument list must be a tuple' in <module 'threading' from '/usr/lib/python2.7/threading.pyc'> ignored

我搜索了多个话题,但每个人都有一个独特的错字或错误。

对我做错的地方有什么建议吗?

我编译为:

g++ -I /usr/include/python2.7/   che.cpp -lpython2.7 -o linkArr

来自文档:

PyObject* PyObject_CallObject(PyObject *callable, PyObject *args) Return value: New reference. Call a callable Python object callable, with arguments given by the tuple args. If no arguments are needed, then args can be NULL.

Return 调用成功的结果,或抛出异常,失败时 return NULL。

这相当于 Python 表达式:callable(*args).

函数 PyObject_CallObject(...) 需要元组作为参数。

另请查看 and 以获得更多说明。