By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,188 Members | 1,710 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,188 IT Pros & Developers. It's quick & easy.

Accessing global namespace from module

P: n/a
Hello,
I am new to Python. I have the following question / problem.
I have a visualization software with command-line interface (CLI),
which essentially is a Python (v. 2.5) interpreter with functions
added to the global namespace. I would like to keep my own functions
in a separate module and then import that module to the main script
(that will be executed using the CLI interpreter). The problem is, I
cannot access the functions in the global namespace of the main script
from my module. Is there anyway to do that ?

Here is an example of what I meant. The function AddPlot() and
DrawPlots() are added to the global namespace by the software CLI. If
I do this:

mainscript.py:
---------------------------
AddPlot("scatter", "coordinate")
# set other things here
DrawPlots()

it works fine. But I want to be able to do this:

myModule.py:
----------------------
def defaultScatterPlot():
AddPlot("scatter", "coordinate")
#do other things
DrawPlots()

and then in mainscript.py:
---------------------------------------
import myModule
myModule.defaultScatterPlot()

This won't work because myModule.py doesnot have access to AddPlot().
How do I do something like this ?

Thank you in advance for any help.
RDB

Jun 11 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
On Jun 11, 11:02 am, reubendb <reube...@gmail.comwrote:
Hello,
I am new to Python. I have the following question / problem.
I have a visualization software with command-line interface (CLI),
which essentially is a Python (v. 2.5) interpreter with functions
added to the global namespace. I would like to keep my own functions
in a separate module and then import that module to the main script
(that will be executed using the CLI interpreter). The problem is, I
cannot access the functions in the global namespace of the main script
from my module. Is there anyway to do that ?

Here is an example of what I meant. The function AddPlot() and
DrawPlots() are added to the global namespace by the software CLI. If
I do this:

mainscript.py:
---------------------------
AddPlot("scatter", "coordinate")
# set other things here
DrawPlots()

it works fine. But I want to be able to do this:

myModule.py:
----------------------
def defaultScatterPlot():
AddPlot("scatter", "coordinate")
#do other things
DrawPlots()

and then in mainscript.py:
---------------------------------------
import myModule
myModule.defaultScatterPlot()

This won't work because myModule.py doesnot have access to AddPlot().
How do I do something like this ?

Thank you in advance for any help.
RDB
I think you're doing it backwards. If you want access to AddPlot, then
you should import mainscript into that module instead of the other way
around. When I have common methods I want to call from different
scripts, I put those methods/functions into their own module/file.
Then I just import the module and call whatever script I need.

<code>

commonMods.py
---------------------
AddPlot(*args, *kwargs):
# Do something
DrawPlots(*args, *kwargs):
# Do something
---------------------
mainProgram.py
------------------------
from commonMods import AddPlot
AddPlot("scatter", "coordinate")
# etc etc
-------------------------

myModule.py
------------------------
from commonMods import AddPlot
AddPlot("scatter", "coordinate")
# etc etc
-------------------------

</code>

Hope that helps.

Mike

Jun 11 '07 #2

P: n/a
On Jun 11, 1:37 pm, kyoso...@gmail.com wrote:
On Jun 11, 11:02 am, reubendb <reube...@gmail.comwrote:
Hello,
I am new to Python. I have the following question / problem.
I have a visualization software with command-line interface (CLI),
which essentially is a Python (v. 2.5) interpreter with functions
added to the global namespace. I would like to keep my own functions
in a separate module and then import that module to the main script
(that will be executed using the CLI interpreter). The problem is, I
cannot access the functions in the global namespace of the main script
from my module. Is there anyway to do that ?
<snip>

I think you're doing it backwards. If you want access to AddPlot, then
you should import mainscript into that module instead of the other way
around. When I have common methods I want to call from different
scripts, I put those methods/functions into their own module/file.
Then I just import the module and call whatever script I need.

<code>

commonMods.py
---------------------
AddPlot(*args, *kwargs):
# Do something
DrawPlots(*args, *kwargs):
# Do something
---------------------
Hi Mike,
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.

Thanks.
RDB

Jun 11 '07 #3

P: n/a
En Mon, 11 Jun 2007 15:18:58 -0300, reubendb <re******@gmail.comescribió:
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.

--
Gabriel Genellina

