title: What causes Python's __main__ to disappear when embedding with Boost.Python
body: <p>I'm embedding Python in a C++ app. I want to define a function <code>V</code> in the main module which takes a string and returns an instance of a class <code>A</code>.</p>
  
  <p>The problem is that <code>A</code> needs some data available in the instance of the class embedding Python, passed in the example as <code>_env</code> - so I thought I could use def(...) to define <code>V</code> in the method, using a lambda to capture the data required. </p>
  
  <p>However, when I do that, I end up getting <code>AttributeError: 'NoneType' object has no attribute '__dict__'</code> when I try to get the dictionary for the main module.</p>
  
  <p>Without the call to boost's def(...), I'm able to get and add to the main module.</p>
  
  <p>Is there something I'm doing wrong that causes <code>__main__</code> to go missing and produce a None when I try to access it? Any recommendations on how else to achieve this?</p>
  
  <pre><code> void B::processRequest(Ptr&lt;protocol::Message&gt; msg, const std::function&lt;void(const std::string &amp;)&gt; &amp;send) {      
        try {
          //make my_module module available to python
          PyImport_AppendInittab("my_module", &amp;initmy_module);
          //
          Py_Initialize();
          //get the main module
          py::object main_module((py::handle&lt;&gt;(py::borrowed(PyImport_AddModule("__main__")))));
          py::object main_namespace = main_module.attr("__dict__");
          py::object my_moduleNamespace((py::handle&lt;&gt;(PyImport_ImportModule("my_module"))));
          //add the module to the main namespace
          main_namespace["my_module"] = my_moduleNamespace;
  //add attribute to namespace
  //        main_namespace["V"] = py::ptr(this);
          auto AFn([this](std::string label) { return ptr&lt;A&gt;(_env, label); });
          typedef boost::mpl::vector&lt;Ptr&lt;A&gt;, std::string&gt; AFnSig;
          const auto policies = py::default_call_policies();
          py::def("V", boost::python::make_function(AFn, policies, AFnSig()));
  
          py::handle&lt;&gt; ignored((PyRun_String((*msg-&gt;action).c_str(), Py_file_input, main_namespace.ptr(), main_namespace.ptr())));
        } catch (py::error_already_set) {
          PyErr_Print();
        }
        Py_Finalize();
      }
  </code></pre>
  
  <p>The only thing I could think of to work around this was to make <code>B</code> callable defining <code>operator(std::stirng)</code> but that doesn't work because I have two other functions which need <code>_env</code> and one of them as the same signature as <code>V</code> so there'd be no way to distinguish between the calls as far as I can see.</p>
  
  <p><strong>EDIT</strong>:
  Changed title to try to make it clearer as to what I'm referring to.</p>