Writing messages in a Python toolbox

When a tool is run, ArcPy is fully aware of the application it is called from. One major effect of this is that you can write messages in Python and your messages automatically appear on the tool dialog box, in geoprocessing history, and the Python window. It also means that any model or script tool that calls your tool has access to the messages you write.

To learn more about messaging, see Understanding messaging in script tools.

In a Python toolbox, a messages object is used for adding additional messages back to the tool.

Message methodsDescription

addMessage(message)

Adds an informative message to the tool's messages

addErrorMessage(message)

Adds an error message to the tool's messages

Note:

addErrorMessage will not throw an exception.

addWarningMessage(message)

Adds a warning message to the tool's messages

addIDMessage(message_type, message_ID, add_argument1=None, add_argument2=None)

Adds a message of any type using geoprocessing message codes

addGPMessages()

Adds messages from the last geoprocessing tool run to the tool's messages

Example of adding messages

In the example below, the input is evaluated, and if it contains no input features, an error message is added to the tool and an arcpy.ExecuteError exception is raised to end the tool.

def execute(self, parameters, messages):
    input = parameters[0].valueAsText
    output = parameters[1].valueAsText
        
    # If the input has no features, add an error message, and raise
    #  an arcpy.ExecuteError
    if int(arcpy.GetCount_management(input)[0]) == 0:
        messages.addErrorMessage("{0} has no features.".format(input))
        raise arcpy.ExecuteError
            
    return
Note:

Messages can also be added using functions such as AddMessage. See Understanding message types and severity for more information.

Related topics