Tool definitions in a Python toolbox

Tools are added to a .pyt file as classes. Each tool class must include, at a minimum, the __init__ and execute methods. Optionally, you can use the getParameterInfo, isLicensed, updateParameters, and updateMessages methods to add control to the behavior of the tool.

Methods used to define a working tool class

Tool methodRequired or optionalDescription

__init__

Required

Initializes the tool class.

getParameterInfo

Optional

Defines the tool's parameters.

isLicensed

Optional

Returns whether the tool is licensed to run.

updateParameters

Optional

Called each time the user changes a parameter on the tool dialog box. After returning from updateParameters, geoprocessing calls its internal validation routine.

updateMessages

Optional

Called after returning from the internal validation routine. You can examine the messages created from internal validation and change them if desired.

execute

Required

The tool's source code.

postExecute

Optional

Called after the execute method completes and outputs are processed and added to the display.

Setting a tool class __init__ method

The __init__ method in a tool class is a standard Python class initialization method. For a tool in a Python toolbox, the __init__ method is used to set the tool properties, including the label and description properties. The tool's name is established by the name of the class (in the example below, the tool name is CalculateSinuosity).

The __init__ method establishes tool properties such as the label and description. Below, a tool named CalculateSinuosity is created.

class CalculateSinuosity(object):
    def __init__(self):
        self.label = "Calculate Sinuosity"
        self.description = "Sinuosity measures the amount that a river meanders within its valley, " + \
                           "calculated by dividing total stream length by valley length."
Note:

Python syntax rules enforce most geoprocessing conventions when naming a Python toolbox tool class. However, avoid underscores in a tool class name, as they cause errors when running tools from ArcGIS Server.

The following properties can be set in a tool's __init__ method:

PropertyDescription

category

The name of the toolset where the tool is located. A toolset is a way to organize tools in a toolbox.

To nest toolsets, separate their names with consecutive backward slashes, for example: self.category = "Toolset 1\\Toolset 2.

description

The description for the tool.

label

The label is the display name for the tool as shown in the Geoprocessing pane.