如何在boost :: python嵌入式python代码中导入模块? [英] How do I import modules in boost::python embedded python code?

查看:156
本文介绍了如何在boost :: python嵌入式python代码中导入模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用boost :: python将一些python代码嵌入到应用程序中.我能够获得要正确评估的打印语句或其他表达式,但是当我尝试导入模块时,它没有导入并且应用程序正在退出.此外,嵌入式代码中的globals()函数调用也给出了运行时错误.

I'm using boost::python to embed some python code into an app. I was able to get print statements or other expressions to be evaluated properly, but when I try to import modules, it is not importing and application is exiting. Further the globals() function call in the embedded code gives a runtime error too.

#include <boost/python.hpp>

using namespace boost;
using namespace boost::python;
using namespace boost::python::api;

int main(void) {
    Py_Initialize();
    object main_module = import("__main__");
    object main_namespace = main_module.attr("__dict__");
    main_namespace["urllib2"] = import("urllib2");

    object ignored = exec(
            "print 'time'\n", main_namespace);
}

在这里,我尝试使用boost import函数导入urllib2,它可以编译并正常运行,但是使用以下exec语句,它会给出错误.

Here, I've tried to import urllib2 using the boost import function, this compiles and runs properly, but with the following exec statement, it gives an error.

    object ignored = exec(
            "print urllib2\n"
            "print 'time'\n", main_namespace);

或者当我删除boost导入功能并从嵌入式代码中进行导入时,也会出现错误.我尝试使用try:except:块,但这也不起作用.这是因为C ++应用程序无法找到urllib2 py模块的位置吗?有没有办法在尝试导入之前设置模块的路径?

Or when I remove the boost import function and do the import from within the embedded code also, it gives an error. I tried using a try: except: block but that doesn't work either. Is this because the C++ app isn't able to find the location of the urllib2 py module or something? Is there a way to set the path of the module before trying to import?

此代码仅供内部使用,因此可以对路径进行一些硬编码.

This is being built only for internal use, so some hard coding of the paths is acceptable.

更多信息:
这就是发生的情况.我尝试了一个.. catch并在有异常时调用了PyErr_Print(),并且在有模块导入甚至函数调用时始终将其视为错误.错误消息:

More info:
This is what happens. I did a try .. catch and called the PyErr_Print() when ever there is an exception, and got this as error all the time when there are module imports or even function calls. Error message:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: 'NoneType' object does not support item assignment

有人能想到任何原因吗?

Can anyone think of any reason?

推荐答案

那没有帮助,但是我找到了解决问题的另一种方法.我当前的代码如下:

That didn't help, but I found a different solution to my problem. My current code looks like this:

#include <boost/python.hpp>
#include <iostream>

using namespace std;
using namespace boost;
using namespace boost::python;
using namespace boost::python::api;

int main(void) {
        Py_Initialize();
        boost::python::object http = boost::python::import("urllib2");

        try
        {
                boost::python::object response = http.attr("urlopen")("http://www.google.com");
                boost::python::object read = response.attr("read")();
                std::string strResponse = boost::python::extract<string>(read);
                cout << strResponse << endl;
        }
        catch(...)
        {
                PyErr_Print();
                PyErr_Clear();
        }
}

无论如何,谢谢乔纳斯的回答

Anyways, thanks for the answer Jonas

这篇关于如何在boost :: python嵌入式python代码中导入模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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