C++中的Python嵌入:ImportError:没有名为pyfunction的模块
你好,我正在尝试把Python(2.7)嵌入到C++(g++ 4.8.2)中,这样我就可以从C++调用Python函数。这是Python文档中提供的嵌入基本代码:
这是我的文件 call_function.cpp:
#include <Python.h>
int main(int argc, char *argv[])
{
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
int i;
if (argc < 3) {
fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
return 1;
}
/* char pySearchPath[] = "/usr/include/python2.7";
Py_SetPythonHome(pySearchPath);*/
Py_Initialize();
pName = PyString_FromString(argv[1]);
/* Error checking of pName left out */
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (pModule != NULL) {
pFunc = PyObject_GetAttrString(pModule, argv[2]);
/* pFunc is a new reference */
if (pFunc && PyCallable_Check(pFunc)) {
pArgs = PyTuple_New(argc - 3);
for (i = 0; i < argc - 3; ++i) {
pValue = PyInt_FromLong(atoi(argv[i + 3]));
if (!pValue) {
Py_DECREF(pArgs);
Py_DECREF(pModule);
fprintf(stderr, "Cannot convert argument\n");
return 1;
}
/* pValue reference stolen here: */
PyTuple_SetItem(pArgs, i, pValue);
}
pValue = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pArgs);
if (pValue != NULL) {
printf("Result of call: %ld\n", PyInt_AsLong(pValue));
Py_DECREF(pValue);
}
else {
Py_DECREF(pFunc);
Py_DECREF(pModule);
PyErr_Print();
fprintf(stderr,"Call failed\n");
return 1;
}
}
else {
if (PyErr_Occurred())
PyErr_Print();
fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
}
Py_XDECREF(pFunc);
Py_DECREF(pModule);
}
else {
PyErr_Print();
fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
return 1;
}
Py_Finalize();
return 0;
}
现在我的Python脚本保存为 pyfunction.py,放在与 call_function.cpp 相同的文件夹里。
这是 pyfunction.py:
def multiply(a,b):
print "Will compute", a, "times", b
c = 0
for i in range(0, a):
c = c + b
return c
现在我在终端中调用:
$ g++ call_function.cpp -I/usr/include/python2.7 -lpython2.7 -o call_function
(编译成功,没有任何错误)
(运行程序)
$ ./call_function pyfunction multiply 2 3
(我遇到了这个错误):
ImportError: No module named pyfunction
Failed to load "pyfunction"
我不明白这怎么可能。我已经按照文档操作,但仍然出现错误。
为什么它找不到 pyfunction.py,明明它就在同一个目录里。
6 个回答
spinus提供的解决方案在Python文件没有导入任何额外的Python库时是有效的。
但是,如果一个Python文件导入了其他库,比如说numpy,上面的代码就会崩溃,具体情况如下:
:~/programs/python$ ./a.out myModule multiply 4 3
Traceback (most recent call last):
File "/home/a/programs/python/myModule.py", line 1, in <module>
import numpy
ImportError: No module named 'numpy'
Failed to load "myModule"
需要注意的是,从C语言中导入Python库是行不通的:
PyObject *pNumpy = PyUnicode_FromString("numpy");
PyObject *pModuleA = PyImport_Import(pNumpy);
有没有人知道如何从C语言调用依赖于其他Python库的Python函数呢?
你也可以尝试把这些代码加到你的C程序里
Py_Initialize();
PyObject *sys = PyImport_ImportModule("sys");
PyObject *path = PyObject_GetAttrString(sys, "path");
PyList_Append(path, PyUnicode_FromString("."));
想了解更多,可以从这里学习
大家好,遇到同样问题的朋友们,我找到了解决办法!
setenv() 是一个在 stdlib.h 里定义的函数,它用来设置环境变量。只需要运行这个函数就可以了!
setenv("PYTHONPATH",".",1);
想了解更多关于 setenv 的信息,可以查看这里:
$ man setenv
祝大家好运 :)
另外,感谢 @spinus 的帮助!
在C/C++代码中,把下面的内容放在Py_Initialize();
之后。
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append(\".\")");
试试这个方法:
$ PYTHONPATH=. ./call_function pyfunction multiply 2 3
如果这个方法不行,可以在这个文件夹里创建一个 __init__.py
文件,然后再试一次。
更新:
我觉得 PYTHONPATH
只是个临时解决办法,用来测试一些东西。如果你想要一个文件夹来放所有你用到的模块,你需要在你的嵌入式解释器里做一些类似下面的设置:
import sys
sys.path.insert(0, "./path/to/your/modules/")
你可以在你的解释器里用 Python 或者在 C 语言层面来做到这一点。这会添加一个搜索路径,和 PYTHONPATH
的效果很像,但我觉得它更持久、更优雅。