我正在尝试从下面的Objective c代码调用python类函数,这是我的目标c函数。
-(void) hitPython
{
Py_Initialize();
NSLog(@"PyInit");
const char *pypath = [[[NSBundle mainBundle] resourcePath] UTF8String];
// import sys
PyObject *sys = PyImport_Import(PyString_FromString("sys"));
// sys.path.append(resourcePath)
PyObject *sys_path_append = PyObject_GetAttrString(PyObject_GetAttrString(sys, "path"), "append");
PyObject *resourcePath = PyTuple_New(1);
PyTuple_SetItem(resourcePath, 0, PyString_FromString(pypath));
PyObject_CallObject(sys_path_append, resourcePath);
// import MyModule # this is in my project folder
PyObject *myModule = PyImport_Import(PyString_FromString("MyModule"));
// MyModule.my_func()
PyObject *my_func = PyObject_GetAttrString(myModule, "my_func");
if (my_func && PyCallable_Check(my_func)){
PyObject *result = PyObject_CallObject(my_func, NULL);
if(result != NULL){
NSLog(@"Result of call: %s", PyString_AsString(result));
}
}
}
运行代码时出现以下错误。
图像指定编译代码时出现的错误
我从系统库中添加了python框架,但仍然发生相同的错误。