[wingide-users] global imports

Paul Du Bois dubois at doublefine.com
Thu Jun 9 15:54:46 MDT 2011

> # ----- Module Initialization ----------
> def fnInitSubroutines(sC,sE,sP):
>     exec "from " + sC + " import *"     # Configuration of test
>     exec "from " + sE + " import *"     # Lists of user Equipment
>     exec "from " + sP + " import *"     # Lists of user test
>     globals().update(locals())

If you want to stick with the exec-based solution, read the docs about
the optional "in <dict>" syntax.  It allows you to avoid calling
locals() and update().  locals() in particular is potentially quite
expensive in some implementations.
    exec "from foo import *" in globals()

Earlier you wrote:

>> No they are NOT the names.  They are the modules themselves.
> That's too bad.

It's slightly more pythonic (and avoids the need for the rather advanced
"exec") if you pass in the modules themselves instead of names:

def fnInitSubroutines(C, E, P):
  """Pass in modules whose contents are copied into the global
  for m in (C, E, P):
   for name,val in m.__dict__.iteritems():
     if not n.startswith('__'):
       globals()[name] = val

import myC, myE, myP
import subroutines
subroutines.init(myC, myE, myP)

More information about the wingide-users mailing list