Select to view content in your preferred language

arcpy.GetMessages returns empty [BUG]

1481
5
02-24-2014 10:30 AM
Matrix_Solutions_Inc_Geomatics
Regular Contributor
I am trying to access the messages that were logged throughout a script.

arcpy.AddMessage("Something...")
message = arcpy.GetMessages()
print message  # always empty


The above is always empty.

According to the documentation [http://resources.arcgis.com/en/help/main/10.1/index.html#/AddMessage/018v00000007000000/]

Creates a geoprocessing informative message (Severity=0) that can be accessed with any of the GetMessages functions.


What am I missing here?


I'm starting to think this is either a bug or something not documented.
Tags (2)
0 Kudos
5 Replies
XanderBakker
Esri Esteemed Contributor
Hi Tony,

The arcpy.AddMessage() will add a message that is visible if you run your script as a tool from a toolbox. The arcpy.GetMessages() will return the messages from a geoprocessing tool used. In your example script you don't run any tool, so no message is returned.

If I create a simple script:

import arcpy
arcpy.AddMessage("Something...")
arcpy.AddMessage("Blah...")

cnt = 0
for message in arcpy.GetMessages():
    cnt += 1
    arcpy.AddMessage("Message {0} is '{1}'".format(cnt, message))

arcpy.AddMessage("arcpy.GetMessageCount() results in: {0}".format(arcpy.GetMessageCount()))


The output in the tool window will be:

Executing: Script
Start Time: Tue Feb 25 08:20:02 2014
Running script Script...
Something...
Blah..
.
arcpy.GetMessageCount() results in: 0
Completed script Script...
Succeeded at Tue Feb 25 08:20:02 2014 (Elapsed Time: 0,00 seconds)

Kind regards,

Xander
0 Kudos
Matrix_Solutions_Inc_Geomatics
Regular Contributor
What then does the documentation refer to ?


http://resources.arcgis.com/en/help/main/10.1/index.html#/AddMessage/018v00000007000000/]


AddMessage (arcpy)
Summary
Creates a geoprocessing informative message (Severity=0) that can be accessed with any of the GetMessages functions.
0 Kudos
XanderBakker
Esri Esteemed Contributor
What then does the documentation refer to ?


You are completely right. I does not seem to do what is described in the help. Even the slightly change sample script does not return what one should expect based on the description.

import arcpy
fc = r'C:\Project\_Forums\ZonStatPol\test.gdb\Intersect'
feature_count = int(arcpy.GetCount_management(fc).getOutput(0))

if feature_count == 0:
    arcpy.AddError("{0} has no features.".format(fc))
else:
    arcpy.AddMessage("{0} has {1} features.".format(fc, feature_count))

print arcpy.GetMessages()


Results in:

Executing: GetCount C:\Project\_Forums\ZonStatPol\test.gdb\Intersect
Start Time: Tue Feb 25 15:46:45 2014
Row Count = 10
Succeeded at Tue Feb 25 15:46:45 2014 (Elapsed Time: 0,19 seconds)


... which is the output of the geoprocessing tool GetCount_management

Said, but true (or I simply don't get it).

Kind regards,

Xander
0 Kudos
Matrix_Solutions_Inc_Geomatics
Regular Contributor
Thanks for the quick reply.

Would you happen to know if the messages get written somewhere else in the interpreter? or rather, the ultimate place that they trickle down to?

I tried redirecting sys.stdout / sys.stderr but it seem arcpy.AddMessage doesn't seem to write to there either. However, there must be a standard Python object that receives the messages because my IDE console always displays them.
0 Kudos
XanderBakker
Esri Esteemed Contributor
Hi Tony,

I can't find where this information is stored. Maybe some implementation of the logging module, although most of the functionality goes through ArcObjects...

I guess it's not so hard to implement an alternative, but if you want to keep using the messaging of arcpy, it might be wise to contact Esri support and report the bug. Maybe in a future release they fix the bug, or change the help text 😉

Kind regards,

Xander
0 Kudos