How to load plugins as objects?

Fredrik Lundh fredrik at pythonware.com
Sat May 28 09:24:48 EDT 2005


"censored" <censored at dontemailme.com> wrote:

> Hi. I'm hoping for a bit of programming guidance here ....
>
> I have an application I wrote (it's an irc bot fyi), with which I want to
> use plugins.
>
> Basically, I want there to be directory called plugins/ .
>
> In plugins, there should be a bunch of *.py files. Each file should
> contain a single class.
>
> When the application launches, I want the interpreter to scan the
> plugins/ dir for *.py files, and load each class in each every *.py file
> as an object an object in memory.
>
> I want it to so that every time I want the app to perform a new function,
> I just write a plugin and stick it in the plugins/ directory.
>
> What's the easiest way to go about this?

    for file in glob.glob("plugins/*.py"):
        ns = {}
        execfile(file, ns)
        for name, object in ns.items():
            # pick out interesting objects

</F>






More information about the Python-list mailing list