Jun 11 '07 #4

P: n/a
En Mon, 11 Jun 2007 15:18:58 -0300, reubendb <re******@gmail.comescribió:
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.

--
Gabriel Genellina

Jun 11 '07 #5

P: n/a
On Jun 11, 3:30 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Mon, 11 Jun 2007 15:18:58 -0300, reubendb <reube...@gmail.comescribió:
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 ?

Thanks.
RDB

Jun 11 '07 #6

P: n/a
En Mon, 11 Jun 2007 17:29:35 -0300, reubendb <re******@gmail.comescribió:
On Jun 11, 3:30 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
>En Mon, 11 Jun 2007 15:18:58 -0300, reubendb <reube...@gmail.com>
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


--
Gabriel Genellina

Jun 11 '07 #7

P: n/a
En Mon, 11 Jun 2007 17:29:35 -0300, reubendb <re******@gmail.comescribió:
On Jun 11, 3:30 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
>En Mon, 11 Jun 2007 15:18:58 -0300, reubendb <reube...@gmail.com>
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


--
Gabriel Genellina

Jun 11 '07 #8

P: n/a
On Monday 11 June 2007 17:10:03 Gabriel Genellina wrote:
En Mon, 11 Jun 2007 17:29:35 -0300, reubendb <re******@gmail.comescribió:
On Jun 11, 3:30 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>

wrote:
En Mon, 11 Jun 2007 15:18:58 -0300, reubendb <reube...@gmail.com>

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
Jun 12 '07 #9

P: n/a
En Mon, 11 Jun 2007 22:19:15 -0300, Reuben D. Budiardja
<te******@pathfinder.phys.utk.eduescribió:
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.
According to your description on how things work, you will need the
globals() from mainscript.py; try this:

--- mainscript.py ---
import plot_setup
plot_setup.setup(globals())
.... rest of script, perhaps importing myModule.py ...

--- plot_setup.py ---
import sys
from types import ModuleType as module

def setup(namespace):
plotModule = module('plot')
for key,value in namespace.items():
if key[:2] != '__':
setattr(plotModule, key, value)
sys.modules['plot'] = plotModule

--- myModule.py ---
import plot

def do_work():
plot.DrawPlot(...)
...

(Having to type two lines at the top of your main scripts doesn't look so
bad...)

--
Gabriel Genellina

Jun 12 '07 #10

P: n/a
On Monday 11 June 2007 23:49:14 Gabriel Genellina wrote:
En Mon, 11 Jun 2007 22:19:15 -0300, Reuben D. Budiardja
<snip>
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.

According to your description on how things work, you will need the
globals() from mainscript.py; try this:
Hi Gabriel,
This works great ! Thanks a lot (and to John Krukoff too).

RDB
--
Reuben D. Budiardja
Dept. Physics and Astronomy
University of Tennessee, Knoxville, TN
Jun 12 '07 #11

P: n/a
On Monday 11 June 2007 23:49:14 Gabriel Genellina wrote:
En Mon, 11 Jun 2007 22:19:15 -0300, Reuben D. Budiardja
It'll be great if I can
put
this trick on a single file that is included by the main scripts, to
According to your description on how things work, you will need the
globals() from mainscript.py; try this:
--- plot_setup.py ---
import sys
from types import ModuleType as module

def setup(namespace):
plotModule = module('plot')
for key,value in namespace.items():
if key[:2] != '__':
setattr(plotModule, key, value)
sys.modules['plot'] = plotModule
Hm.. apparently this works for function but doesn't work for method :(. Do you
know of any reason ?

Thanks.
RDB
--
Reuben D. Budiardja
Dept. Physics and Astronomy
Jun 12 '07 #12

P: n/a
En Tue, 12 Jun 2007 18:37:45 -0300, Reuben D. Budiardja
<te******@pathfinder.phys.utk.eduescribió:
>import sys
from types import ModuleType as module

def setup(namespace):
plotModule = module('plot')
for key,value in namespace.items():
if key[:2] != '__':
setattr(plotModule, key, value)
sys.modules['plot'] = plotModule

Hm.. apparently this works for function but doesn't work for method :(.
Do you
know of any reason ?
Do you mean, it doesn't work for classes? What kind of error do you get?

--
Gabriel Genellina

Jun 13 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.