title: AttributeError: 'module' object has no attribute 'func' Only With C++ Invocation
body: <p>I've been working on integrating a scripting subsystem into an existing architecture and have been employing the use of the boost.python library.</p>
  
  <p>My goal is to have the python environment that I create to be identical, or as close to, that which is expected in existing code - i.e. preserving namespace conventions, etc. The trick here is that I have two source files illustrating a use case that works if I fire up the interpreter standalone but do not work in my environment, meaning I must be doing something wrong. </p>
  
  <p>As a disclaimer I'm not an experienced python user so I'm bound to butcher the correct terminology here.</p>
  
  <h2><b>core.py</b></h2>
  
  <pre><code>import sys
  
  def func():    
      print "sys is",sys
  
  print "call from core"  
  func()
  </code></pre>
  
  <h2><b>main.py</b></h2>
  
  <pre><code>import core
  
  print "---------GLOBALS-------------"
  globals_ = dict(globals())
  for g in globals_:
      print g
  
  print "--------------LOCALS---------"   
  locals_ = dict(locals())
  for l in locals_:
      print l
  
  print "call from main"
  core.func()
  </code></pre>
  
  <p><i>Invoking <code>python main.py</code> from command prompt</i></p>
  
  <pre><code>call from core
  sys is &lt;module 'sys' (built-in)&gt;
  ---------GLOBALS-------------
  core
  __builtins__
  __name__
  __file__
  __doc__
  __package__
  --------------LOCALS---------
  core
  g
  __builtins__
  __file__
  globals_
  __package__
  __name__
  __doc__
  call from main
  sys is &lt;module 'sys' (built-in)&gt;
  </code></pre>
  
  <p>I understand the steps as being:</p>
  
  <ol>
  <li>main invokes <strong>import</strong> to import core.py.</li>
  <li>The core module is loaded; the core namespace and the attributes it provides become accessible from main. In the process, core invokes its func method, printing what is bound to <code>sys</code>.</li>
  <li>main invokes the function bound under the module core.</li>
  </ol>
  
  <p><i>Example of how I bring up the scripting environment:</i></p>
  
  <pre><code>void Run()
  {
      Py_Initialize();
      if(PyErr_Occurred())
      {
          PyErr_Print();
      }
  
      // Install builtins in __main__
      boost::python::object main_module = boost::python::import(ToplevelScope.c_str());
      boost::python::object main_namespace = main_module.attr("__dict__");
      boost::python::dict main_dictionary = boost::python::extract&lt;boost::python::dict&gt;(main_namespace);
      main_dictionary["__builtins__"] =  boost::python::handle&lt;&gt;(boost::python::borrowed(PyEval_GetBuiltins()));
  
      // Load modules
      LoadModule("core.py",GetDataForCore());
      LoadModule("main.py",GetDataForMain());
  }
  
  void LoadModule(std::string name, char* data)
  {
      // First, see if the module already exists.
      PyObject* new_module;
      if( new_module = PyImport_AddModule(name.c_str()) )
      {
          if( PyDict_GetItemString(new_module,"__name__") )
          {
              return; // already loaded. no work to do.
          }
      }
  
      // Initialize default namespace parameters - global, local
      boost::python::dict base_dict = boost::python::dict();
      base_dict["__builtins__"] = boost::python::handle&lt;&gt;(PyImport_AddModule("__builtin__"));
      base_dict["__name__"] = name.c_str();
      base_dict["__file__"] = name.c_str();
      base_dict["__doc__"] = "None";
      base_dict["__package__"] = name.c_str();
  
      boost::python::exec(    data,
                              boost::python::dict(base_dict),  // make a copy of base_dict for both global &amp; local
                              boost::python::dict(base_dict) );
  }
  </code></pre>
  
  <p><i>Resultant output:</i></p>
  
  <pre><code>call from core
  sys is &lt;module 'sys' (built-in)&gt;
  ---------GLOBALS-------------
  core
  __builtins__
  __name__
  __file__
  __doc__
  __package__
  --------------LOCALS---------
  core
  g
  __builtins__
  __file__
  globals_
  __package__
  __name__
  __doc__
  call from main
  [LOG] Framework error while loading script main.py.
  Traceback (most recent call last):
    File "&lt;string&gt;", line 17, in &lt;module&gt;
  AttributeError: 'module' object has no attribute 'func'
  </code></pre>
  
  <p>It appears that the function in core somehow isn't accessible to main even after an import.</p>
  
  <p>I suspect this has to do with me not fully initializing the core module. Replacing exec with PyRun_String, PyImport_ImportModule... and they all yield the same result. Here I'm creating a copy of a base dictionary with <strong>name</strong> set to the module name (core in this case) and supplying it as the global &amp; local dictionaries.</p>
  
  <p>I think that I'm not fully initializing the module somehow and will be doing a lot more reading to put it together... Any insight will be greatly appreciated!!</p>