Accessing global namespace from module

Gabriel Genellina gagsl-py2 at
Mon Jun 11 23:10:03 CEST 2007

En Mon, 11 Jun 2007 17:29:35 -0300, reubendb <reubendb at> escribió:

> On Jun 11, 3:30 pm, "Gabriel Genellina" <gagsl-... at>
> wrote:
>> En Mon, 11 Jun 2007 15:18:58 -0300, reubendb <reube... at>  
>> escribió:
>> > The problem is I don't define the functions AddPlot() and DrawPlots().
>> > It's built into the python interpreter of the CLI version of the
>> > program I mentioned, and they are defined on the main script. I load
>> > the main script using something like "software -cli -s
>> >".
>> > In the I import myModule, but of course myModule does
>> > not have access to the functions defined in the global namespace of
>> >
>> Don't you have some import statements at the top of that  
>> are
>> responsible for bringing AddPlot and DrawPlots into the current  
>> namespace?
>> Import the same things in your second module.
> No, I *don't* have any import statement When using this
> software's CLI, AddPlot and DrawPlots are available to me
> automagically from Hence my question: How do I make
> this available from other module. Is there any way at all ?

Yes: create your own module on-the-fly, using the recipe posted earlier by  
John Krukoff.
If there are many functions, try enumerating them all:

import sys
 from types import ModuleType as module

plotModule = module('plot')
for key,value in globals().items():
     if key[:2] != '__':
         setattr(plotModule, key, value)

sys.modules['plot'] = plotModule

Gabriel Genellina

More information about the Python-list mailing list