Masthead

Debugging ArcGIS Tools

You are about to learn how to create Tools for ArcGIS based on Python scripts. One of the challenges with this is that there is no "Debug I/O" window within ArcGIS to help us in debugging the scripts. There are a number of approaches to help with debugging:

We'll look at the first three below and then touch on the last one a little later.

Outputting Messages to the Results Dialog in ArcGIS

You can add messages to the "Results Dialog" that appears when you run a custom tool in ArcGIS. These messages are also available through the "Results" item in the "Geoprocessing" menu. This is done by calling "AddMessage()" as below:

arcpy.AddMessage("Put the message here")

You'll want to use the dialog boxes when you only have a few messages and AddMessage() when you have a lot. The dialogs can be a pain if you have a lot of them to click "OK" on but the AddMessage() calls can get lost in many lines of output in the Results Dialog. Another trick is to call AddMessage() with something you can see easily in the output like:

arcpy.AddMessage("-------------------------")
arcpy.AddMessage("Put the message here")
arcpy.AddMessage("-------------------------")

 

A Structure for Building Tools

It's harder to write and debug code when it is running as an ArcGIS Tool. I recommend writing your tools and testing them before creating a tool to use them in ArcMap. Using the code below, you can just check the number of parameters to direct the debugging output to ArcGIS or to the Python console (e.g. the Debug I/O panel in the Wing IDE).

#########################################################################
# Starter script to create tools for ArcGIS.
# Author: Jim Graham
# Date: 4th of March, 2018
#########################################################################

import arcpy # include the ArcGIS library
import sys # include the system library to access the number of parameters

#########################################################################
# Mini function to send the print messages to the right output
# Input: Message - string to be printed
#########################################################################
def MyPrint(Message):
	if (len(sys.argv)>1): # if running in a tool, print into ArcGIS
		arcpy.AddMessage(Message)
	else: # else print to Debug I/O
		print(Message)
	
#########################################################################
# Setup default parameters and then get the parameters from the argument
# list if we are running as a tool in ArcMap.
#########################################################################

InputLayer="C:/Temp/Input.shp" # Default input path for testing
OutputLayer="C:/Temp/Output.shp" # Default output path for testing

if (len(sys.argv)>1): # if running in a tool, get the parameters from arc
	InputLayer=arcpy.GetParameterAsText(0)
	OutputLayer=arcpy.GetParameterAsText(1)

# Print the parameters for debugging
MyPrint("InputLayer: "+InputLayer)
MyPrint("OutputLayer: "+OutputLayer) 

#########################################################################
# Main code (replace this with your own)
#########################################################################

print("Begining processing")

# Add your code here to process the data

print("Completed successfully")

Tkinter (optional)

Tkinter is one of the packages for Python to display Graphical User Interfaces (GUIs). Tkinter is included with Python and is very easy to use to display simple dialog boxes. Tkinter also allows other types of windows and can be used to create complex GUIs.

Note: Python is a scripting language and not a compiled language. This does limit the performance of Python scripts. If you want to build a high performance application you will want to either switch to a compiled language like Java or use compiled packages with Python for much of the application's functionality.

Tkinter by default will put up a modeless window typical of most desktop applications. We only want to put up a dialog box (also known as a message box) at this point so the first thing we're going to do is turn off the modeless window and then we'll put up a dialog with a message. Try the code below.

import tkMessageBox #  import the message box module

# before showing the dialog box, we'll want to turn off the rest of the GUI
import Tkinter # import the GUI package
root=Tkinter.Tk() # get the root object of the GUI
root.withdraw() # turn off the typical modeless window in the GUI

tkMessageBox.showinfo("Dialog Title","Message within the dialog") # show the dialog box

You should see the message in the middle of the box which should disappear when you click 'OK'. When you call the "showinfo()" function, the function will wait for the user to click the "OK" button. The "tkMessageBox" allows you to display a variety of message boxes including warnings and errors.

Imaging what you can put into the string in the message box! This could be a message to your users, a message to you than an error has occurred, or it could be debugging information.

Additional Resources

Python Documentation: Tkinter

 

© Copyright 2018 HSU - All rights reserved.