Select to view content in your preferred language

arcpy.GetMessages() in Python Toolbox does not return tool executed

01-10-2019 01:03 PM
New Contributor III

I'm working on a Python toolbox and the function to get the geoprocessing messages doesn't seem to work as documented. I'm just using the arcpy.GetMessages() function


and the result is 

Start Time: Thursday, January 10, 2019 3:21:16 PM
Succeeded at Thursday, January 10, 2019 3:21:29 PM (Elapsed Time: 12.40 seconds)‍‍

If I use the arcpy.GetMessageCount() function, it likewise returns 2 as the result. This page of message documentation says that when using the GetMessages function "the first message gives the tool executed, and the last message gives the ending and elapsed time for the tool's execution. The tool's second and last messages always give the start and end time, respectively, for the tool's execution." 

The part that I need, the name of the tool executed, isn't coming through. Even if I use the GetMessage function and specify the 0 index position, it still returns the start time. What am I missing?

0 Kudos
2 Replies
MVP Emeritus

I prefer to add my own messages for documentation, that way you can use the basic python print function or see messages in the tool output section.  I suspect things that work and don't report messages aren't added

I add the running tool's name in the script and print or report to tool output

import sys

def tweet(msg):
    """Print a message for both arcpy and python.

    msg - a text message
    m = "\n{}\n".format(msg)

script = sys.argv[0]

tweet("running {} script".format(script))

# ---- prints in python console and shows in the tool report section

# running C:/GIS/ script

You can always accumulate messages and 'tweet' at the end

msgs = ""
msgs += "hello"
msgs += " there "

hello there 
MVP Alum

Can you supply the code you are having issues with?  I haven't been able to duplicate your results.  In the meantime, here is an example of a python toolbox that utilizes arcpy.GetMessages().  It is rather simple; it just asks for a feature layer and then uses arcpy.GetCount_management() to print a count of the rows in the feature layer.

import arcpy

class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = "Toolbox"
        self.alias = ""

        # List of tool classes associated with this toolbox = [Tool]

class Tool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Tool"
        self.description = "Get count of items in feature layer"
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""

        # First parameter 
        inFeature = arcpy.Parameter(
            displayName="Input Features",

        params = [inFeature]
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""

    def execute(self, parameters, messages):
        """The source code of the tool."""

        inFeature = parameters[0].valueAsText


        # last tool used: GetCount


The script will display the following messages (rows 4-7 are from the GetCount tool):

Executing: Tool "MyFeature"
Start Time: Thu Jan 10 20:24:08 2019
Running script Tool...
Executing: GetCount "MyFeature"
Start Time: Thu Jan 10 20:24:08 2019
Row Count = 2965
Succeeded at Thu Jan 10 20:24:08 2019 (Elapsed Time: 0.00 seconds)
Completed script Tool...
Succeeded at Thu Jan 10 20:24:08 2019 (Elapsed Time: 0.05 seconds)

If line 50 --  arcpy.AddMessage(arcpy.GetMessages()) -- in the python toolbox is commented out, the following messages are displayed (the output of GetCount_management is suppressed):

Executing: Tool "MyFeature"
Start Time: Thu Jan 10 20:26:00 2019
Running script Tool...
Completed script Tool...
Succeeded at Thu Jan 10 20:26:00 2019 (Elapsed Time: 0.05 seconds)‍‍‍‍‍‍‍‍‍‍

Hope this helps.