Writing messages in script tools

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

Learn more about messaging in Understanding messaging in script tools

During execution of a tool, messages are written that can be retrieved with geoprocessing functions. The four ArcPy functions for writing messages are as follows:

A call to AddIDMessage() displays a short message and the message ID, which is a link to an explanation about the cause of and solutions to the problem. When you add an error message using either AddError() or AddIDMessage(), the following occur:

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.

import arcpy
input = arcpy.GetParameterAsText(0)
output = arcpy.GetParameterAsText(0)
       
# If the input has no features, add an error message, and raise
#  an arcpy.ExecuteError
if int(arcpy.GetCount_management(input).getOutput(0)) == 0:
    arcpy.AddError("{0} has no features.".format(input))
    raise arcpy.ExecuteError

Related Topics

3/3/2014