> -----Original Message----- > From: [EMAIL PROTECTED] [mailto:python- > [EMAIL PROTECTED] On Behalf Of Reuben D. > Budiardja > Sent: Monday, June 11, 2007 7:19 PM > To: python-list@python.org > Subject: Re: Accessing global namespace from module > > On Monday 11 June 2007 17:10:03 Gabriel Genellina wrote: > > En Mon, 11 Jun 2007 17:29:35 -0300, reubendb <[EMAIL PROTECTED]> > escribió: > > > On Jun 11, 3:30 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> > > > > > > wrote: > > >> En Mon, 11 Jun 2007 15:18:58 -0300, reubendb <[EMAIL PROTECTED]> > > >> > > >> 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 > > >> > mainscript.py". > > >> > In the mainscript.py I import myModule, but of course myModule does > > >> > not have access to the functions defined in the global namespace of > > >> > mainscript.py. > > >> > > >> Don't you have some import statements at the top of mainscript.py > 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 mainscript.py. When using this > > > software's CLI, AddPlot and DrawPlots are available to me > > > automagically from mainscript.py. 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 > > Great ! That seems to work, thanks a lot. > One last question. Do I have to do this for ever script I write, or can I > put > this into separate file and "include" it somehow ? > I am going to have several mainscripts.py, and all is going to import > myModule > that will need access to this plots subroutine. It'll be great if I can > put > this trick on a single file that is included by the main scripts, to avoid > violating DRY principle. > > Thanks for all the help. > RDB > -- > Reuben D. Budiardja > -- > http://mail.python.org/mailman/listinfo/python-list
Well, an alternative way to access the main script namespace is using: >>> import __main__ >>> __main__.AddPlot( blah, blah ) And so on, from within your imported file that you want to have muck about in the main namespace. I've no idea if your custom application will setup __main__ properly, but the documentation indicates that it should work the same way for an embedded application. You can probably substitute dir( __main__ ) for globals( ) in the above script and have it set things up automatically. --------- John Krukoff [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list