compiling module from string and put into namespace

glomde tbrkic at
Sat May 13 20:45:32 CEST 2006

> I dont want the caller to call import but a function.

come again?

>>> type (__builtins__.__import__)

<type 'builtin_function_or_method'>

I didnt mean that  __import__ isnt a function, but that I want to
make a function called ImoprtFile that actually does something
very similar that what __import__.

So to rephrsase the questin how does __import__ load a module
into the callers namespace.



def ImportFile(fileName):
    parsedCode = Parser(fileName).Parse()
    module = new.module(name)
    exec parsedCode in module.__dict__
    sys.modules[name] = module
    import name #!!!!!!! This doesn't work. Imports in file1


import file1

fileName.function()  #This wont work because the import happened
locally in file1!!!!!!!!!!!!!

Now the import in file1 doesnt take effect in file2. So what do I have
do to make that work. And I dont want to do a custom hook to import.
So how does __import__ do?

More information about the Python-list mailing list