<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.esri.com/Dev/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Geoprocessing : script tools</title><link>http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/script+tools/default.aspx</link><description>Tags: script tools</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Debug Build: 61120.2)</generator><item><title>2009 Developers Summit</title><link>http://blogs.esri.com/Dev/blogs/geoprocessing/archive/2009/03/25/2009-Developers-Summit.aspx</link><pubDate>Wed, 25 Mar 2009 23:54:00 GMT</pubDate><guid isPermaLink="false">b60b3f0a-e2bd-4be5-8a18-822c697649ab:4302</guid><dc:creator>dmhoneycutt</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.esri.com/Dev/blogs/geoprocessing/comments/4302.aspx</comments><wfw:commentRss>http://blogs.esri.com/Dev/blogs/geoprocessing/commentrss.aspx?PostID=4302</wfw:commentRss><description>&lt;p&gt;The 2009 Developers Summit has come and gone.&amp;nbsp; Thanks to all who attended!&amp;nbsp; We enjoyed meeting with you.&amp;nbsp; We've uploaded the geoprocessing presentations to the Model and Script tool gallery. Session videos have been posted to the Media Gallery.&amp;nbsp; &lt;br&gt;&lt;/p&gt;&lt;p&gt;Slides, scripts, and data from the two workshops on Designing and Building geoprocessing tools are available in the Model and Script tool gallery, or by clicking &lt;a href="http://resources.esri.com/geoprocessing/index.cfm?fa=codeGalleryDetails&amp;amp;scriptID=16168"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Slides and other materials from the Building Geoprocessing Services session are available &lt;a href="http://resources.esri.com/geoprocessing/index.cfm?fa=codeGalleryDetails&amp;amp;scriptID=16177"&gt;here&lt;/a&gt;. &lt;/p&gt;&lt;p&gt;Slides and scripts for the Python Scripting Advanced Techniques session are available &lt;a href="http://resources.esri.com/geoprocessing/index.cfm?fa=codeGalleryDetails&amp;amp;scriptID=16184"&gt;here&lt;/a&gt;.&amp;nbsp; &lt;br&gt;&lt;/p&gt;&lt;img src="http://blogs.esri.com/Dev/aggbug.aspx?PostID=4302" width="1" height="1"&gt;</description><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/advanced/default.aspx">advanced</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/script+tools/default.aspx">script tools</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/Python/default.aspx">Python</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/Digging+deeper/default.aspx">Digging deeper</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/Tips+and+tricks/default.aspx">Tips and tricks</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/2009+Developers+Summit/default.aspx">2009 Developers Summit</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/Geoprocessing+services/default.aspx">Geoprocessing services</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/ArcGIS+Server/default.aspx">ArcGIS Server</category></item><item><title>Send us your feedback</title><link>http://blogs.esri.com/Dev/blogs/geoprocessing/archive/2009/03/25/Send-us-your-feedback.aspx</link><pubDate>Wed, 25 Mar 2009 23:47:00 GMT</pubDate><guid isPermaLink="false">b60b3f0a-e2bd-4be5-8a18-822c697649ab:2779</guid><dc:creator>dmhoneycutt</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.esri.com/Dev/blogs/geoprocessing/comments/2779.aspx</comments><wfw:commentRss>http://blogs.esri.com/Dev/blogs/geoprocessing/commentrss.aspx?PostID=2779</wfw:commentRss><description>&lt;p&gt;You may not have noticed, but you can send us feedback about this site. There's
a feedback link at the bottom right of the home page, as illustrated at the bottom 
of this post.&lt;/p&gt;


&lt;p&gt;We're particularly interested in hearing from you about blog post topics
you'd like to see and suggestions on how we could make this site more useful.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;a href="http://blogs.esri.com/Dev/photos/geoprocessing/picture2780.aspx" target="_blank"&gt;&lt;img src="http://blogs.esri.com/Dev/photos/geoprocessing/images/2780/408x480.aspx" border="0"&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://blogs.esri.com/Dev/aggbug.aspx?PostID=2779" width="1" height="1"&gt;</description><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/beginner/default.aspx">beginner</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/advanced/default.aspx">advanced</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/New+at+9.3/default.aspx">New at 9.3</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/Spatial+Analysis/default.aspx">Spatial Analysis</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/script+tools/default.aspx">script tools</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/Python/default.aspx">Python</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/ArcSDE/default.aspx">ArcSDE</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/Geostatistical+Analyst/default.aspx">Geostatistical Analyst</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/Digging+deeper/default.aspx">Digging deeper</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/Tips+and+tricks/default.aspx">Tips and tricks</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/UNIX_2F00_Linux/default.aspx">UNIX/Linux</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/3D+Lidar+Point+Data/default.aspx">3D Lidar Point Data</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/ModelBuilder/default.aspx">ModelBuilder</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/2009+Developers+Summit/default.aspx">2009 Developers Summit</category></item><item><title>Tips and tricks - Error handling in Python script tools</title><link>http://blogs.esri.com/Dev/blogs/geoprocessing/archive/2008/12/01/Tips-and-tricks-_2D00_-Error-handling-in-Python-script-tools.aspx</link><pubDate>Mon, 01 Dec 2008 17:43:00 GMT</pubDate><guid isPermaLink="false">b60b3f0a-e2bd-4be5-8a18-822c697649ab:3169</guid><dc:creator>dmhoneycutt</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.esri.com/Dev/blogs/geoprocessing/comments/3169.aspx</comments><wfw:commentRss>http://blogs.esri.com/Dev/blogs/geoprocessing/commentrss.aspx?PostID=3169</wfw:commentRss><description>&lt;p&gt;I recently had a project that involved creating lots of Python script tools.  
Since other people were going to use these tools, I wanted my error handling to 
be robust and informative.  This post is about some tips and tricks I discovered 
during this project.&lt;/p&gt;

