嵌入式Python加载模块但不加载该模块的内部导入语句

发布于 2024-10-30 00:56:08 字数 1274 浏览 7 评论 0原文

最后(!)我编译了 Boost::Python 并让我的 XCode 项目导入本地模块。该模块以 from xml.dom import minidom 行开头,但是当它执行时,出现以下错误:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "FeedStore.py", line 1, in <module>
    from xml.dom import minidom
ImportError: No module named xml.dom

然而,我知道我已经安装了 xml Python 模块 -- 当我打开时从命令提示符处输入 Python 并输入 from xml.dom import minidom,一切都很顺利。此外,当我导入模块时,它的行为符合我的预期。

我怀疑 sys.path 有问题,因此我将提示中得到的路径与嵌入式模块中使用的路径进行了比较。唯一的区别是嵌入的 sys.path 不包含 ''。我尝试附加它,但这并没有改变行为。

我还怀疑嵌入式版本访问的 Python 版本与我在提示符下使用的 Python 版本不同,但 sys.prefix 在两次执行之间匹配。

这是导入我的模块并运行它的代码。目前它还很简单(甚至还没有引用计数),因为此时我只想确保我能够嵌入我的模块(我是一个完全新手的 C++ 程序员)。

    Py_Initialize();

    //PyRun_SimpleString("import sys");
    //PyRun_SimpleString("sys.path.append('')"); //tried this to no avail!

    PySys_SetPath("/Users/timoooo/Documents/Code/TestEmbed/"); //this allows me to import my local module


    PyRun_SimpleString("import FeedStore as fs"); //here's where it whines about the lack of xml.dom
    PyRun_SimpleString("store = fs.feedStore()");
    PyRun_SimpleString("print store.next()");

    Py_Finalize();

我可能误解了 boost::python 的一些重要内容。有人可以帮我吗?

At long last(!) I've compiled Boost::Python and have gotten my XCode project to import a local module. This module starts with the line from xml.dom import minidom, but when it executes, I'm given this error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "FeedStore.py", line 1, in <module>
    from xml.dom import minidom
ImportError: No module named xml.dom

However, I know that I've installed the xml Python module -- when I open Python from my command prompt and type from xml.dom import minidom, everything goes smoothly. Moreover, when I import the module, it behaves as I would expect.

I suspected that there was something wrong with sys.path, so I compared the one I get from the prompt to the one that's being used in my embedded module. The only difference is that the embedded sys.path does not include ''. I've tried appending it, but that didn't change the behavior.

I also suspected that the embedded version was accessing a different version of Python than I was using from the prompt, but sys.prefix matched between both executions.

Here's the code that imports my module and runs it. It's pretty bare-bones at the moment (not even reference counting yet) because at this point I'd just like to make sure I'll be able to embed my module (I'm a total newbie C++ programmer).

    Py_Initialize();

    //PyRun_SimpleString("import sys");
    //PyRun_SimpleString("sys.path.append('')"); //tried this to no avail!

    PySys_SetPath("/Users/timoooo/Documents/Code/TestEmbed/"); //this allows me to import my local module


    PyRun_SimpleString("import FeedStore as fs"); //here's where it whines about the lack of xml.dom
    PyRun_SimpleString("store = fs.feedStore()");
    PyRun_SimpleString("print store.next()");

    Py_Finalize();

I'm probably misunderstanding something essential about boost::python. Can anyone help me out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事还在继续 2024-11-06 00:56:08

尽管具有相同的 sys.path 值,但调用
PyRun_SimpleString("sys.path.append(\"<>\")");
我需要的地方解决了问题。

Despite having identical sys.path values, calling
PyRun_SimpleString("sys.path.append(\"<<path>>\")");
with the places I needed fixed the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文