code: def _exception(etype, eval_, etrace):
          
          if hasattr(sys, 'ps1') or not sys.stderr.isatty():
              # we are in interactive mode or we don't have a tty-like
              # device, so we call the default hook
              sys.__excepthook__(etype, eval_, etrace)
          else:
              # we are NOT in interactive mode, print the exception...
              traceback.print_exception(etype, eval_, etrace, limit=2,
                                        file=sys.stdout)
              six.print_()
              # ...then start the debugger in post-mortem mode.
              pdb.pm()
docs: Wrap exception in debugger if not in tty