Home > python > Debugging in Python

Debugging in Python

If you are doing debugging with “print“, it’s time to try one of the following methods.

Debugging with pdb/ipdb

If you want a clear and gentle introduction to the usage of the Python debugger “pdb“, read Steve Ferg’s excellent tutorial Debugging in Python.

Here I make a short summary for reference purposes:

  • import pdb” or “import ipdb as pdb“, then “pdb.set_trace()
  • n (next)
  • ENTER (repeat previous)
  • q (quit)
  • p <variable> (print value)
  • c (continue)
  • l (list where you are)
  • s (step into subroutine)
  • r (continue till the end of the subroutine)
  • ! <python command>

ipdb is like pdb but it adds syntax highlightning and completion. You can install it with “sudo pip install ipdb“. If you used pdb with “import pdb“, just change this line to “import ipdb as pdb“. This way the line “pdb.set_trace()” can be left unchanged.

Debugging with Winpdb

Another interesting debugger is Winpdb, which is a platform independent GUI debugger for Python with support for multiple threads, namespace modification, embedded debugging, encrypted communication… It can be installed from the Ubuntu repos (sudo apt-get install winpdb). Tutorial here.

Short summary again:

  • restart (restart debugging session)
  • exit
  • n (next)
  • go (continue)
  • x <python command> (exec, changes state)
  • v <python command> (eval, print to console, no changes in state)
  • j <line> (jump)
  • s (step in subroutine)
  • r (return from subroutine)
  • bp <line> (breakpoint @ line)
  • bp <line>, <expression> (conditional breakpoint @ line)
  • bl (breakpoint list)
  • bc <id> | * (breakpoint clear)

Debugging with the Eric IDE

I would sum up how to debug with the Eric IDE too:

  • F5 (start debugging; untick “Don’t stop at first line” or set a breakpoint)
  • F10 (stop)
  • F7 (next, step in subroutines)
  • F8 (next, step over subroutines)
  • F9 (return, step out of subroutine)
  • F6 (continue, go, run)
  • Shift+F6 (continue till cursor)
  • conditional breakpoints are supported (set a breakpoint, right click on it, edit)

I find Eric’s debugger is much faster than Winpdb.

Update (20141227)
If you want to add a break point to your script and you want to “break out” to ipython, then just add this line:

import ipdb; ipdb.set_trace()
Categories: python Tags: , , , , ,
  1. Tom
    May 18, 2011 at 05:09

    For debugging I use my IDE feature (PyScripter) but there is another approach to do this task codeinvestigator. It sounds interesting.

  2. John B
    July 3, 2013 at 12:36

    Hi I have used PDB as though it is basic it is pretty reliable and always available in a python environment which can only be good !
    On Lubuntu I have tried to use WINPDB but although the window comes up OK the program code is missing from the SOURCE window.
    I get this message –
    *** Starting debuggee…
    *** The debugger does not know how to open a new console on this system. You
    *** can start the debuggee manually with the -d flag on a separate console
    *** and then use the ‘attach’ command to attach to it.

    Any ideas welcome !

    • July 3, 2013 at 22:37

      Nowadays I use pudb. I haven’t written about it yet, so until then check out this blog post.

      • John B
        July 4, 2013 at 10:29

        Hi there – yes PDB is reliable and ever-present. As I’m getting ‘nowhere fast’ with WINPDB, I’ve taken a look at the IDE called IDLE which is showing promise – also it does more debugging which is handy ! Cheers john

  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: