The following code will drop you back into the Python Interpreter or "debug mode" anytime debug_mode() is called or an unhandled exception occurs. Useful for debugging when you want to automate a portion of a script then get manual control.
import sys,os,readline
def debug_exception(type, value, traceback):
# Restore redirected standard I/O
sys.stdin = sys.__stdin__
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
# Kick the interpreter into interactive mode and call the original
# exception handler.