Can module access global from __main__?

Steve Holden steve at
Tue Oct 11 17:18:58 CEST 2005

Neal Becker wrote:
> Everything you said is absolutely correct.  I was being lazy.  I had a main
> program in module, and wanted to reorganize it, putting most of it into a
> new module.  Being python, it actually only took a small effort to fix this
> properly, so that in, what were global variables are now passed as
> arguments to class constructors and functions.
> Still curious about the answer.  If I know that I am imported from __main__,
> then I can do access X as sys.modules[__main__].X.  In general, I don't
> know how to determine who is importing me.
I don't think you can without huge amounts of introspection - it's even 
worse than the "what's the name of this object" question that seems to 
come up regularly. A module can be imported from multiple modules, and 
you only get to execute code on the first import. Even then (on the 
first import) I am not sure how you could introspect to find the answer 
you want.

Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC           
PyCon TX 2006        

More information about the Python-list mailing list