&lt;h4&gt;Error handling basics&lt;/h4&gt;
&lt;p&gt;The basics of handling Python errors are covered in the 9.3 help topic 
&lt;a href="http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Error_handling_with_Python"&gt;Error handling with Python&lt;/a&gt;.  
The Python.org document &lt;a href="http://www.python.org/doc/2.5.2/tut/node10.html#SECTION0010200000000000000000"&gt;Errors and Exceptions&lt;/a&gt; has more detailed information.&lt;/p&gt;

&lt;h4&gt;Tip #1 - Use the arcgisscripting.ExecuteError exception&lt;/h4&gt;
&lt;p&gt;In version 9.2, we introduced the arcgisscripting object along with a new exception class, 
arcgisscripting.ExecuteError.  (This arcgisscripting.ExecuteError exception class wasn't 
documented in version 9.2, so few people knew about it.)&amp;nbsp;
The arcgisscripting.ExecuteError exception is thrown whenever a geoprocessing tool 
or geoprocessing function encounters an error.   What this means is that you can divide errors 
into two groups, 
geoprocessing errors (those that throw the arcgisscripting.ExecuteError exception) and everything else.&amp;nbsp; You can then handle the errors differently, as 
demonstrated in the code below:&lt;/p&gt;

&lt;blockquote&gt;
&lt;pre&gt;&lt;font size="2" face="courier new, courier, mono"&gt;import arcgisscripting&lt;br&gt;gp = arcgisscripting.create(9.3)&lt;br&gt;try:&lt;br&gt;    result = gp.getcount("C:/blah.shp")&lt;br&gt;    &lt;i&gt;&lt;font color="#006600"&gt;# x = y&lt;/font&gt;&lt;/i&gt;&lt;br&gt;        &lt;br&gt;&lt;i&gt;&lt;font color="#006600"&gt;# Return GEOPROCESSING specific errors&lt;br&gt;#&lt;/font&gt;&lt;/i&gt;&lt;br&gt;except arcgisscripting.ExecuteError:&lt;br&gt;    gp.AddError(gp.GetMessages(2))&lt;br&gt;    &lt;br&gt;&lt;i&gt;&lt;font color="#006600"&gt;# Return any PYTHON or system specific errors&lt;br&gt;#&lt;/font&gt;&lt;/i&gt;&lt;br&gt;except:&lt;br&gt;    gp.AddError("Python or system error occurred")&lt;/font&gt;&lt;/pre&gt;&lt;/blockquote&gt;
	
&lt;p&gt;The code above is used as the source for a script tool.  To keep things simple, 
the script tool has no parameters.  When the script tool is executed, the call to 
getcount produces an error because the dataset
"C:/blah.shp" doesn't exist.  As a result, the progress dialog
looks as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.esri.com/Dev/photos/geoprocessing/images/3164/original.aspx" target="_blank"&gt;&lt;img src="http://blogs.esri.com/Dev/photos/geoprocessing/images/3164/591x271.aspx" border="0"&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;


&lt;p&gt;To prove how geoprocessing errors and Python errors are handled differently, 
change the two lines of code as follows:&lt;/p&gt;

&lt;pre&gt;&lt;font size="2" face="courier new, courier, mono"&gt;    &lt;i&gt;&lt;font color="#006600"&gt;# result = gp.getcount("C:/blah.shp")&lt;/font&gt;&lt;/i&gt;&lt;br&gt;    x = y&lt;/font&gt;&lt;/pre&gt;

&lt;p&gt;Now when the script is executed, an error will occur because the variable
'y' is undefined, and the progress dialog will display as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.esri.com/Dev/photos/geoprocessing/images/3166/original.aspx" target="_blank"&gt;&lt;img src="http://blogs.esri.com/Dev/photos/geoprocessing/images/3166/591x221.aspx" border="0"&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;h4&gt;Tip #2 - Beware of getting error messages from a result object&lt;/h4&gt;	
&lt;p&gt;Before moving on, a quick word about the result object, shown below:&lt;/p&gt;

&lt;pre&gt;&lt;font size="2" face="courier new, courier, mono"&gt;    result = gp.getcount("C:/blah.shp")&lt;/font&gt;&lt;/pre&gt;

&lt;p&gt;If the call to getcount above raises an exception, the result object is null.
This means you cannot retrieve error messages from the result object.  For
example:&lt;/p&gt;

