Using import to create a "plugin" architecture

Posted on 2004-11-04
Last Modified: 2013-11-18
Hello all,
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:

def load_plugin_funcs(pluginDir):
    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
    return plugins

I really have no idea how to make something like this actually work, though.  Any suggestions?

Question by:subject42
    LVL 14

    Accepted Solution

    The key pieces you're misssing are new.module() to create a new module, and execfile() to execute the plugin code in the context of such a newly-created module.

    Here's a full working example.  It has this structure:

    ee-plugins          - top-level directory - the main script that loads and runs the plugins
       plugins           - the directory in which the plugins live
        - an example plugin
        - another example plugin

    *** ****

    import os, new

    def load_plugin_funcs(pluginDir):
        plugins = dict()
        pluginNames = [x.strip(".py") for x in os.listdir(pluginDir) if x.endswith(".py")]
        for x in pluginNames:
            pluginModule = new.module(x)
            execfile(os.path.join(pluginDir, x+'.py'), pluginModule.__dict__)
            plugins[pluginModule.__doc__] = pluginModule.function
        return plugins

    if __name__ == '__main__':
        plugins = load_plugin_funcs('plugins')
        for description, function in plugins.items():
            print description, function()

    *** ***

    """This is plugin one."""

    def function():
        return "One"

    *** ***

    """This is plugin two."""

    def function():
        return "Two"

    *** end ***

    Run and you should see the following output:

    This is plugin one. One
    This is plugin two. Two

    Author Comment

    You rock my world.
    LVL 14

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
    I made this because I wanted to get e-mail with a attached csv file so I'd would be able to import user input into a MS Excel template, but I also wanted to register/save all inputs from each day in a file on the server. 1st - It creates a temp C…
    Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
    Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now