[英]“No module named site” when embedding python in c++ in Visual studio
[英]Python Embedding in C++ : ImportError: No module named 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。
将以下内容放在 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/")
您可能可以在解释器中或在 C 级别使用 python 来完成。 这将以与PYTHONPATH
非常相似的方式添加搜索路径,但它更加持久和优雅(恕我直言)。
嗨所有面临同样问题的人,我找到了解决方案! setenv() 是 stdlib.h 中定义的函数,用于设置环境变量。 只需要运行它!
setenv("PYTHONPATH",".",1);
有关 setenv 的更多信息:
$ man setenv
一切顺利:) 另外,感谢@spinus
您也可以尝试将这些代码包含在您的 c 程序中
Py_Initialize();
PyObject *sys = PyImport_ImportModule("sys");
PyObject *path = PyObject_GetAttrString(sys, "path");
PyList_Append(path, PyUnicode_FromString("."));
从这里学习
如果 python 文件没有导入任何额外的 python 库,spinus 提供的解决方案就可以工作。
但是,如果一个 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-library 不起作用:
PyObject *pNumpy = PyUnicode_FromString("numpy");
PyObject *pModuleA = PyImport_Import(pNumpy);
有人知道如何从依赖于其他一些 python 库的 C python 函数调用吗?
对于遇到此问题的其他人:
您确定您的 .py 文件位于 C++可执行文件所在的同一目录中吗?
我在 CLion 中编程并忘记了可执行文件位于 cmake-build-debug 中。 所以我在项目目录中添加了 .py 文件,毫不奇怪我得到了同样的错误 ImportError。 我将 .py 文件放置到 cmake-build-debug (默认情况下可执行文件在那里),使用了这个问题的答案,一切正常!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.