Import/Create module from buffer

Irmen de Jong irmen.NOSPAM at xs4all.nl
Mon May 12 07:48:28 EDT 2008


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



More information about the Python-list mailing list