469,623 Members | 1,020 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

compiling module from string and put into namespace

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

May 12 '06 #1
8 997
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 'o****@xiludom.gro'.split('@')])"
May 12 '06 #2
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.

May 12 '06 #3
glomde wrote:
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 (__builtins__.__import__)

<type 'builtin_function_or_method'>
--
Edward Elliott
UC Berkeley School of Law (Boalt Hall)
complangpython at eddeye dot net
May 12 '06 #4
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).

May 13 '06 #5
> 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?

May 13 '06 #6
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?

May 13 '06 #7
glomde wrote:
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?

--
Edward Elliott
UC Berkeley School of Law (Boalt Hall)
complangpython at eddeye dot net
May 13 '06 #8

glomde wrote:
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

May 13 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Jeff Wagner | last post: by
14 posts views Thread by Luc Mazardo | last post: by
reply views Thread by phoolimin | last post: by
3 posts views Thread by Mitko Haralanov | last post: by
reply views Thread by Gabriel Genellina | last post: by
6 posts views Thread by msb_6 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.