[C++-sig] import module from string
Stefan Seefeld
seefeld at sympatico.ca
Thu Feb 22 14:43:00 CET 2007
Hans Meine wrote:
> 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.
Ah, so I was misled by the use of 'module' in the OP.
In that case, boost::python::exec() should do the job.
(Sorry, can't paste a link to an example, as sourceforge appears to be down
right now :-( )
HTH,
Stefan
--
...ich hab' noch einen Koffer in Berlin...
More information about the Cplusplus-sig
mailing list