Plugins in Python
Hans Nowak
wurmy at earthlink.net
Fri Jun 28 00:16:15 EDT 2002
Lars Yencken wrote:
> Hello,
>
> I'm trying to do plugins for a program I'm writing in Python, and I'm not
> sure how exactly to do it. Basically what I want it to be able to write a
> module which loads every python file in a directory and stores an instance
> of each one that loaded successfully.
>
> I tried using exec() and compile() to run each of the files in the
> directory from my Plugins module, and had each of the files import my
> Plugins module and store an instance of themselves there, but at the end of
> it they weren't stored.
>
> Any ideas?
One way to do this is making a subdirectory and using it as a package. For
example, a structure like this:
main.py
plugins/
__init__.py
foo.py
bar.py
If you want all the plugin modules imported, you can simply do this in
__init__.py (import foo, import bar, etc). Then all you have to do from a
program that uses the plugins, is import the package:
import plugins
plugins.bar.baz()
# call baz() function in module bar in plugins directory
HTH,
--
Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA=='))
# decode for email address ;-)
The Pythonic Quarter:: http://www.awaretek.com/nowak/
More information about the Python-list
mailing list