Mass importing of a template based system.. Trouble with name substitutions

Robert Kern rkern at ucsd.edu
Wed Aug 3 23:25:46 EDT 2005


rh0dium wrote:
> Hi all,
> 
> Basically I have a bunch of pluggins in a directory (METDIR).  For each
> one of these templated pluggins I want to do a specific routine. Let's
> start with a basic template
> 
> file example1.py
> ----------------
> class example1:
>    def __init__(self):
>       print "Initialize"
>    def run(self):
>       print "Hi from example 1"
> ----------------
> 
> file example2.py
> ----------------
> class example2:
>    def __init__(self):
>       print "Initalize"
>    def run(self):
>       print "example 2"
> ----------------
> 
> Now I want to go through each pluggin ( example1.py and example2.py )
> and execute run.  So here is my code but it doesn't work..
> 
> 
> if os.path.isdir(METDIR):
>    modules = []
> 
>    # Add the metrics dir toyour path..
>    sys.path.insert( 0, os.getcwd() + "/" +  METDIR )
> 
>    for metric in glob.glob(METDIR+"/*.py"):
>       # Now lets start working on the individual metrics
>       module_name, ext = os.path.splitext(os.path.basename(metric))
>       try:
>          module = __import__(module_name)
>          modules.append( module )
>       except ImportError , e:
>          print "Failed import of  %s - %s" % ( module_name, e)
>          pass
> 
>    for mod in modules:
>       a = mod.mod()
>       a.run()
> 
> But it doesn't work with the following..
> 
> Traceback (most recent call last):
>   File "./metriX.py", line 109, in main
>     a = mod.mod()
> AttributeError: 'module' object has no attribute 'mod'
> 
> So it looks like it's not doing the substitution.

Doing what substitution? Neither of the modules that you showed define a 
mod() callable and nothing else seems to add one.

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter




More information about the Python-list mailing list