importing files from a directory

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
 
T

Thomas Guettler

Am Sat, 09 Jul 2005 20:30:04 -0700 schrieb 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. [cut]

for item in dir_list:
# strip off the extensions...
if (item == "__init__.py"):
continue
elif (item[-3:] == '.py'):

item.endswith(".py") would be more python-like.
mod_name = item[:-3]
elif (item[-4:] == '.pyc'):

elif item.endswith(".pyc"):
continue # Don't load module twice
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?

See above: Just don't load it. The compiled "pyc" file is taken
automatically if it is newer than the "py" file.
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))

I use this getattr() after __import__, too. I don't think there
is a easier way.

HTH,
Thomas
 
S

spike grobstein

my reason for loading both the .py and .pyc files was just in case
compiled files were supplied as modules... but I'm gonna disallow that,
so yeah.

I also got a response in email and I've been dabbling with my code
since I posted this and found a slightly better way of handling this
plugin system...

I stuck the import code into the Modules/__init__.py file, so it can
act as a kind of manager (instead of moving the files to a
Modules(disabled) directory) and appended the __import__s to an array.

like this:

spike@flaphead ~/Aphex $ cat Modules/__init__.py
module_list = []

def load_module(mod_name):
mod = __import__("Modules.%s" % mod_name)
mod = getattr(mod, mod_name)
module_list.append(mod.module())

def load_modules():
load_module("nes")
load_module("snes")
load_module("mame")

load_modules()

[end code]

I then just have to 'import Modules' from my main program and the whole
module import thing is encapsulated and invisible to my main program.

I'm gonna add some functions to the Modules module to make fetching
plugins a little less ambiguous (get_module(index) and
get_named_module(module_name), etc).

Thanks for the help. Python's making me have to think a little
backwards. I have to make sure I declare my functions before I call
them, but it's a very cool language overall.

....spike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top