Arcpy GetMessages in PythonWin

885
2
Jump to solution
08-27-2018 06:02 PM
JacobHorwitz1
New Contributor II

I'm a beginner to Python coding and now working on a basic script program, where I'm using both Print and arcpy.AddMessage/arcpy.AddError in a standalone script for ArcMap or in Pythonwin. However, I'm having issues with how I can do this for arcpy.GetMessages in PythonWin to find a traceback error.

I tried using:

msgs = arcpy.GetMessages(2)
print (msgs)
arcpy.AddMessage(msgs)

But couldn't get any traceback message. Is there a code that helps show the traceback error in Pythonwin?

Full Script below:

import arcpy
from arcpy import env
from arcpy.sa import *
arcpy.env.overwriteOutput = True
try:
    # Sets the workspace evironment settings.
    env.workspace = "E:/College/Penn State/GEOG 485/Lesson 1/Lesson1"
  
    inRaster = "foxlake"
    contourInterval = 25
    baseContour = 0
    outContours = "E:/College/Penn State/GEOG 485/Lesson 1/Lesson1/ContourFoxLake.shp"
    arcpy.CheckOutExtension("Spatial")
    arcpy.sa.Contour(inRaster, outContours, contourInterval, baseContour)
   
    # Report a success message
    print "Contour creation successful!"
    arcpy.AddMessage("Contour creation successful!")
except:
    # Report an error message
    print "Failed to create the contour."
    arcpy.AddError("Failed to create the contour.")
   
    # Report any error messages that the Contour tool might have generated
    msgs = arcpy.GetMessages(2)
    print (msgs)
    arcpy.AddMessage(msgs)
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

As a note... if you take everything out of a try except block, you will get an error message which is largely the same as would be returned otherwise.  I quite using try except blocks a long time ago... especially in cases where you only want to get the traceback messages rather than 'do' something else on failure.

From notes I made a long time agoo

Script variantsOutput

A very simple script, with an obvious error.

 

# version 1
print(something)

( IDE dependent message.... )

  File "Path_to_script\script_name.py", line 1, in <module>

    print(something)

NameError: name 'something' is not defined

As above, but getting smarter.

 

# version 2
try:
    print(something)
except:
    print("ooops")
>>> ooops

Lets bring out the tools and see if that helps.

 

# version 3
import sys
import traceback
try:
    print(something)
except:
    exc_traceback = sys.exc_info()
    print(traceback.print_exc())

>>> Traceback (most recent call last):

  File "Path_to_script\script_name.py", line 5, in <module>

    print(something)

NameError: name 'something' is not defined

None

You can draw your own conclusions which is the most useful

View solution in original post

2 Replies
DanPatterson_Retired
MVP Emeritus

GetMessages—ArcPy Functions | ArcGIS Desktop 

did you try just

msgs = arcpy.GetMessages()  # get all

0 Kudos
DanPatterson_Retired
MVP Emeritus

As a note... if you take everything out of a try except block, you will get an error message which is largely the same as would be returned otherwise.  I quite using try except blocks a long time ago... especially in cases where you only want to get the traceback messages rather than 'do' something else on failure.

From notes I made a long time agoo

Script variantsOutput

A very simple script, with an obvious error.

 

# version 1
print(something)

( IDE dependent message.... )

  File "Path_to_script\script_name.py", line 1, in <module>

    print(something)

NameError: name 'something' is not defined

As above, but getting smarter.

 

# version 2
try:
    print(something)
except:
    print("ooops")
>>> ooops

Lets bring out the tools and see if that helps.

 

# version 3
import sys
import traceback
try:
    print(something)
except:
    exc_traceback = sys.exc_info()
    print(traceback.print_exc())

>>> Traceback (most recent call last):

  File "Path_to_script\script_name.py", line 5, in <module>

    print(something)

NameError: name 'something' is not defined

None

You can draw your own conclusions which is the most useful