如何从Qt C++项目运行Python函数?

2024-04-26 01:00:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我遵循了一些教程和建议,甚至使用PyRun_SimpleString运行了一个没有问题的函数。 我有一个名为tomograph.py的文件,其中有一个我想要执行的makeFiles(path,alpha,n,l)函数,还有一个我设法运行的my_fun(x)函数(正如我已经提到的)。你知道吗

<>这是我在Qt C++项目

中所做的
Py_Initialize();

PyObject* moduleString = PyString_FromString((char*)"tomograph");
PyObject* module = PyImport_Import(moduleString);

PyObject* function = PyObject_GetAttrString(module,(char*)"makeFiles");
PyObject* args = PyTuple_Pack(4,filename,alpha,n,l);

//PyRun_SimpleString("my_fun(5)");

PyObject* myResult = PyObject_CallObject(function,args);

Py_Finalize();

当我运行这个代码时,我在

PyObject* function = PyObject_GetAttrString(module,(char*)"makeFiles");

我做错什么了?你知道吗


Tags: 函数pyalphamyfunctionpyrunmodulepyobject