compiling module from string and put into namespace

glomde tbrkic at yahoo.com
Fri May 12 06:28:09 EDT 2006


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.

So this wouldnt work:

from XXX import ImportFile

ImportFile(fileName)
fileName.Function()

Since the import in ImportFile isnt visible the module that calls it.
So how do I do so that the import is visible from callers namespace?

Best regards,

T




More information about the Python-list mailing list