importing files from a directory

spike grobstein spike666 at mac.com
Mon Jul 11 11:14:47 EDT 2005


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 at 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




More information about the Python-list mailing list