title: How do I import modules in boost::python embedded python code?
body: <p>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.</p>
  
  <pre><code>#include &lt;boost/python.hpp&gt;
  
  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);
  }
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>    object ignored = exec(
              "print urllib2\n"
              "print 'time'\n", main_namespace);
  </code></pre>
  
  <p>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?</p>
  
  <p>This is being built only for internal use, so some hard coding of the paths is acceptable.</p>
  
  <p><em>Edit: More info:</em> <br/>
  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:</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;string&gt;", line 1, in &lt;module&gt;
  TypeError: 'NoneType' object does not support item assignment
  </code></pre>
  
  <p>Can anyone think of any reason?</p>