rad*_*man 7 c++ python import boost boost-python
我正在使用Boost.Python在我的C++可执行文件中嵌入一个解释器并执行一些预先编写的脚本.我有它的工作,所以我可以调用python文件中的函数,但我想使用的python代码导入外部文件,这些导入失败,因为'没有名为'的模块.如果我直接从python运行脚本,一切都按预期工作.
所以我的问题是在通过C++绑定运行的python脚本中导入模块的正确方法是什么?
C++代码:
#include "boost/python.hpp"
int main(int argc, char** argv)
{
try
{
Py_Initialize();
boost::python::object test = boost::python::import("__main__");
boost::python::object testDict = test.attr("__dict__");
boost::python::exec_file("test.py", testDict, testDict);
}
catch(boost::python::error_already_set& e)
{
PyErr_Print();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Python代码:
import ModuleX
Run Code Online (Sandbox Code Playgroud)
rad*_*man 14
事实证明,我的问题是在C++中初始化时模块搜索路径未正确设置的简单情况.
在大多数系统上(特别是在Unix和Windows上,尽管细节略有不同),Py_Initialize()根据对标准Python解释器可执行文件位置的最佳猜测来计算模块搜索路径,假设找到了Python库在相对于Python解释器可执行文件的固定位置.特别是,它查找名为lib/pythonX.Y的目录,该目录相对于父目录,其中在shell命令搜索路径(环境变量PATH)上找到名为python的可执行文件.
所以这意味着模块搜索路径决不会指向当前工作目录,而是指向系统python安装文件夹.
我的解决方案是正确设置模块搜索路径以指向当前工作目录.为此,您需要初始化python,然后提取sys.path值并添加任何其他路径.如果你没有加入,请原谅使用助推器; 你应该能够轻松地看到如何替换所需的任何字符串.
Py_Initialize();
// now time to insert the current working directory into the python path so module search can take advantage
// this must happen after python has been initialised
boost::filesystem::path workingDir = boost::filesystem::absolute("./").normalize();
PyObject* sysPath = PySys_GetObject("path");
PyList_Insert( sysPath, 0, PyString_FromString(workingDir.string().c_str()));
Run Code Online (Sandbox Code Playgroud)