Monday, 25 April 2011

Using python plugin scripts with py2exe

Py2exe is great. It rolls up you python application's dependencies, including the python interpreter and any stdlib or third party libraries you have included, into an executable and a few support files. Your users don't need to install Python on their Windows box, but can still run your Python application. Even if the user has Python installed, they don't need to worry about installed packages, the python version, etc. It just works!

The only problem is that now your python scripts are swallowed up inside an exe or zip file and can't be modified. Sometimes it's useful to be able to modify the behaviour of the app beyond what is convenient to do through a config file.

YAPSY is a simple python plugin system that can be confiured to scan a directory for plugin scripts. A plugin consists of a python script and an accompanying config file. Fortunately I've found that it's possible to create a py2exe project is such a way that the plugins directory and the scripts inside it are still exposed in the py2exe distribution of your app and don't get bundled away out of sight.

Py2exe is configured using a setup.py script. Here's an example, showing how to include the plugin scripts so that they are still accessible.
from distutils.core import setup
import py2exe
 
 
setup(name='StarBase',
      version='0.47',
      author='Simon D. Hibbs',
      windows=[{'script' : 'Starbase.pyw'}],
      options={'py2exe' : {'includes' : ['sip'], 'bundle_files' : '1'}},
      scripts=['log.py'],
      data_files=[('', ['projects.ini']),
                  ('plugins', ['plugins/default.py',
                               'plugins/default.yapsy-plugin',
                               'plugins/alternate.py',
                               'plugins/alternate.yapsy-plugin'])])

Additional plugins can be installed by the user later simply by copying the plugin script and associated yapsy config file into the plugins directory. YAPSY will then load the plugins at runtime.

So now your application is easily distributable, and also scriptable through a nicely implemented plugin system.

1 comment:

  1. Hello Simon,

    It's been over a year since you wrote this. But I have the hope that you can help me.

    I'm working with this like you. But my software doesn't find my plugins directory after packing all with py2exe. Maybe it's necessary do something special for the setpluginsplace code line. My code line for do that is this:

    self.manager.setPluginPlaces(["plugins"])

    ReplyDelete