gdb like step by step debugger for Python

Posted on In QA, Tutorial

Any good debugger for Python that is like gdb so that the code can be executed step by step for debugging purpose?

You might check pdb.

You can debug a program such as prog.py by invoking it through pdb:

python -m pdb prog.py

Common gdb commands can be found in pdb:

(Pdb) h

Documented commands (type help <topic>):
========================================
EOF    bt         cont      enable  jump  pp       run      unt   
a      c          continue  exit    l     q        s        until 
alias  cl         d         h       list  quit     step     up    
args   clear      debug     help    n     r        tbreak   w     
b      commands   disable   ignore  next  restart  u        whatis
break  condition  down      j       p     return   unalias  where 

One example for your reference:

$ python -m pdb solution.py 
> /home/zma/.../braille_translation_2/solution.py(1)<module>()
-> def answer(plaintext):

(Pdb) l
  1  ->  def answer(plaintext):
  2       # your code here
  3       # build dict
  4       braillecapmark = "000001"
  5       brailledict = dict()
  6       sample = "the quick brown fox jumps over the lazy dog"
  7       sampleencode = "011110110010100010000000111110101001010100100100101000000000110000111010101010010111101110000000110100101010101101000000010110101001101100111100011100000000101010111001100010111010000000011110110010100010000000111000100000101011101111000000100110101010110110"
  8       for idx in xrange(len(sample)):
  9         brailledict[sample[idx]] = sampleencode[(idx * 6):(idx * 6 + 6)]
 10     
 11       # encode now
(Pdb) b 4
Breakpoint 1 at /home/zma/.../braille_translation_2/solution.py:4
(Pdb) c
> /home/zma/.../braille_translation_2/solution.py(4)answer()
-> braillecapmark = "000001"
(Pdb) l
  1     def answer(plaintext):
  2       # your code here
  3       # build dict
  4 B->    braillecapmark = "000001"
  5       brailledict = dict()
  6       sample = "the quick brown fox jumps over the lazy dog"
  7       sampleencode = "011110110010100010000000111110101001010100100100101000000000110000111010101010010111101110000000110100101010101101000000010110101001101100111100011100000000101010111001100010111010000000011110110010100010000000111000100000101011101111000000100110101010110110"
  8       for idx in xrange(len(sample)):
  9         brailledict[sample[idx]] = sampleencode[(idx * 6):(idx * 6 + 6)]
 10     
 11       # encode now

Leave a Reply

Your email address will not be published. Required fields are marked *