Running JOSM with support for plugins written in Python

In order to run plugins written in Python, a Jython jar has to be on the classpath at startup time.

Here's how to start JOSM:
$  java -classpath "/your/josm.jar:/your/jython.jar" org.openstreetmap.josm.gui.MainApplication
jython.jar isn't included in the scripting plugin. The scripting plugin can download it for you, just go to Scripting -> Configure..., or you can download it manually.

Configuring Python support

In the preferences you can configure
  1. sys paths, i.e. directories and jar files where jython is looking for Python packages, modules, and classes
  2. plugins, the names of Python plugins which shall be loaded at startup time

Writing a Python Plugin

Here's a simple example of a Python plugin for JOSM.
#
# A simple Python plugin which does basically nothing. It only prints
# a message when one of the callback methods is invoked by JOSM.
#
from org.openstreetmap.josm.plugins.scripting.python import JosmPythonPlugin

class HelloWorldPlugin(JosmPythonPlugin):
    def onLoad(self):        
        print "onLoad: starting ..."
        
    def onMapFrameChanged(self, oldFrame, newFrame):
        print "onMapFrameChanged:"
        print "   old frame is: ", oldFrame
        print "   new frame is: ", newFrame 
The plugin must inherit from
org.openstreetmap.josm.plugins.scripting.python.JosmPythonPlugin

Deploying a Python Plugin

as source files

If you have plugin class MyPythonPlugin in the source file my_plugin.py then
  1. make sure the parent directory of my_plugin.py is on the sys paths (how?)
  2. make sure you add the plugin name my_plugin.MyPythonPlugin to the list of plugins (how?)

as jar file

You can create a jar file which includes the python source files required for a plugin.

If you have a plugin class MyPythonPlugin in the source file my_plugin.py, you can create a jar archive as follows:
$ jar cvf my_plugin.jar my_plugin.py # add other python modules required by your plugin
Here's a typical layout of a jar for a Python plugin:
my_plugin.jar
  +--   my_plugin.py
  +--   other_module.py
  +--   my_package_1
          +-- yet_another_module.py
  +--  ....
To configure a plugin deployed in a jar file
  1. make sure the jar file my_plugin.jar is on the sys paths (how?)
  2. make sure you add the plugin name my_plugin.MyPythonPluginy to the list of plugins (how?)