0

终于(!)我编译了 Boost::Python 并让我的 XCode 项目导入了一个本地模块。该模块以 line 开头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 版本与我从提示中使用的版本不同,但 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 的一些重要内容。谁能帮我吗?

4

1 回答 1

1

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

于 2011-04-07T02:19:12.580 回答