&lt;blockquote&gt;
&lt;pre&gt;&lt;font size="2" face="courier new, courier, mono"&gt;import arcgisscripting&lt;br&gt;gp = arcgisscripting.create(9.3)&lt;br&gt;try:&lt;br&gt;    result = gp.getcount("C:/blah.shp")&lt;br&gt;        &lt;br&gt;&lt;i&gt;&lt;font color="#006600"&gt;# Return GEOPROCESSING specific errors&lt;br&gt;# (this method is INCORRECT!)&lt;/font&gt;&lt;/i&gt;&lt;br&gt;except arcgisscripting.ExecuteError:&lt;br&gt;    &lt;b&gt;gp.AddError(result.GetMessages(2))&lt;/b&gt;&lt;br&gt;    &lt;br&gt;&lt;i&gt;&lt;font color="#006600"&gt;# Return any PYTHON or system specific errors&lt;br&gt;#&lt;/font&gt;&lt;/i&gt;&lt;br&gt;except:&lt;br&gt;    gp.AddError("Python or system error occurred")&lt;/font&gt;&lt;/pre&gt;&lt;/blockquote&gt;


&lt;p&gt;The above code will fail with the message "name 'result' is not defined".
This is due to the fact that the result object is null.  Since the result
object is null, a Python error will be 
raised since a null object doesn't have a GetMessages() method.  
Note -- a result object created by calling
a &lt;i&gt;geoprocessing service&lt;/i&gt; on an ArcGIS Server is never null.  Null result objects
a created only when a tool is run &lt;i&gt;locally&lt;/i&gt; and it raises an error.&amp;nbsp; For more information about using the result object, see &lt;a href="http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Getting_results_from_a_geoprocessing_tool"&gt;Getting results from a geoprocessing tool&lt;/a&gt;.&lt;br&gt;&lt;/p&gt;

	
&lt;h4&gt;Tip #3 - Use AddReturnMessage() to preserve links to error codes&lt;/h4&gt;
In version 9.3, geoprocessing error numbers shown in the progress dialog are 
hyperlinks to a help page that further describes the error.  To enable hyperlinks
for errors in your script, you need to use the AddReturnMessage() method instead of the
AddError method, as follows:

&lt;blockquote&gt;
&lt;pre&gt;&lt;font size="2" face="courier new, courier, mono"&gt;import arcgisscripting&lt;br&gt;gp = arcgisscripting.create(9.3)&lt;br&gt;try:&lt;br&gt;    result = gp.getcount("C:\\blah.shp")&lt;br&gt;        &lt;br&gt;&lt;i&gt;&lt;font color="#006600"&gt;# Return GEOPROCESSING specific errors&lt;br&gt;#&lt;/font&gt;&lt;/i&gt;&lt;br&gt;except arcgisscripting.ExecuteError:&lt;br&gt;    &lt;b&gt;for msg in range(0, gp.MessageCount):&lt;br&gt;        if gp.GetSeverity(msg) == 2:&lt;br&gt;            gp.AddReturnMessage(msg)&lt;/b&gt;&lt;br&gt;    &lt;br&gt;&lt;i&gt;&lt;font color="#006600"&gt;# Return any PYTHON or system specific errors&lt;br&gt;#&lt;/font&gt;&lt;/i&gt;&lt;br&gt;except:&lt;br&gt;    gp.AddError("Python or system error occurred")&lt;/font&gt;&lt;/pre&gt;&lt;/blockquote&gt;

&lt;p&gt;Now when the script is executed, the progress dialog will look as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.esri.com/Dev/photos/geoprocessing/picture3165.aspx" target="_blank"&gt;&lt;/a&gt;&lt;a href="http://blogs.esri.com/Dev/photos/geoprocessing/images/3165/original.aspx" target="_blank"&gt;&lt;img src="http://blogs.esri.com/Dev/photos/geoprocessing/images/3165/592x224.aspx" border="0"&gt;&lt;/a&gt;&lt;br&gt;&amp;nbsp;&lt;/p&gt;

&lt;h4&gt;Tip #4 -- Use traceback to return more information about the error&lt;br&gt;&lt;/h4&gt;

&lt;p&gt;The help system topic &lt;a href="http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Error_handling_with_Python"&gt;Error handling with Python&lt;/a&gt; shows you how to use Python's traceback to retrieve the line number causing the error, the name of the .py file,
and, for Python errors, a description of the error.
&lt;/p&gt;

&lt;p&gt;At version 9.3, you can run script tools in-process, as described in &lt;a href="http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Running_a_script_in_process"&gt;Running a script in process&lt;/a&gt;. Scripts run in-process execute much faster than scripts run out-of-process, so you 
always want to run the script in-process.  When a script is run in-process,
ArcGIS reads the .py file into memory and then executes it.  Because ArcGIS puts
the .py files into memory, the Python traceback module doesn't know the name of the
.py file and will report the file name as "&amp;lt;string&amp;gt;".  If you want to report 
the name of the .py file causing the error (very useful for debugging), you'll have to provide it yourself.
&lt;/p&gt;

&lt;p&gt;The script below has a local routine, trace(), that uses traceback to return the
line number, the file name, and the error description. The routine also contains
the the hard-coded file name (shown in bold) which you must change for your script.&lt;/p&gt;

