How to write python plug-ins for your own python program?

Terry Hancock hancock at anansispaceworks.com
Thu Mar 3 10:27:05 CET 2005


On Wednesday 02 March 2005 11:28 pm, Tian wrote:
> I am writing an audio game using Python. in this game you can apply
> some sound effects for the clips you have recorded. I want to make this
> function extensible. I want user to be able to add new sound effect
> plug-ins in the future.
> 
> I want the plug-in to be a simple python code (text file) and a
> description file. I will set some rules for plug-in writing (like you
> must inherit some class and implement some method). I hope plugin can
> be added while original program is running. Is there any good method to
> read in python code and test availability and invoke the functions
> inside?

It's not hard to scrape the contents of a directory and import any modules
you find (i.e. to implement a "plugin directory").  I do it here (beware line-wrap):

http://cvs.sourceforge.net/viewcvs.py/narya-project/Narya1/VarImage/Operators/__init__.py?rev=1.1&view=markup

You might not want to have the reading code in the same directory as the
plugins -- you don't have to do it that way.  Take a look at the __import__()
built-in in the Python Library Reference for more information.

I'm sure it's possible to run this code periodically at runtime, although I
prefer to do it only at startup (what if one of the plugins is faulty and
crashes the program?).

Cheers,
Terry

-- 
--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks  http://www.anansispaceworks.com




More information about the Python-list mailing list