Module 3: Error Handling and Debugging

 Module 3 focused on debugging Python scripts. The lab taught me how to identify syntax, runtime and file path-related errors while improving my understanding of troubleshooting techniques and exception handling. Three scripts were successfully debugged. 

Script 1 


For the first script, I first received a name error stating that ‘fc’ is not defined. To fix this, I changed it to ‘FC’. The second error I received was another Name error stating that field is not defined. To fix this error, I switched ‘fields’ to ‘field’. The script then ran successfully, as shown above.

Script 2 

The first error received on the second script was a syntax warning on line 15, saying there was an invalid escape sequence (/p). To fix this, I simply fixed the backslash to the proper form. The second error was a name error on line 14, stating that ‘Filepath’ was not described. To fix this, I just had to change ‘Filepath’ to ‘filepath’ because Python is case-sensitive. The third error encountered was an OSerror, stating that the output folder of AustinTx.aprx did not exist. This is because that is the wrong filepath, the correct folder was TravisCountyAustinTX.aprx, which is also found in the data folder rather than the results folder. Once the filepath was fixed, another name error occurred, stating that arcp is not defined. To be fixed, this just had to be changed to arcpy. The next error that popped up was an attribute error saying that spatialrefrence does not exist, so this just needed an extra e to be added to make it spatialreference. The final error received was a TypeError cause by using “map” instead of “maps”, which was caused by Python to misinterpret the loop created. 

Script 3

For Script 3, I did not directly fix the error in Part A. I added a try except statement around the section of code that contained the error. The ArcGISProject() function was missing a positional argument, causing the exception. The exception was able to be printed through the try-except variable and able to continue running the script. The result was Part A printing an error message and Part B running smoothly, printing the layer names, data sources and spatial reference. 

A flowchart was also created to summarize the process of debugging Script 3.

Overall, this exercise helped me better understand how to debug Python scripts. I learned how small mistakes such as capitalization, misspellings, invalid file paths and syntax issues can prevent a script from running properly. I also learned how to read Python error messages to identify where problems occur and how to use try-except statements to handle exceptions without stopping the entire script from completing.

Comments

Popular posts from this blog

Module 1: Crime Analysis

Module 2: LiDAR