Another way to exit a Python script is to simply raise the SystemExit
exception with raise
:
print 'step 1'
raise SystemExit
print 'step 2'
This solution does exactly what sys.exit
does, except that you do not need to import sys
first.
Also, your specific problem was caused by the fact that you were not actually calling the exit
function:
print 'step 1'
exit() # Add () after exit to call the function
print 'step 2'
However, you should not use this solution because it is considered a bad practice. Instead, you should use sys.exit
or raise
as shown above.