compiling module from string and put into namespace

bruno at modulix onurb at xiludom.gro
Fri May 12 07:43:31 EDT 2006


glomde wrote:
> Hi,
> 
> I want to create a function that preprocesses a file and then imports
> the parsed file.
> 
> What I found out is that you can do something like this:
> 
> def ImportFile(fileName):
>     parsedCode = Parser(fileName).Parse()
>     module = new.module(name)
>     exec parsedCode in module.__dict__
>     sys.modules[name] = module
>     import name
> 
> But this works only if the ImportFile isnt loaded from a module.
> Since the import is local.

Look for __import__() (it's in the builtins) and sys.modules.

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list