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 an arcpy tool script for ArcGIS 10.0 that has two major functional sections. The user can choose whether or not to run the second section. If the user chooses NOT to run the second section, I simply want to run a cleanup function and exit the script with a sys.exit(0) without having an error message posted to the tool results window. There are two major threads here in GIS-SE about exiting arcpy scripts, but the solutions therein don't specifically address the error message. The general structure of the code is as follows:

import sys
##import arcpy

def CleanUp():
    print 'Cleaning up ...\n'

def finish():
    CleanUp()
    print 'Exiting ...'
    sys.exit(0)

do_more = False  #or True ... input from user

#Section 1:  do some stuff
print 'Doing some stuff ...\n'

if not do_more:
    finish()

#Section 2:  do more stuff
print 'doing more stuff ...'
CleanUp()

If I run this code in the Python interpreter outside of ArcGIS/arcpy, it works as I'd expect, exiting gracefully with no error message; however, in my arcpy script with this same structure, the script exits, but a SystemExit error message is posted to the tool results window. Is there a way to make the arcpy tool script eat the exception and bury the SystemExit error message?

share|improve this question
    
Just taking a wild stab here, but is it possible that you could "del arcpy" before calling the SystemExit in your script? –  Dan Patterson Aug 31 '11 at 14:47
    
@Dan - I doubt it, but it's an interesting idea ... hmmm ... what the heck. I'll give it a whirl. –  celticflute Aug 31 '11 at 18:00
1  
thank you for posting concise code example which clearly illustrates the logic and leaves out the extras. +1 –  matt wilkie Aug 31 '11 at 19:32
add comment

3 Answers

up vote 8 down vote accepted

Short answer: fixed in 10.1. For now, you're going to need to add an extra level of indentation. This might encourage some useful refactoring to your code though. Any time you get a large number of lines in a single script/routine, you'll want to think about cutting it up into smaller sections (functions, classes) anyway.

def main():
    <your code>

if __name__ == "__main__":
    try:
        main()
    except SystemExit as err:
        pass
share|improve this answer
    
Thanks, Jason. Yes, I should bite the bullet and refactor. It's just hard to do that with the client breathing down my neck. Such is life. –  celticflute Aug 31 '11 at 17:57
add comment

I don't know how to 'bury' the SystemExit error message, but you could always restructure your code to avoid calling sys.exit

import sys
##import arcpy

def DoMoreStuff():
    print 'doing more stuff ...'

def CleanUp():
    print 'Cleaning up ...'

def finish():
    CleanUp()
    print 'Exiting ...'

do_more = False  #or True ... input from user

#Section 1:  do some stuff
print 'Doing some stuff ...'

if do_more:DoMoreStuff()

finish()
share|improve this answer
    
True, but the two sections in my arcpy script are rather huge. I would rather not create yet another level of indentation and have to deal with the scoping issues that would ensue ... but, yes, this would be a solution. What's really frustrating is that the error message appears only when you use sys.exit in arcpy. Out in regular Python the message isn't posted, because the SystemExit exception isn't considered an error. –  celticflute Aug 31 '11 at 14:00
add comment

ArcPy messes with your Python environment. In short, either don't do sys.exit() or find/call arcpy's "cleanup-before-exit" (if one exists) function before calling sys.exit().

share|improve this answer
2  
The way that Python's executable handles sys.exit() is a sort of dirty trick -- calling exit raises a SystemExit exception and the top-level interpreter, which normally prints a traceback, will detect the exception type and terminate the interpreter. Embedding a Python interpreter in another application, such as the ArcGIS family of executables, requires a nontrivial amount of implementation-specific code such as this. –  Jason Scheirer Aug 31 '11 at 15:49
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.