How to import a module so that the current globals are available to the module?

Scott David Daniels Scott.Daniels at Acm.Org
Thu Apr 9 18:17:55 EDT 2009


mrstevegross wrote:
> I'm trying to import a module so that the globals() of the importer
> module are available to the imported module itself....

> One awkward solution is to deliberately initialize mymod.py ....
> That will work, but it's a bit ugly. Plus, I have to repeat it for
> every module with the same problem (of which I have many!).

Nope.  Think about it.  Only the first import is anything but a dict
lookup.
Even if you know how you want that to work, suppose you use two
different modules in a single program, both of which define a
"builtin" named "somename".  Does the first one win? Does the second?
Does the last that actually imported?  There is a hack that can let
you change the builtins module, but you don't want to use it: you make
your programs harder to understand.
Accept that you may want a bit of boilerplate if you have a common
base, and start each use with something like:

     from base_functions import bibble, babble, bobble, bome

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list