HOW TO Create new module from source text

Oren Tirosh oren-py-l at hishome.net
Sat Aug 10 14:45:03 CEST 2002


On Fri, Aug 09, 2002 at 11:08:34PM -0500, Chris Gonnerman wrote:
> I know I've done this before but darned if I can figure out
> how.  I need to load a block of source text and convert it
> into a module.  I can't just use import because the name 
> being imported may match a builtin module.

import new, sys
mod = new.module('modname')
exec sourcestring in mod.__dict__
sys.modules['modname'] = mod

You can also use execfile(filename, mod.__dict__) instead of exec.

	Oren




More information about the Python-list mailing list