[Q] How to get global namespace of __main__?

Emile van Sebille emile at fenx.com
Sun Oct 21 03:17:23 CEST 2001


I think this does it:


#getglobals.py
def gg():
    import sys
    return sys.modules['__main__'].__dict__

Then, interactively:

>>> reload(getglobals)
<module 'getglobals' from 'f:\python21\getglobals.py'>
>>> gg = getglobals.gg()
>>> gg
{'gg': {...}, '__doc__': None, 'getglobals': <module 'getglobals' from
'f:\pytho
n21\getglobals.py'>, '__name__': '__main__', '__builtins__': <module
'__builtin_
_' (built-in)>, 'sys': <module 'sys' (built-in)>}
>>> globals()
{'gg': {...}, '__doc__': None, 'getglobals': <module 'getglobals' from
'f:\pytho
n21\getglobals.py'>, '__name__': '__main__', '__builtins__': <module
'__builtin_
_' (built-in)>, 'sys': <module 'sys' (built-in)>}
>>>


HTH,

--

Emile van Sebille
emile at fenx.com

---------
"Daehyok Shin" <sdhyok at email.unc.edu> wrote in message
news:9qt5cj$bef$1 at news2.isis.unc.edu...
> For a specialized task of my own, I have to get a global namespace of
python
> interactive environment from a function in a module. The following example
> simplified it.
> How can I solve the problem? Thanks in advance.
>
> Inside python interactive environment:
> >from mymod import put
> >x = globals()
> >put()
>
> Inside mymod module:
> def put():
>    y = ???? #y should be the same as x.
>
> --
> Daehyok Shin (Peter)
>
>
>




More information about the Python-list mailing list