Search code examples
boostimportboost-pythonpython-embedding

Embedded Python loads module but does not load that module's internal import statements


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?


Solution

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