如何与Boost.Python的进口工作从Python文件内 [英] How does import work with Boost.Python from inside python files

查看:156
本文介绍了如何与Boost.Python的进口工作从Python文件内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Boost.Python的嵌入在我的C ++可执行跨preTER并执行一些prewritten脚本。我有它的工作,这样我可以调用函数在Python文件,但蟒蛇code我想用进口外部文件和这些进口失败,因为没有模块名为'。但是,如果我直接从蟒蛇一切正常运行脚本预期。

所以我的问题是什么是正在通过C ++绑定运行在进口Python脚本模块的正确方法?

C ++ code:

 的#include助推/ python.hppINT主(INT ARGC,字符** argv的)
{
  尝试
  {
    Py_Initialize();
    提高::蟒蛇::对象测试=提振::蟒蛇::进口(__ main__);
    提高::蟒蛇::对象testDict = test.attr(__ dict__);
    提高::蟒蛇:: exec_file(test.py,testDict,testDict);  }
  赶上(升压::蟒蛇:: error_already_set急症)
  {
    PyErr_Print();
  }
返回0;}

Python的code:

 进口MODULEX


解决方案

所以,事实证明,我的问题是从C ++初始化时没有正确设置模块搜索路径的简单情况。

从Python文档介绍:


  

在大多数系统(特别是在Unix和Windows,虽然
  细节略有不同),Py_Initialize()计算模块
  搜索路径根据其最佳猜测为标准的位置
  Python的跨preTER可执行文件,假设Python库是
  在相对固定的Python的跨preTER一个位置找到
  可执行文件。特别是,它会寻找一个名为
  相对于父目录的lib / pythonX.Y位置的可执行文件
  名为Python是shell命令搜索路径上找到(该
  环境变量PATH)。


那么,这就意味着模块搜索路径是决不以任何方式在当前的工作目录指向,而它指向系统蟒蛇安装文件夹中。

对我来说,解决办法是正确设置模块搜索路径在当前的工作目录指向。要做到这一点,你需要初始化蟒蛇然后提取的sys.path值,添加任何额外的路径。请问使用升压如果你没有成;你应该能够很容易地看到如何替换任意字符串所需。

  Py_Initialize();//现在时间插入当前工作目录到Python路径,以便模块搜索可以利用
// Python已经被初始化后,这一定会发生
提高::文件系统::路径workingDir =的boost ::文件系统::绝对(./)归()。
*的PyObject = SYSPATH PySys_GetObject(路径);
PyList_Insert(SYSPATH,0,PyString_FromString(workingDir.string()c_str()));

I am using Boost.Python to embed an interpreter in my C++ executable and execute some prewritten scripts. I have got it working so that I can call functions in the python file but the python code I want to use imports external files and these imports fail because 'no module named '. If I run the script directly from python everything works as expected however.

So my question is what is the correct way of importing modules in python scripts that are being run via C++ bindings?

C++ Code:

#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;

}

Python Code:

import ModuleX

解决方案

So it turns out that my problem is a simple case of the module search path not being set correctly when initialised from within C++.

From the Python Documentation intro:

On most systems (in particular, on Unix and Windows, although the details are slightly different), Py_Initialize() calculates the module search path based upon its best guess for the location of the standard Python interpreter executable, assuming that the Python library is found in a fixed location relative to the Python interpreter executable. In particular, it looks for a directory named lib/pythonX.Y relative to the parent directory where the executable named python is found on the shell command search path (the environment variable PATH).

So what this means is that the module search path is in no way set to point at the current working directory, rather it points at the system python install folder.

The solution for me was to correctly set the module search path to point at the current working directory. To do this you need to initialise python and then extract the sys.path value and add any additional paths. Excuse the use of boost if you're not into that; you should be able to easily see how to substitute any string desired.

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()));

这篇关于如何与Boost.Python的进口工作从Python文件内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