arcpy.AddMessage() Weird behavior or not?

1054
2
Jump to solution
10-24-2017 09:11 AM
MollyFoley
Regular Contributor

I'm developing arcpy scripts in PyCharm, though I have also tested this in Wing IDE and it does the same thing. Is it normal that when you use arcpy.AddMessage() it prints to your console output? I know it does not do this in PythonWin (or it used to not do that ~2 years ago when I was using that IDE). I thought that AddMessage only allowed messages to show up when running the script tool in ArcMap, not in the console output and that's why you use AddMessage and print statements together in stand-alone scripts, so that way if you run it in a python IDE, you still get the messages. 

Note: I do not use arcpy.GetMessages() or GetMessage() anywhere within the script.

I am running 10.5 (python 2.7)

This

arcpy.AddMessage("Hello")
print "Hello"‍‍

will print this in the output window:

Hello

Hello

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

I assume ArcPy's AddMessage implements ArcObject's IGeoProcessor.AddMessage.  Since at least ArcGIS 9.2, the documentation has stated:

Output an informative message. When running within a script tool, message becomes part of the tool messages, otherwise outputs to console

I don't have ready access to an ArcGIS 9.2 or earlier machine, but I did test on an ArcGIS 10.1 machine.  On both my 10.5.1 and 10.1 machine, running arcpy.AddMessage("Hello World") from the Python interpreter bundled with ArcGIS outputs Hello World to the console.

This is less an issue of ArcPy, or even ArcGIS, and more an issue of how different environments implement the console.  I can't say exactly what difference is driving the different behavior between platforms, but I don't think ArcPy is behaving differently with the different platforms.

View solution in original post

2 Replies
RebeccaStrauch__GISP
MVP Esteemed Contributor

Interesting, I hadn't noticed (or paid attention) to that before, but you are correct that Wing can use either.

But the Python tab/window in ArcCatalog (10.3.1) does not acknowledge arcpy.AddMessage

I use both Wing and the Python tab for testing and debugging.  Since I want it to work in either, I use a function so it will print, but I have notices that it prints twice in Wing (but just accepted it).

# shows messages for python window or tool
def myMsgs(message):
    arcpy.AddMessage(message)
    print(message)

fwiw.

JoshuaBixby
MVP Esteemed Contributor

I assume ArcPy's AddMessage implements ArcObject's IGeoProcessor.AddMessage.  Since at least ArcGIS 9.2, the documentation has stated:

Output an informative message. When running within a script tool, message becomes part of the tool messages, otherwise outputs to console

I don't have ready access to an ArcGIS 9.2 or earlier machine, but I did test on an ArcGIS 10.1 machine.  On both my 10.5.1 and 10.1 machine, running arcpy.AddMessage("Hello World") from the Python interpreter bundled with ArcGIS outputs Hello World to the console.

This is less an issue of ArcPy, or even ArcGIS, and more an issue of how different environments implement the console.  I can't say exactly what difference is driving the different behavior between platforms, but I don't think ArcPy is behaving differently with the different platforms.