&lt;blockquote&gt;
&lt;pre&gt;&lt;font size="2" face="courier new, courier, mono"&gt;import arcgisscripting&lt;br&gt;gp = arcgisscripting.create(9.3)&lt;br&gt;try:&lt;br&gt;    def trace():&lt;br&gt;        import os, sys, traceback&lt;br&gt;        tb = sys.exc_info()[2]&lt;br&gt;        tbinfo = traceback.format_tb(tb)[0]  # script name + line number&lt;br&gt;        line = tbinfo.split(", ")[1]&lt;br&gt;        &lt;br&gt;        &lt;i&gt;&lt;font color="#006600"&gt;# Construct the pathname to this script.  Get the pathname&lt;br&gt;        #  to the folder containing the script using sys.path[0]. &lt;br&gt;        #  Modify line below and replace 'myscript.py' with the name of&lt;br&gt;        #  this script. &lt;br&gt;        #&lt;/font&gt;&lt;/i&gt;&lt;br&gt;        filename = sys.path[0] + os.sep + &lt;b&gt;"myscript.py"&lt;/b&gt;&lt;br&gt;        &lt;br&gt;        &lt;i&gt;&lt;font color="#006600"&gt;# Get Python syntax error&lt;br&gt;        #&lt;/font&gt;&lt;/i&gt;&lt;br&gt;        synerror = traceback.format_exc().splitlines()[-1]&lt;br&gt;        return line, filename, synerror&lt;br&gt;    if __name__ == '__main__':  &lt;br&gt;        &lt;i&gt;&lt;font color="#006600"&gt;# y = x&lt;/font&gt;&lt;/i&gt;&lt;br&gt;        result = gp.getcount("C:/blah.shp")&lt;br&gt;        &lt;br&gt;except arcgisscripting.ExecuteError:&lt;br&gt;    &lt;i&gt;&lt;font color="#006600"&gt;# Call our trace routine to retrieve line number and filename.&lt;br&gt;    #  (returned variable 'err' is ignored since this is a gp error.&lt;br&gt;    #&lt;/font&gt;&lt;/i&gt;&lt;br&gt;    &lt;b&gt;line, filename, err = trace()&lt;/b&gt;&lt;br&gt;    gp.AddError("Geoprocessing error on " + line + " of " + filename + " :")&lt;br&gt;    for msg in range(0, gp.MessageCount):&lt;br&gt;        if gp.GetSeverity(msg) == 2:&lt;br&gt;            gp.AddReturnMessage(msg)&lt;br&gt;    &lt;br&gt;except:&lt;br&gt;    &lt;b&gt;line, filename, err = trace()&lt;/b&gt;&lt;br&gt;    gp.AddError("Python error on " + line + " of " + filename)&lt;br&gt;    gp.AddError(err)&lt;/font&gt;&lt;/pre&gt;&lt;/blockquote&gt;


&lt;p&gt;Below are illustrations of the progress dialog showing the line number
and file name for both a geoprocessing error and a Python syntax error (by commenting out
the "y = x" statement).&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;a href="http://blogs.esri.com/Dev/photos/geoprocessing/images/3167/original.aspx" target="_blank"&gt;&lt;img src="http://blogs.esri.com/Dev/photos/geoprocessing/images/3167/640x222.aspx" border="0"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;a href="http://blogs.esri.com/Dev/photos/geoprocessing/images/3168/original.aspx" target="_blank"&gt;&lt;img src="http://blogs.esri.com/Dev/photos/geoprocessing/images/3168/640x226.aspx" border="0"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;Tip #5 -- Use Finally statement to clean up cursors&lt;br&gt;&lt;/h4&gt;
&lt;p&gt;Something I discovered by reading the Python doc &lt;a href="http://www.python.org/doc/2.5.2/tut/node10.html#SECTION0010200000000000000000"&gt;Errors and Exceptions&lt;/a&gt;
is the 'finally' statement.  No matter what happens in your Python script, code 
in a 'finally:' block always gets executed.  It's a great place to put clean-up 
actions, such as deleting cursors, as shown in the code below.  &lt;/p&gt;


&lt;blockquote&gt;&lt;pre&gt;&lt;font size="2" face="courier new, courier, mono"&gt;import arcgisscripting&lt;br&gt;gp = arcgisscripting.create(9.3)&lt;br&gt;&lt;br&gt;def trace():&lt;br&gt;    import os, sys, traceback&lt;br&gt;    tb = sys.exc_info()[2]&lt;br&gt;    tbinfo = traceback.format_tb(tb)[0] &lt;br&gt;    line = tbinfo.split(", ")[1]&lt;br&gt;    filename = sys.path[0] + os.sep + "testscript.py"&lt;br&gt;    synerror = traceback.format_exc().splitlines()[-1]&lt;br&gt;    return line, filename, synerror&lt;br&gt;&lt;br&gt;if __name__ == '__main__':&lt;br&gt;    &lt;i&gt;&lt;font color="#006600"&gt;# Define variables for a cursor and its row&lt;br&gt;    #&lt;/font&gt;&lt;/i&gt;&lt;br&gt;    &lt;b&gt;cursor, row = None, None&lt;/b&gt;&lt;br&gt;    try:&lt;br&gt;        cursor = gp.searchcursor("E:/Data/CityOfSanFrancisco.gdb/FireStations")&lt;br&gt;        row = cursor.Next()&lt;br&gt;        while row:&lt;br&gt;          &lt;i&gt;&lt;font color="#006600"&gt;# Code here...&lt;br&gt;          # for demonstration...this next statement will cause an error&lt;/font&gt;&lt;/i&gt;&lt;br&gt;          y = x&lt;br&gt;          row = cursor.Next()&lt;br&gt;    except arcgisscripting.ExecuteError:&lt;br&gt;        line, filename, err = trace()&lt;br&gt;        gp.AddError("Geoprocessing error on " + line + " of " + filename + " :")&lt;br&gt;        for msg in range(0, gp.MessageCount):&lt;br&gt;            if gp.GetSeverity(msg) == 2:&lt;br&gt;                gp.AddReturnMessage(msg)&lt;br&gt;    except:&lt;br&gt;        line, filename, err = trace()&lt;br&gt;        gp.AddError("Python error on " + line + " of " + filename)&lt;br&gt;        gp.AddError(err)&lt;br&gt;    &lt;b&gt;finally:&lt;/b&gt;&lt;br&gt;        &lt;i&gt;&lt;font color="#006600"&gt;# Close cursor and row objects.&lt;br&gt;        #&lt;/font&gt;&lt;/i&gt;&lt;br&gt;        &lt;b&gt;del row, cursor&lt;/b&gt;&lt;br&gt;            &lt;br&gt;        &lt;i&gt;&lt;font color="#006600"&gt;# Print a message for demo purposes&lt;br&gt;        #&lt;/font&gt;&lt;/i&gt;&lt;br&gt;        gp.AddMessage("\n   ** Cursor and row have been deleted ** \n")&lt;/font&gt;&lt;/pre&gt;&lt;/blockquote&gt;
	  
