title: Passing a C++ instance into Python outside of BOOST_PYTHON_MODULE macro
body: <p>I want to pass an instance of TestObj from the C++ code into python. The code posted here produces the error in cout: "No to_python (by-value) converter found for C++ type: class TestObj". If I move the object creation and <code>main_module.attr("obj") = obj;</code> into the BOOST_PYTHON_MODULE macro, the code runs fine.</p>
  
  <p>Similar things happen when I try passing a <code>*TestObj</code> with or without boost::ptr.</p>
  
  <p>testembed.py:</p>
  
  <pre><code>import sfgame
  
  print("old x: " + str(obj.x))
  obj.x = 10
  print("new x: " + str(obj.x))
  </code></pre>
  
  <p>testobj.h</p>
  
  <pre><code>class TestObj{
  public:
      TestObj();
      int x;
      int getX();
      void setX(int xv);
  };
  </code></pre>
  
  <p>testobj.cpp</p>
  
  <pre><code>#include "TestObj.h"
  TestObj::TestObj(){
  }
  
  int TestObj::getX(){
      return x;
  }
  
  void TestObj::setX(int xv){
      x = xv;
  }
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;boost/python.hpp&gt;
  #include "TestObj.h"
  
  using namespace boost::python;
  
  BOOST_PYTHON_MODULE(sfgame){
      class_&lt;TestObj&gt;("TestObj")
          .add_property("x", &amp;TestObj::getX, &amp;TestObj::setX)
          ;
  }
  
  int main(){
      Py_Initialize();
  
      object main_module = import("__main__");
      object main_namespace = main_module.attr("__dict__");
  
      TestObj obj;
  
      try{
          obj.setX(5);
          main_module.attr("obj") = obj;
  
          exec_file("testembed.py", main_namespace);
      }
      catch (const boost::python::error_already_set &amp;){
          PyObject *ptype, *pvalue, *ptraceback;
          PyErr_Fetch(&amp;ptype, &amp;pvalue, &amp;ptraceback);
          std::string error;
          error = boost::python::extract&lt;std::string&gt;(pvalue);
          std::cout &lt;&lt; error &lt;&lt; std::endl;
      }
  
      system("PAUSE");
      return 0;
  }
  </code></pre>