[wingide-users] global imports

Mclam, GeorgeX georgex.mclam at intel.com
Wed Jun 8 14:33:55 MDT 2011


Hi Paul.

Thank you for the reply.

> I believe you may already have a list of names you want to steal from
> the modules; I presume your current import statement in subroutines.py
> looks like
>
> from <something> import w,x,y,z, ...

Nope.  My subroutines.py file presently looks something like:
# subroutines.py
from project_conn import *
from project_eset import *
from project_parm import *

subroutines does not know what is being imported and needs everything.
Objects are added all the time to projects.  The idea is to avoid any
editing of subroutines (the variables are in the project files, not
subroutines).


> No they are NOT the names.  They are the modules themselves.

That's too bad.  My list of objects is simply too large and varies too
much to call out each and every item individually.

I am looking to make things easier than what I presently have but w/o
the need to have so many copies of subroutines.  As soon as I have to 
create a list of all the objects, it makes no sense.  It would be one
thing if the list was final, but it is constantly changing.


Thank you for your suggestions.  I can see there are other ways to 
accomplish what the present import (or from) statements do.  I still 
feel this should be a common situation for anyone who wants to re-use
the same code over and over w/o complexities or constant editing.

George



-----Original Message-----
From: Paul Du Bois [mailto:dubois at doublefine.com] 
Sent: Wednesday, June 08, 2011 12:25 PM
To: Mclam, GeorgeX; wingide-users at wingware.com
Subject: RE: [wingide-users] global imports

> >> def init(a, b, c):
> >>  for module in a,b,c:
> >>    globals().update(module.__dict__)
> 
> Hmmm.... so a, b & c are the names of modules that I want to "import",
correct?
> If so, this just might work.  I'll have to check the docs on this one.

No they are NOT the names.  They are the modules themselves.  Re-read
the original code snippet:

# project_main.py
import parameter1, parameter2, parameter3
import subroutines
subroutines.init(parameter1, parameter2, parameter3)

I believe you may already have a list of names you want to steal from
the modules; I presume your current import statement in subroutines.py
looks like

from <something> import w,x,y,z, ...

If you have that list, you can do something like this instead of
stealing the entire contents of the module:

def init(param_module):
   for name in ('w', 'x', 'y', 'z'):
      globals()[name] = getattr(param_module, name) 

modules are just like any other object in this respect.

p 


More information about the wingide-users mailing list