&lt;p&gt;As illustrated below, the cursor gets deleted even if there is an error in
the script.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.esri.com/Dev/photos/geoprocessing/images/3163/original.aspx" target="_blank"&gt;&lt;/a&gt;&lt;a href="http://blogs.esri.com/Dev/photos/geoprocessing/images/3163/original.aspx" target="_blank"&gt;&lt;img src="http://blogs.esri.com/Dev/photos/geoprocessing/images/3163/640x242.aspx" border="0"&gt;&lt;/a&gt;&lt;br&gt;&lt;/p&gt;

&lt;p&gt;In the main body of the script, the cursor and row object variables are declared and initialized to None (shown in bold in the above code).&amp;nbsp; This ensures that these two variables exist when the del statement in the finally clause is executed; if you try to delete a non-existent variable, an exception will be thrown.&amp;nbsp;&amp;nbsp; &lt;br&gt;&lt;/p&gt;

&lt;h5&gt;About cursors and the del statement&lt;/h5&gt;

&lt;p&gt;The Python &lt;a href="http://www.python.org/doc/2.5.2/ref/del.html"&gt;del statement&lt;/a&gt; deletes a variable.  You can use it to delete any
Python variable, such as a list or dictionary.  However, you rarely need to delete native Python
variables.&amp;nbsp; The del statement is mainly used for deleting non-native
variables, such as geoprocessing cursors and rows obtained from the cursor.  When you delete a cursor and its row, all pending changes to the row (caused by an update or insert cursor) are flushed and all locks on the dataset are removed.&amp;nbsp; &lt;i&gt;You should always delete cursors and rows obtained from the cursor.&lt;/i&gt;&amp;nbsp; When deleting a cursor, delete the row object first, then the cursor.&lt;/p&gt;

&lt;span style="font-weight:bold;"&gt;&lt;br&gt;&lt;/span&gt;&lt;img src="http://blogs.esri.com/Dev/aggbug.aspx?PostID=3169" width="1" height="1"&gt;</description><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/script+tools/default.aspx">script tools</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/Python/default.aspx">Python</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/Tips+and+tricks/default.aspx">Tips and tricks</category></item><item><title>Digging deeper - Troubleshooting geoprocessing errors when using ArcSDE data</title><link>http://blogs.esri.com/Dev/blogs/geoprocessing/archive/2008/09/05/Digging-deeper-_2D00_-Troubleshooting-geoprocessing-errors-when-using-ArcSDE-data.aspx</link><pubDate>Fri, 05 Sep 2008 23:13:00 GMT</pubDate><guid isPermaLink="false">b60b3f0a-e2bd-4be5-8a18-822c697649ab:2601</guid><dc:creator>dmhoneycutt</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.esri.com/Dev/blogs/geoprocessing/comments/2601.aspx</comments><wfw:commentRss>http://blogs.esri.com/Dev/blogs/geoprocessing/commentrss.aspx?PostID=2601</wfw:commentRss><description>&lt;p&gt;Geoprocessing error reporting has significantly improved in the ArcGIS 9.3 release.  
All tractable errors now have a unique error code and each error code has 
a detailed explanation in the help system.  See&lt;a href="http://webhelpdev.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Understanding_geoprocessing_tool_errors_and_warnings"&gt; Understanding geoprocessing tool errors and warnings&lt;/a&gt; for more details.
&lt;/p&gt;


&lt;p&gt;However, at this time, geoprocessing errors that occur when reading or writing 
ArcSDE/DBMS data receive a generic 'catch-all' error message, such as error 00210 when writing output:&lt;/p&gt;

&lt;p&gt;&lt;font face="courier new, courier, mono"&gt;ERROR 000210: Cannot create output E:\Infra\ToolData\Connection to ArcSDE.sde\ken.cities&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;  We are working 
hard to improve reporting of ArcSDE/DBMS geoprocessing errors in future service 
packs and releases.  In the meantime, here are some techniques you can use to 
determine the cause of your error.&amp;nbsp; Note that the cause of the error could be anything from not being able to connect 
to ArcSDE to any number of DBMS or network related issues.&lt;/p&gt;

