Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

Jason's method works great. If you have a bug in your toolbox, such as validation, your IDE probably won't be able to pinpoint the problem because toolboxes are encoded. At least WING wasn't able to pinpoint it.

share|improve this question
    
In the OS file properties, have you tried setting the default program for .py files to PyScripter? –  KevinMayall Apr 7 '11 at 19:18
    
By doing this, wouldn't I be effectively telling the OS that I didn't want to run my python script, but rather just open it in the editor? –  RyanDalton Apr 7 '11 at 20:05
    
Yes, correct. Sorry I misunderstood your purpose. –  KevinMayall Apr 11 '11 at 17:38
add comment

7 Answers 7

up vote 24 down vote accepted

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.

share|improve this answer
1  
nice solution! i will try it. –  George Mar 2 '11 at 20:12
    
I have used winpdb successfully in the past to debug long-process scripts. winpdb, once set up allows you to "jump into" a currently running standalone script or theoretically one within another process such as ArcMap and debug it normally. This was very handy for checking in on a script that would take weeks to complete. I have not tried debugging arcpy within an ArcGIS application process though. –  blah238 Mar 3 '11 at 4:47
    
I'd recommend PyCharm. it works like magic –  bo6 Apr 4 '11 at 21:26
add comment

You can change it from the GP options dialog, just point to your executable of choice for editor/debugger.

GP Settings Dialog

share|improve this answer
    
This looks to be a new enhancement for ArcGIS 10, correct? I don't see this setting option for 9.3. –  RyanDalton Apr 7 '11 at 20:25
    
Correct, new in 10. –  Jason Scheirer Apr 7 '11 at 20:33
add comment

For users of 10, clearly the best route to go is with Jason's post, which I marked as the "best answer". For users of 9.3, I was able to follow the ESRI KB Support instructions linked in Brad's post to get it working.

Ultimately the key to get to default editing of ArcGIS python scripts in Pyscripter was to edit the system "action" for "registered file types" ending in *.py files (step #4). I created a new "Edit" action type and then included the Pyscripter.exe path. Once I did this, the default edit action was setup to launch Pyscripter instead of IDLE.

The string I used (because it was cut off in the dialog box shown below) is:

"C:\Program Files\PyScripter\PyScripter.exe" "%1"

enter image description here

share|improve this answer
1  
+1 for the answer, and again, if I could, for including the string and not just an image (text is so much easier to copy and paste!). –  matt wilkie Sep 20 '11 at 22:39
add comment

I don't know how this will work with arcpy, but you can try pdb:

import pdb; pdb.set_trace()
share|improve this answer
    
there is a similar functionality for pydev with eclipse, which is described here. You set eclipse to listen for a signal, and you can trigger that in code running entirely outside eclipse by a pydevd.settrace() function call. urbansim.org/Documentation/DebuggingWithEclipse –  ako Jul 29 '11 at 6:14
add comment

The ArcGIS help discusses how to use the debugger in PythonWin

share|improve this answer
add comment

Maybe an option here?
code google pyscripter

share|improve this answer
    
+1 for PyScripter! –  blah238 Mar 3 '11 at 4:45
    
has anyone actually used this to debug arc scripts though? –  mindless.panda Mar 29 '11 at 0:16
1  
Yes PyScripter works fine for debugging arcpy scripts –  blah238 Jul 30 '11 at 21:43
add comment

Sounds like a very similar problem to this... esri forums

Re: Edit script opens Notepad not PythonWin
Author Christopher Hogan
Date Mar 31, 2010
Message Try going into your registry, and going to this key: HKEY_CLASSES_ROOT\Python.File\shell\Edit with IDLE\command

create a new string value key and name it old(Default), and copy and paste your default value into it. Then paste the path to pythonwin into the default - I pasted "C:\Python25\Lib\site-packages\pythonwin\pythonwin.exe" "%1"

Subject Re: Edit script opens Notepad not PythonWin
Author Kim Slezak
Date Mar 31, 2010
Message Thanks, Christopher! I had to uninstall Python, numby and PythonWin and reinstall all three, then change the IDLE registry key. Onward, finally. Thanks again.
Kimberly Paul Slezak Geospatial Analyst, Programmer, Forester

share|improve this answer
    
Sounds like a reasonable possibility, but when I looked at the registry setting for Python.File, I actually see 2 options, "Edit with IDLE" and "Edit with Pyscripter". Both have similar "command" strings (Pyscripter's string is "C:\Program Files\PyScripter\PyScripter.exe" "%1"). So how do you identify which is the "primary" option? –  RyanDalton Apr 7 '11 at 20:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.