compiling module from string and put into namespace

G

glomde

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
 
B

bruno at modulix

glomde said:
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.
 
G

glomde

Tanks but that isn't what I am looking for.
I really want a function that you can call and imports
the module into the calling functions namespace.

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

Edward Elliott

glomde said:
Tanks but that isn't what I am looking for.
I really want a function that you can call and imports
the module into the calling functions namespace.

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

come again?
<type 'builtin_function_or_method'>
 
B

Bruno Desthuilliers

glomde a écrit :
Tanks but that isn't what I am looking for.
I really want a function that you can call and imports
the module into the calling functions namespace.

Please read the documentation of the __import__() *function*. (notice
the double leading and ending underscores and the parenthesis).
I dont want the caller to call import but a function.

Please read the documentation of the __import__() *function*. (notice
the double leading and ending underscores and the parenthesis).
 
G

glomde

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

come again?

<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?
 
G

glomde

Ok, now I think I know what I need to do.
I need to create a variable in the calling functions locals.
So how do I get access to the calles locals dictionary?
Is it possible?
 
E

Edward Elliott

glomde said:
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.

Ok got it now. I'm sure it's doable but it sounds tricky. Wouldn't it be
easier to just have your function return a list of modules to the caller
and have the caller import them?
 
F

Fuzzyman

glomde said:
Ok, now I think I know what I need to do.
I need to create a variable in the calling functions locals.
So how do I get access to the calles locals dictionary?
Is it possible?

If your import is used in the current namespace, just use the global
keyword.

Best is if your function just returns the module though, and then you
have bound your name to it exactly as if you had used import.

If you *really* want to poke it into the globals of whereever your
function is called from, you can use :

sys._getframe(1).globals['name'] = name

(or something like that, you will need to look up the semantics.)

This is a hack really though, just return the module object and bind
the right name to it where you call it from.

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top