&lt;h3&gt;Test the connection&lt;/h3&gt;

&lt;p&gt;The first thing you should test is whether your ArcSDE/DBMS connection properties 
are correct.  Open ArcCatalog and navigate to the location containing the .sde 
connection file ("E:\Infra\ToolData" in the above example) and double-click the 
connection, causing a connection to be made to the database.  If a connection cannot be made, you'll receive an error message with details on the cause. If you can make a connection, then your connection properties may be invalid.&amp;nbsp; Right-click the connection and select Connection 
Properties.&amp;nbsp; Visually inspect the connection properties.  Make sure that your 
script or model is using the correct user name when fully qualifying object names. For example, you may have connected to the database as the user 'ken' in the 
connection file, but the data paths in the script or model has a different user 
for the qualified feature class name, as illustrated below. &lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.esri.com/Dev/photos/geoprocessing/picture2608.aspx" target="_blank"&gt;&lt;img src="http://blogs.esri.com/Dev/photos/geoprocessing/images/2608/640x320.aspx" border="0"&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;
If everything seems correct with your connection file, the next investigation step is to set up a client-side SDEINTERCEPT.&lt;/p&gt;
&lt;h3&gt;Client-side SDEINTERCEPT&lt;/h3&gt;
&lt;p&gt;You may need to dig 
deeper by instructing ArcGIS to log information about the communication between 
your computer and the ArcSDE server.  To instruct ArcGIS to log information, and where 
to write the logs, you need to set certain system environment variables.  
The exact technique for setting these variables depends on whether you are 
using Windows or UNIX.  For a full explanation of the variables and how to set 
them, see the web help topic &lt;a href="http://webhelpdev.esri.com/arcgisdesktop/9.3/index.cfm?topicname=the_dbinit.sde_file"&gt;The dbinit.sde file&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;(For a more general introduction to configuring ArcSDE geodatabases, see &lt;a href="http://webhelpdev.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=An_overview_of_configuring_an_ArcSDE_geodatabase"&gt;An Overview of configuring an ArcSDE geodatabase&lt;/a&gt; .)
&lt;/p&gt;
A SDEINTERCEPT file contains all the calls to ArcSDE along with their return values.  
These return values often contain the failure message received from ArcSDE or the error message ArcSDE received from the database.  On Windows, the general 
steps are:

&lt;p&gt;&lt;b&gt;1. Set the following system environment variables:&lt;/b&gt;&lt;/p&gt;


&lt;ul&gt;SDEINTERCEPT=crwft&lt;br&gt;
SDEINTERCEPTLOC=&amp;lt;folder name&amp;gt;&lt;/ul&gt;

&lt;p&gt;&amp;lt;folder name&amp;gt; can be any existing folder on your computer.  
Just be sure to add the slash at the end of the pathname (i.e.: C:\temp\logs\)&lt;/p&gt;


&lt;p&gt;On Windows XP, system environment variables are accessed by right-clicking 
My Computer and clicking Properties.  On the Advanced Tab, click Environment 
Variables, as illustrated below.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.esri.com/Dev/photos/geoprocessing/picture2609.aspx" target="_blank"&gt;&lt;img src="http://blogs.esri.com/Dev/photos/geoprocessing/images/2609/640x383.aspx" border="0"&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;


&lt;p&gt;&lt;b&gt;2. Restart the application that will run the geoprocessing tool 
(such as ArcMap) so that it inherits the new system environment. &lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;3. Run the geoprocessing tool or script that caused the failure.&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;After the task is run, a file named se_intercept.&amp;lt;nnn&amp;gt;  is created in the folder specified 
in the SDEINTERCEPTLOC variable (where &amp;lt;nnn&amp;gt; is a unique number starting at 001).  
This file will contain error and warning messages.  Not all of these 
errors and warnings are issues you need to worry about; some are a normal part 
of using ArcSDE and are just warnings that are automatically handled by ArcSDE.  
It will take some time to learn how to read these files.  If necessary, you can get 
help interpreting the file from ESRI support.  A good strategy is to search the file 
from the bottom up since it's more likely that the real error is near the bottom.&lt;/p&gt;

&lt;h3&gt;ArcSDE Server error logs&lt;/h3&gt;
&lt;p&gt;Each entry in an ArcSDE Server error log contains three pieces of information: the time of the message, the name of the client machine, and the message.&amp;nbsp;  If you can identify the time your error occurred, you can 
search the log for any obvious error messages around the time the error occurred on your client machine.&lt;br&gt;&lt;/p&gt;


&lt;p&gt;ArcSDE Server error logs are written to the location specified by your SDE administrator, 
so you will need to contact your SDE administrator to gain access to these logs.  
(They are written to SDEHOME\etc\&amp;lt;sde_instance_name&amp;gt;.log, such as sde_esri_sde.log).&lt;/p&gt;

&lt;p&gt;To have more detailed information in the ArcSDE Server error logs, see the SDEVERBOSE setting in the topic &lt;a href="http://webhelpdev.esri.com/arcgisdesktop/9.3/index.cfm?topicname=the_dbinit.sde_file"&gt;The dbinit.sde file&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;DBMS logs and trace files&lt;/h3&gt;
&lt;p&gt;Most database management systems have the ability to log detailed information 
about database activities and queries.  Investigating these logs usually 
require assistance from your database administrator as a certain amount of 
database specific expertise is needed to gain useful information from their contents.&lt;/p&gt;

