Python toolbox messages

When a tool is run, ArcPy identifies the application it is called from. This allows you to write messages in Python. Those messages automatically appear on the tool dialog box, in Geoprocessing history, and in 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 can be accessed in the execute method to add messages back to the tool. The five methods for writing messages are as follows:

MethodDescription

addMessage(message)

An informative message is added to the tool's messages.

addWarningMessage(message)

A warning message is added to the tool's messages.

addErrorMessage(message)

An error message is added to the tool's messages.

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

A message of any type is added using geoprocessing message codes.

addGPMessages()

Messages from the last geoprocessing tool run are added to the tool's messages.

Remarque :

Messages can also be added using script tool functions such as AddMessage. See Writing messages in script tools for more information.

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

Rubriques connexes


Dans cette rubrique
  1. Example of adding messages