HOW TO Create new module from source text

Chris Gonnerman chris.gonnerman at
Sat Aug 10 10:42:12 EDT 2002

----- Original Message ----- 
From: "Oren Tirosh" <oren-py-l at>

> 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.

Thank you!  This is precisely what I needed!

Chris Gonnerman -- chris.gonnerman at

More information about the Python-list mailing list