[Solved-4 Solutions] Starting python debugger automatically on error



Error Description:

Starting python debugger automatically on error

Solution 1:

  • We can use traceback.print_exc to print the exceptions traceback.
  • Then use sys.exc_info to extract the traceback and finally call pdb.post_mortem with that traceback
import pdb, traceback, sys

def bombs():
    a = []
    print a[0]

if __name__ == '__main__':
    try:
        bombs()
    except:
        type, value, tb = sys.exc_info()
        traceback.print_exc()
        pdb.post_mortem(tb)
 
click below button to copy the code. By - python tutorial - team
  • If we want to start an interactive command line with code.interact using the locals of the frame where the exception originated we can do
import traceback, sys, code

def bombs():
    a = []
    print a[0]

if __name__ == '__main__':
    try:
        bombs()
    except:
        type, value, tb = sys.exc_info()
        traceback.print_exc()
        last_frame = lambda tb=tb: last_frame(tb.tb_next) if tb.tb_next else tb
        frame = last_frame().tb_frame
        ns = dict(frame.f_globals)
        ns.update(frame.f_locals)
        code.interact(local=ns)
 
click below button to copy the code. By - python tutorial - team

Solution 2:

  • When Python runs a script and an uncatched exception is raised, a traceback is printed and the script is terminated.
  • Python2.1 has introduced sys.excepthook, which can be used to override the handling of uncaught exceptions.
  • This allows to automatically start the debugger on an unexpected exception, even if python is not running in interactive mode.
  • Code:
# code snippet, to be included in 'sitecustomize.py'
import sys

def info(type, value, tb):
   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__(type, value, tb)
   else:
      import traceback, pdb
      # we are NOT in interactive mode, print the exception...
      traceback.print_exception(type, value, tb)
      print
      # ...then start the debugger in post-mortem mode.
      pdb.pm()

sys.excepthook = info
 
click below button to copy the code. By - python tutorial - team
  • The above code should be included in 'sitecustomize.py', which is automatically imported by python.
  • The debugger is only started when python is run in non-interactive mode.

Solution 3:

python -m pdb -c continue myscript.py
 
click below button to copy the code. By - python tutorial - team
  • If we don't provide the -c continue flag then we'll need to enter 'c' (for Continue) when execution begins. Then it will run to the error point and give us control there.

Solution 4:

  • We just checked python -?, and if we use the -i command we can interact where our script stopped.
  • So given this script:
testlist = [1,2,3,4,5, 0]

prev_i = None
for i in testlist:
    if not prev_i:
        prev_i = i
    else:
        result = prev_i/i
 
click below button to copy the code. By - python tutorial - team
  • We can get this output!
PS D:\> python -i debugtest.py
Traceback (most recent call last):
  File "debugtest.py", line 10, in <module>
    result = prev_i/i
ZeroDivisionError: integer division or modulo by zero
>>>
>>>
>>> prev_i
1
>>> i
0
>>>
 python modes debugging code type

Learn python - python tutorial - python modes debugging code type - python examples - python programs


Related Searches to Starting python debugger automatically on error