&lt;p&gt;DBMS logs contain information on the state of the database and any database related 
issues that may have occurred.  Things such as the database running out of 
space to store your data, dead locks, database system errors and much more are 
contained in these files.  &lt;/p&gt;

&lt;p&gt;DBMS trace files, on the other hand, are files that contain all the SQL 
statements and their return values for a session in the database.  When you 
request ArcSDE to perform an action, a lot of what is done is the running of SQL 
in the database.  The database trace file will contain all the SQL used by ArcSDE 
and the messages the database returned when running this SQL.  Any unexpected errors 
can be found in these files.&lt;/p&gt;&lt;img src="http://blogs.esri.com/Dev/aggbug.aspx?PostID=2601" width="1" height="1"&gt;</description><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/advanced/default.aspx">advanced</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/script+tools/default.aspx">script tools</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/ArcSDE/default.aspx">ArcSDE</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/Digging+deeper/default.aspx">Digging deeper</category></item><item><title>Tips and Tricks - Debugging Python script tools</title><link>http://blogs.esri.com/Dev/blogs/geoprocessing/archive/2008/09/01/Tips-and-Tricks-_2D00_-Debugging-Python-script-tools.aspx</link><pubDate>Mon, 01 Sep 2008 22:02:00 GMT</pubDate><guid isPermaLink="false">b60b3f0a-e2bd-4be5-8a18-822c697649ab:2573</guid><dc:creator>dmhoneycutt</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.esri.com/Dev/blogs/geoprocessing/comments/2573.aspx</comments><wfw:commentRss>http://blogs.esri.com/Dev/blogs/geoprocessing/commentrss.aspx?PostID=2573</wfw:commentRss><description>&lt;p&gt;If you create your own script tools using Python, you'll appreciate these tips 
and tricks on debugging.&lt;/p&gt;

&lt;p&gt;Coding errors are inevitable and there are two basic ways to find out where the 
error occurred:&lt;/p&gt;

&lt;ol&gt;- Add some form of print statements to your code that help you isolate the problem.&lt;br&gt;
- Use an interactive debugger.&lt;/ol&gt;

&lt;p&gt;Using print statements to discover bugs is an obvious and common method.  Since script 
tools have access to the tool progress dialog, you can edit your script to include 
calls to AddMessage(), AddWarning(), or AddError() to print values and checkpoint 
messages to the progress dialog.  Another variation is to use an independent method 
of returning messages, like the win32ui module's MessageBox method.  This method 
displays a popup dialog.  Since you have to click OK on the dialog to continue 
execution, this method allows you to pace the execution of the script.  Here's an 
example using both methods:
&lt;/p&gt;

&lt;blockquote&gt;
&lt;pre&gt;&lt;font size="2" face="courier new, courier, mono"&gt;import arcgisscripting, win32ui, win32con&lt;br&gt;gp = arcgisscripting.create()&lt;br&gt;n = 5&lt;br&gt;&lt;br&gt;# Print message to progress dialog&lt;br&gt;#&lt;br&gt;gp.AddMessage("Value of n = " + str(n))&lt;br&gt;&lt;br&gt;# Issue a popup dialog with OK and Cancel button&lt;br&gt;#&lt;br&gt;val = win32ui.MessageBox("Value of n = " + str(n), "title", win32con.MB_OKCANCEL)&lt;br&gt;&lt;br&gt;# Based on the button clicked, you can branch execution&lt;br&gt;#&lt;br&gt;if val == 1:&lt;br&gt;    gp.AddMessage("You clicked OK")&lt;br&gt;else:&lt;br&gt;    gp.AddError("You clicked Cancel")&lt;br&gt;    raise arcgisscripting.ExecuteError, "Execution stops due to Cancel button click"&lt;br&gt;&lt;br&gt;gp.AddMessage("This statement reached")&lt;br&gt;&lt;/font&gt;&lt;br&gt;&lt;/pre&gt;
&lt;/blockquote&gt;


&lt;p&gt;The other method is to use a Python IDE (Integrated Development Environment) 
that supports debugging, such as:&lt;/p&gt;

