Python plug-in

Terry Hancock hancock at anansispaceworks.com
Fri Mar 31 11:17:53 EST 2006


toto wrote:

>I'm trying to find some howto, tutorial in order to create a python program
>that will allow plug-in programming. I've found various tutos on how to
>write a plug-in for soft A or soft B but none telling me how to do it in my
>own programs. Do you have any bookmarks ?
>  
>
There is more than one way to accomplish this, but one of the
simplest is to provide a directory where plugins are loaded, and
put an __init__.py in it which automatically finds files in the directory
that conform to some standard, and imports them (or tries to).

Here's a snippet from one of my projects:

import sys, os
from Operators import Operator, operate, Ops

# Find and load all available plugin modules:

operator_path = os.path.abspath(__path__[0])
for module_file in filter(
    lambda n: n[-3:]=='.py' and n not in ('__init__.py', 'Operators.py'),
    os.listdir(operator_path)):
    #print "Loading %s" % module_file
    f, e = os.path.splitext(module_file)
    __import__(f, globals(), locals(), [])


(Operators.py is in the same directory and includes general purpose
code that the plugins use -- I think it might be better design to put
that in the parent directory. But that's awkward until Python
introduces relative import notation -- supposed to be coming in v2.5).




More information about the Python-list mailing list