[C++-sig] import module from string

Stefan Seefeld seefeld at sympatico.ca
Thu Feb 22 13:27:46 CET 2007


Воля Овчинников wrote:
> Hi all.
> 
>  
> 
> Sorry for my English.
> 
>  
> 
> 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, get the dictionary, get the function and run it.
> 
> But I don’t want to do it. Is the another way exist?

If I understand what you want correctly, the following (untested !) code
should work:

bpl::object module = bpl::import("your_module");
bpl::dict dict(module.attr("__dict__"));
bpl::object function = dict["your_function"];
function();


HTH,
		Stefan

-- 

      ...ich hab' noch einen Koffer in Berlin...



More information about the Cplusplus-sig mailing list