[C++-sig] import module from string

Hans Meine hans_meine at gmx.net
Thu Feb 22 13:39:18 CET 2007


On Thursday 22 February 2007, Stefan Seefeld wrote:
> > In my c++ code I need to run a function from python module, but I have
> > only a string with module body.
> >
> > Of course, I can save the module body into temporary file, use
> > PyImport_Import, [...]
>
> bpl::object module = bpl::import("your_module");
> bpl::dict dict(module.attr("__dict__"));
> bpl::object function = dict["your_function"];
> function();

If I understood the question correctly, he does not want to import a module, 
but he wants to execute code from a string which defines a function, 
*without* writing that string to disk for the above to work.

Ciao, /  /                                                    .o.
     /--/                                                     ..o
    /  / ANS                                                  ooo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20070222/1379e8fa/attachment.pgp>


More information about the Cplusplus-sig mailing list