S
spike grobstein
I'm a total Python newbie, so bear with me here...
I'm writing a program that has a user-configurable, module-based
architecture. it's got a directory where modules are stored (.py files)
which subclass one of several master classes.
My plan is to have the program go into the folder called "Modules" and
load up each file, run the code, and get the class and append it to an
array so I can work with all of the objects later (when I need to
actually use them and whatnot).
What I'm currently doing is:
import os
print "Loading modules..."
mod_dir = "Modules"
module_list = [] #empty list...
dir_list = os.listdir(mod_dir)
for item in dir_list:
# strip off the extensions...
if (item == "__init__.py"):
continue
elif (item[-3:] == '.py'):
mod_name = item[:-3]
elif (item[-4:] == '.pyc'):
mod_name = item[:-4]
else:
continue
print "Loading %s..." % mod
module_list.append(__import__("Modules.%s" % mod))
print "Done."
it works more or less like I expect, except that...
A. the first time it runs, blah.py then has a blah.pyc counterpart.
When I run the program again, it imports it twice. Not horrible, but
not what I want. is there any way around this?
B. module_list winds up consisting of items called 'Modules.blah' and
I'd like to have just blah. I realize I could say:
my_module = __import__("Modules.%s" % mod)
module_list.append(getattr(my_module, mod))
but...
is there a better way to accomplish what I'm trying to do?
tia.
....spike
I'm writing a program that has a user-configurable, module-based
architecture. it's got a directory where modules are stored (.py files)
which subclass one of several master classes.
My plan is to have the program go into the folder called "Modules" and
load up each file, run the code, and get the class and append it to an
array so I can work with all of the objects later (when I need to
actually use them and whatnot).
What I'm currently doing is:
import os
print "Loading modules..."
mod_dir = "Modules"
module_list = [] #empty list...
dir_list = os.listdir(mod_dir)
for item in dir_list:
# strip off the extensions...
if (item == "__init__.py"):
continue
elif (item[-3:] == '.py'):
mod_name = item[:-3]
elif (item[-4:] == '.pyc'):
mod_name = item[:-4]
else:
continue
print "Loading %s..." % mod
module_list.append(__import__("Modules.%s" % mod))
print "Done."
it works more or less like I expect, except that...
A. the first time it runs, blah.py then has a blah.pyc counterpart.
When I run the program again, it imports it twice. Not horrible, but
not what I want. is there any way around this?
B. module_list winds up consisting of items called 'Modules.blah' and
I'd like to have just blah. I realize I could say:
my_module = __import__("Modules.%s" % mod)
module_list.append(getattr(my_module, mod))
but...
is there a better way to accomplish what I'm trying to do?
tia.
....spike