473,326 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 1071
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Jeff Wagner | last post by:
Is there a Python module or method that can convert between numeric bases? Specifically, I need to convert between Hex, Decimal and Binary such as 5Ah = 90d = 01011010b. I searched many places...
14
by: Luc Mazardo | last post by:
The following code works with g++ 2.75 but i've some troubles with compiling with g++ 3.0. Thanks in advance if you have a solution. Here is the error message. main.hpp:28: specializing...
0
by: David W. Fenton | last post by:
Today I was working on a hideous old app that I created a long time ago that does a lot of showing/hiding/resizing of fields on one of the forms. I had used constants to store reference values for...
4
by: Aaron Queenan | last post by:
When I build a C++ library to .NET using the managed C++ compiler, I get the following error message: Linking... LINK : error LNK2020: unresolved token (0A000005) _CrtDbgReport LINK : error...
0
by: phoolimin | last post by:
Dear all, I am trying to embed python into another scripting language, to do this I need to solve a number of problems on importing or compiling python script. First let me state what exactly I...
3
by: Mitko Haralanov | last post by:
For various reason, what I need to do is be able to send some Python code (mostly entire functions in the form of a string) to a remote server (written in Python), have that server compile the code...
1
by: CeciMar | last post by:
I'm new to vb.net and I have a very simple console application. When I attempt to compile it at the command prompt I get error BC30420 Sub Main not found. I know the namespace and module name but...
0
by: Gabriel Genellina | last post by:
En Sat, 12 Jul 2008 16:15:36 -0300, Akathorn Greyhat <akathorn@gmail.com> escribi�: Welcome! You have to pass in the namespace of the desired module - instead of globals. I'd use an...
6
by: msb_6 | last post by:
Currently I have a PHP extension thats all written and compiles under windows, but the PC I'm going to end up putting it on is running Ubuntu 8.04 (g++ 4.2.3). I've delved into PHP documentation...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.