Using import to create a "plugin" architecture
Posted on 2004-11-04
I have a hairy problem that I'm working on. I'd appreciate any help that you can offer.
I'm currently trying to build a "plugin" system using Python. It would operate something like this.
A directory would hold a number of python modules.
Each module would define one function and a string giving an english language description of the function's purpose.
A function in the main program would loop over the files in the directory, importing each module.
The description of the function would be a dictionary key, with a reference to the function as it's value.
In my head it looks something like this:
plugins = dict()
plugins = [x.strip(".py") for x in os.listdir(pluginDir) if x.endswith(".py")]
for x in plugins:
exec("import %s" % (x))
plugins[MODULE_NAME_FROM_IMPORT.description] = MODULE_NAME_FROM_IMPORT.function
I really have no idea how to make something like this actually work, though. Any suggestions?