Supply a plugin interface
R. David Murray
rdmurray at bitdance.com
Thu Apr 23 08:06:20 EDT 2009
Ulrich Eckhardt <eckhardt at satorlaser.com> wrote:
> Johannes Bauer wrote:
> > What I'd like to add: I want the GUI users to supply plugin scripts,
> > i.e. offer some kind of API. That is, I want the user to write short
> > Python pieces which look something like
> >
> > import guiapp
> >
> > class myplugin():
> > def __init__(self):
> > guiapp.add_menu("foobar")
> >
> > def exec(self, param):
> > print("foo")
> >
> > the GUI application should now browse the plugin directory and read
> > those plugin python files and somehow incorporate (i.e. discover what
> > modules are there, instanciate, etc.)
>
> You will find ways to scan a directory using os.path and related things. In
> order to import a module, you add the base directory to sys.path. Since you
> have just a variable with the module name (excluding the .py), you can't
> use 'import' as it is. Instead, use the __import__ functio (I wonder if
> there is anything more elegant) which returns the plugin.
In 2.7 and 3.1 there will be:
from importlib import import_module
myname = import_module('somename')
which has much more sensible semantics than __import__ does.
--
R. David Murray http://www.bitdance.com
More information about the Python-list
mailing list