&lt;ol&gt;- Python IDE that is installed with Python&lt;br&gt;
- PythonWin, available on the web at &lt;a href="https://sourceforge.net/projects/pywin32"&gt;https://sourceforge.net/projects/pywin32&lt;/a&gt;&lt;br&gt;
- Commercial systems, such as Wing IDE (&lt;a href="http://wingware.com/"&gt;http://wingware.com&lt;/a&gt;)&lt;/ol&gt;


&lt;p&gt;Debuggers allow you to set breakpoints, step in, out, and over individual lines 
of code, and examine the contents of variables, all without modifying your code.  
Compared to inserting print statements, debuggers are much more efficient and 
usually allow you to quickly isolate your bug.&amp;nbsp; The illustration below shows how to use debug mode in PythonWin.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.esri.com/Dev/photos/geoprocessing/picture2576.aspx" target="_blank"&gt;&lt;img src="http://blogs.esri.com/Dev/photos/geoprocessing/images/2576/450x341.aspx" border="0"&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;One way to use a debugger is to open your script directly in your IDE, modify 
it so that all parameters have values, and then proceed with debugging.  This works 
reasonably well in simple cases.  However, if your script uses layer or table view 
parameters, these variables need created on the fly.  Complex parameters like a 
Field Map or Spatial Reference are hard to create as variables. &lt;/p&gt;

&lt;p&gt;Ideally, you'd like to be able to open your script tool dialog, enter parameters, 
and then have the Python IDE launch with your code ready to be debugged.  You can 
do this with a few simple changes,&amp;nbsp; described below.&lt;/p&gt;

&lt;h4&gt;Use GetParameterAsText()&lt;/h4&gt;

&lt;p&gt;The first step is to modify your script so that it used GetParameterAsText() 
instead of sys.argv[], as discussed in &lt;a href="http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Understanding_script_tool_parameters" target="_blank"&gt;Understanding script tool parameters&lt;/a&gt;&lt;br&gt;&lt;/p&gt;

&lt;p&gt;This is a modification you can keep - there is no need to change your code 
back to using sys.argv[]. 
&lt;/p&gt;
&lt;h4&gt;Create a .bat file that launches your IDE with your .py as an argument&lt;/h4&gt;
&lt;p&gt;The next step is to create a one-line batch file that will be the source of 
your script tool (instead of your .py file).  This single line contains the pathname 
to your IDE plus the full path to the .py file, as illustrated below.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.esri.com/Dev/photos/geoprocessing/picture2571.aspx" target="_blank"&gt;&lt;img src="http://blogs.esri.com/Dev/photos/geoprocessing/images/2571/505x124.aspx" border="0"&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;


&lt;p&gt;Below are 3 examples of how to set your batch (.bat) file for commonly used IDEs.  
You'll need to confirm that the path is correct for your computer.  Other debuggers 
are of course valid options, and can be called in a similar fashion.&lt;/p&gt;

&lt;i&gt;Python IDLE&lt;/i&gt;&lt;br&gt;
&lt;font face="courier new, courier, mono"&gt;"C:\Python25\pythonw.exe" "C:\Python25\Lib\idlelib\idle.pyw" "C:\mytools\myscript.py"&lt;/font&gt;&lt;br&gt;
&lt;br&gt;
&lt;i&gt;PythonWin&lt;/i&gt;&lt;br&gt;
&lt;font face="courier new, courier, mono"&gt;"C:\Python25\Lib\site-packages\pythonwin\Pythonwin.exe" "C:\mytools\myscript.py"&lt;/font&gt;&lt;br&gt;
&lt;br&gt;
&lt;i&gt;Wing IDE 2.1&lt;/i&gt;&lt;br&gt;
&lt;font face="courier new, courier, mono"&gt;"C:\Program Files\Wing IDE 2.1\bin\wing.exe" "C:\mytools\myscript.py"&lt;/font&gt;&lt;br&gt;
&lt;h4&gt;Reset script tool path to the .bat file&lt;/h4&gt;
&lt;p&gt;The next step is to change your script tool properties to use this new batch file.  
In the ArcToolbox window, right-click your script tool and click Properties.  
Click the Source tab and change the script file to be the new .bat file you created, as illustrated below.  
Click OK.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.esri.com/Dev/photos/geoprocessing/picture2572.aspx" target="_blank"&gt;&lt;/a&gt;&lt;a href="http://blogs.esri.com/Dev/photos/geoprocessing/picture2572.aspx" target="_blank"&gt;&lt;img src="http://blogs.esri.com/Dev/photos/geoprocessing/images/2572/328x193.aspx" border="0"&gt;&lt;/a&gt;&lt;br&gt;&amp;nbsp;&lt;/p&gt;

&lt;h4&gt;Execute and debug your tool&lt;/h4&gt;
&lt;p&gt;Now you can open the script tool from the ArcToolbox window, enter the parameters, 
and click OK.  Shortly afterwards, the debugging application specified in your batch 
file will open with your Python script displayed.  At this point, you are free to interact 
with the debugging application as you would normally.  You can set a breakpoint and
let the script run to the breakpoint and use any other options the debugger allows.  
Parameter values you entered in the dialog will get picked up by GetParameterAsText().&lt;/p&gt;

&lt;p&gt;When running the script, any interaction you would normally expect between the script 
and the application will still occur.  So, if you're using methods like AddMessage(), 
AddWarning(), or AddError(), these messages will show up in the application.  If you are 
using progressor methods (new at 9.3), the progressor will be updated in the tool 
dialog as you walk through your script.  Once the script is finished, you can return 
back to the application by closing your debugger.  
&lt;/p&gt;

&lt;h4&gt;Reset the script tool source&lt;/h4&gt;
&lt;p&gt;Once you have fixed your bugs, you can reset the source to the script file from 
the .bat file back to the .py file.&lt;/p&gt;

&lt;br&gt;
&lt;p&gt;Looking ahead to future releases, we will have integrated options that will 
make this process smoother.  But for now, and with a few little tricks, you can 
easily perform your script tool debugging.&lt;/p&gt;&lt;img src="http://blogs.esri.com/Dev/aggbug.aspx?PostID=2573" width="1" height="1"&gt;</description><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/advanced/default.aspx">advanced</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/script+tools/default.aspx">script tools</category><category domain="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/tags/Python/default.aspx">Python</category></item></channel></rss>