Pdb is Interactive debugging environment for Python programs. It includes features to pause a program, look at the values of variables, and watch program execution step by step and all this on terminal itself, you don’t need to install any IDE.

Let’s learn some basic of PDB.

Starting the Debugger

There are three ways to start the debugger

  1. From the Command line.
    “python -m pdb test.py”
  2. Within the interpreter.
    1
    2
    3
    
        import pdb
        import test
        pdb.run("test.start()")
        import pdb
        import test
        pdb.run("test.start()")
  3. Within the program
    1
    2
    
        import pdb
        pdb.set_trace()
        import pdb
        pdb.set_trace()

Debugger Commands

once you enter into debugger prompt then you can run following commands on debug prompt

  1. b or break
    break command used for setting breakpoints in code.
    Option available for break command

    1. Line Number:
      (pdb) break 17
      This will set break point on line no. 17 of your program.
    2. Filename:
      if you imported another module in your program, then you can set break point in imported module as
      (pdb) break filename:linenumber
      For example “(pdb) break add.py:180″
      this will set break point on line no. 180 in add.py module.
    3. Function Name:
      (pdb) break func_name
      For Example : “(pdb) break add”
      when you provide function_name to break command then pdb will set break point on first line of function.
  2. s or step
    Step command will execute current line of program and stop at the next execution   point.
  3. c or cont
    “continue” command will continue execution of program only stop when break point encountered.
  4. l or list
    List command will list 11 lines around current line (five before and five after)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    (Pdb) list
     
    zipcode = ''
     
    state_zip_regex = r'((?P<state>.*)\s(?P<zip>.*))$'
    address_split = address.split(',')
     
    B-> state_zip_str = address_split[-1].strip() 
    state_zip_match = re.search(state_zip_regex, state_zip_str)
    (Pdb) list
    
    zipcode = ''
    
    state_zip_regex = r'((?P<state>.*)\s(?P<zip>.*))$'
    address_split = address.split(',')
    
    B-> state_zip_str = address_split[-1].strip() 
    state_zip_match = re.search(state_zip_regex, state_zip_str)
  5. run
    Run command is used to restart your program at any point when you are debugging your program.you can also provide command line argument to run command as follows

    1
    2
    3
    
    (Pdb) run arg1 arg2
    Restarting test.py with arguments:
        arg1 arg2
    (Pdb) run arg1 arg2
    Restarting test.py with arguments:
    	arg1 arg2

These are just some basic commands of PDB that I use everyday, if you want explore more on pdb then please refer following link
http://pymotw.com/2/pdb/

Follow on Google+ LinkedIn Updated on December 26, 2013 at
Post your comment

All the fields must be filled out before posting.