Import/Create module from buffer

Gruik benjamin.fremiot at gmail.com
Mon May 12 09:13:25 EDT 2008


On May 12, 1:48 pm, Irmen de Jong <irmen.NOS... at xs4all.nl> wrote:
> Gruik wrote:
> > But before that 1 question: what if I'm in Python ?
> > Following your solution, I did that in Python :
>
> >     def load_buffer(buffer) :
> >         compiled_buffer = compile(buffer, "module_name", "exec")
> >         exec(compiled_buffer)
>
> > It works great except that I can't have a module object and that it is
> > as if I did "from module import *"
> > But I need the module object and not an "import *" behavior.
> > Any idea about the way to do that?
>
> Something along the lines of:
>
> import new
> mymodule = new.module("mymodule")
> exec <<<code>>> in mymodule.__dict__
>
> --irmen

Yeah it works !

    exec(compiled_module, globals(), mymodule.__dict__)

Just to add mymodule to sys.modules and it's good!

Thanks again
Benjamin



More information about the Python-list mailing list