debugging – How to step through Python code to help debug issues?

debugging – How to step through Python code to help debug issues?

Yes! Theres a Python debugger called pdb just for doing that!

You can launch a Python program through pdb by using pdb or python -m pdb

There are a few commands you can then issue, which are documented on the pdb page.

Some useful ones to remember are:

  • b: set a breakpoint
  • c: continue debugging until you hit a breakpoint
  • s: step through the code
  • n: to go to next line of code
  • l: list source code for the current file (default: 11 lines including the line being executed)
  • u: navigate up a stack frame
  • d: navigate down a stack frame
  • p: to print the value of an expression in the current context

If you dont want to use a command line debugger, some IDEs like Pydev, Wing IDE or PyCharm have a GUI debugger. Wing and PyCharm are commercial products, but Wing has a free Personal edition, and PyCharm has a free community edition.

By using Python Interactive Debugger pdb

First step is to make the Python interpreter to enter into the debugging mode.

A. From the Command Line

Most straight forward way, running from command line, of python interpreter

$ python -m pdb
> .../<module>()

B. Within the Interpreter

While developing early versions of modules and to experiment it more iteratively.

$ python
Python 2.7 (r27:82508, Jul  3 2010, 21:12:11)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type help, copyright, credits or license for more information.
>>> import pdb_script
>>> import pdb
> <string>(1)<module>()

C. From Within Your Program

For a big project and long-running module, can start the debugging from inside the program using
import pdb and set_trace()
like this :

#!/usr/bin/env python
# encoding: utf-8

import pdb

class MyObj(object):
    count = 5
    def __init__(self):
        self.count= 9

    def go(self):
        for i in range(self.count):
            print i

if __name__ == __main__:

Step-by-Step debugging to go into more internal

  1. Execute the next statement… with “n” (next)

  2. Repeating the last debugging command… with ENTER

  3. Quitting it all… with “q” (quit)

  4. Printing the value of variables… with “p” (print)

    a) p a

  5. Turning off the (Pdb) prompt… with “c” (continue)

  6. Seeing where you are… with “l” (list)

  7. Stepping into subroutines… with “s” (step into)

  8. Continuing… but just to the end of the current subroutine… with “r” (return)

  9. Assign a new value

    a) !b = B

  10. Set a breakpoint

    a) break linenumber

    b) break functionname

    c) break filename:linenumber

  11. Temporary breakpoint

    a) tbreak linenumber

  12. Conditional breakpoint

    a) break linenumber, condition

Note:**All these commands should be execute from **pdb

For in-depth knowledge, refer:-

debugging – How to step through Python code to help debug issues?

There is a module called pdb in python. At the top of your python script you do

import pdb

and you will enter into debugging mode. You can use s to step, n to follow next line similar to what you would do with gdb debugger.

Leave a Reply

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