FAQ
overflow

Great Answers to
Questions About Everything

QUESTION

I have written many Python scripts using arcpy in ArcGIS 10, and so far my only means of debugging is restricted to printing messages to the geoprocessing results window using arcpy.AddMessage(). Are there any other options out there, such as setting break points? This would save a lot of time and frustration.

Thanks

EDIT

Here's the code I attempted to use when trying Jason's method:

import os
import arcpy

arcpy.ImportToolbox(os.path.join(os.path.dirname(__file__), 'MasterBuildings.tbx'))
arcpy.ExportAtlasOfRooms(r"\\Geogdata\campusmapping\Space\Maps\PDF\All_Buildings", "TEST1", "PDF", "300", "BEST", "true", "false", "false")

But it gave me an error:

invalid syntax: MasterBuildings.tbx, line 26, pos 23 

Wing IDE attempted to open the MasterBuildings.tbx to show me the line of code that has an error, but it is encoded. I don't understand how there would be invalid syntax in the toolbox, unless it is corrupted. Any help is appreciated.

{ asked by Tanner }

ANSWER

Usually Python debuggers/IDEs assume the Python script is running in the same process as itself so debugging a script running in ArcMap.exe is right out -- you need to get enough of the GP scripting environment bootstrapped in a Python script as you can to debug with.

A method that's worked very well for me over the past few years is to write a simple script that just calls the tool and use that as my main script in the Python IDE (Wing or Pythonwin) and have my breakpoints set in the tool's .py file also open in the same IDE session.

So basically I do this:

  1. Get the set of inputs that aren't working in my script tool
  2. Open a simple .py file in the same folder as the .tbx that calls the tool
  3. Open up the caller script and the script tool .py file in the IDE
  4. Set breakpoints in script tool file
  5. Run the caller script

And my caller script is usually pretty simple:

import os
import arcpy
arcpy.ImportToolbox(os.path.join(os.path.dirname(__file__), 'my.tbx'))
arcpy.MyToolThatIsFailing_myalias("inputs", "that", "don't" "work")

I've tried winpdb to debug scripts running in ArcMap but I've never had any luck. If you want to try it out and you get it working well, please share your findings.

{ answered by Jason Scheirer }
Tweet