The Art Of Debugging In ArcPy
Debugging Time...
Today I wanted to share a post about debugging code and error handling. I am going to talk about why debugging is such an important topic in software development. The n we are going to take e a look at handling errors to make incorrect code work. Below are three examples from ArcPy Python scripts in which we look at both debugging errors and handline errors to pass the code for GIS data located inside of an Arc Pro project file.
Syntax Debugging (Spelling can make or break your code, as well as spaces)
This is the Script 1 Output I completed.
o Prints out a list of the field names within park.shp
For Loop Debugging
Before running this script, I need to verify that the four available shapefiles (.shp) are added to the ArcGIS Project TravisCountyAustinTX.aprx. The successful script should printout the names of all layers in the Project.
In order to fix this script, I did the following...
I fixed the spelling for the aprx, the file path, and I fixed the file path for the file path variable. I made sure arcpy.listMaps() is camel cased. I ensured the concatenating print statement had a plus sign with appropriate spaces. SpatialRef = desc.spatialReference needs to be spelled correctly. The dataset needs to be called in arcpy.Describe.
The for loop needs a colon at the end for both for loop sections, and the listLayers() needs to reference m in maps. I also put an str aroiund lyr.name to stringify the name of each layer so I can then print out each layer name in the print statement. I made sure aprx was lower cased and correct.
This is my Script 2 Output...MNo
o Prints out a list of layers for each data frame in the TravisCountyAustinTx.aprx
Try and except for error handling
For this final script, we are going to try some error handling! So we are not going to fix the errors in our script, but we are going to use a Try and Except statement to bypass the incorrect code to still run our script. So Try and Except are our keywords for this exception...
Script 3 Output
Part 3 script results
o Prints out an error message for Part A and the name, spatial reference, and scale for three data frames in the TravisCountyAustinTx.aprx.
Comments
Post a Comment