[C++-sig] How to access a custom module from embedded Python?

Peter python at cityofdreams.com
Sun Apr 11 01:18:55 CEST 2004


Problem solved.  My ignorance of how to use boost.python was to blame.

I realized there is a fn named "inithello" that must be imported:
  PyImport_AppendInittab("hello", inithello);
before 
  Py_Initialize();

and later:
  object strRes(handle<>(PyRun_String(
                "from hello import *\n"
                "result = greet()\n",
                Py_file_input,
                main_namespace.ptr(),
                main_namespace.ptr()) ));

  const char* res 
    = extract<const char*>(main_namespace["result"]);

So simple!
- Peter

> I've just started using boost.python and have embedded a Python
> interpretor in a test app.  But how do I access a Python extension I
> have created in the same app that embeds Python?
> 
> e.g. I define a hello module
> 
>  BOOST_PYTHON_MODULE(hello)
>  {
>      def("greet", greet);
>  }
> 
> and in the same code I embed Python:
> 
>  Py_Initialize();
> 
>  handle<> main_module(borrowed( 
>           PyImport_AddModule("__main__") ));
>  dict main_namespace(handle<>(borrowed( 
>           PyModule_GetDict(main_module.get()) )));
> 
> And am stuck at the next step... how do I call hello.greet() from this
> embedded Python instance?
> 
> Thanks,
> Peter
> 
> 
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
> 
> 







More information about the Cplusplus-sig mailing list