compiling module from string and put into namespace

glomde tbrkic at yahoo.com
Sat May 13 14:45:32 EDT 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.

Example:

file1

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
namespace!!!!!

file2

import file1

file1.ImportFile(fileName)
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
to
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