Quick and Easy Debugging in Python

Comments

12 comments posted
Alternative: invoking PDB from the command line

You can also run a Python program under the debugger by running it like so:

python -m pdb your_file.py [arguments as normal]

It will break before running and you can create breakpoints with the normal commands:

break [filepath]:[line]

As well as not requiring you to change the source to add the breakpoint (which can escape into production!), it means that you can disable the breakpoint during the debugging session.

Additionally, you can put the breakpoint commands (or any other PDB commands) in a .pdbrc file in the current directory or in your home directory.

Posted by xtian (not verified) on Wed, 06/01/2011 - 11:54
:g/re/d?

Why would you use

:g /pdb\.set_trace()/normal! Vd

when

:g /pdb\.set_trace()/d

does the same and is much simpler? Am I missing something?

Posted by Marius Gedminas (not verified) on Wed, 06/01/2011 - 08:36
Shorter Vim sequence to delete pdb statement

Instead of this Vim sequence to remove the set_trace calls:

:g /pdb\.set_trace()/normal! Vd

You could use this shorter invocation:

:g /pdb\.set_trace()/d

The ex-mode command :d deletes a line, so you don't need
the more verbose "normal! Vd". See more information at:

:help :d

As another example, I frequently use the :g//d idiom for deleting blank lines:

:g/^\s*$/d

Thanks for the interesting post.

Posted by Michael Henry (not verified) on Wed, 06/01/2011 - 06:05
Thanks for that!

Thanks for pointing that out. There's always something new to learn about Vim, and it always gets better!

Posted by Jeet Sukumaran on Wed, 06/01/2011 - 12:37
Graphical debuggers are good for newbies and overviews

Although I can appreciate the minimality of dropping into a console-based debugger like pdb, there's a lot to be said about using graphical debuggers. For one, they are much more immediately approachable for novice programmers who have not used debuggers before. To this end, I wrote a full tutorial for WinPDB that takes under an hour to work through.

On top of that, the ability to simultaneously display all variables and their values in a given context without having to explicitly request the debugger to print out each can save a lot of time and keystrokes. Often when I begin debugging, I do not know exactly which variable I should be monitoring, if even the scope. A graphical debugger like WinPDB means that I don't have to take wild guesses but can get an overview of the entire variable space once I reach my breakpoint with only a glance or maybe a few mouse clicks.

Posted by Chris Lasher (not verified) on Tue, 05/31/2011 - 13:27
From Ipython

I use Vim + Ipython and I love it. The Ipython debugger has pretty colors. I put the following in my scripts up where the imports are:

def run_from_ipython():
try:
__IPYTHON__
return True
except NameError:
return False

if run_from_ipython():
from IPython.Debugger import Tracer
debug = Tracer()
else:
def debug():
print ('Warning: debugging mark present.')
pass

Now I just write "debug()" wherever I want to enter the debugger. The cool thing about the ipython debugger is that it has tab-completion of names and class attributes.

Posted by Jorgen (not verified) on Tue, 05/31/2011 - 09:47
Why does your python code

Why does your python code have semicolons?

Posted by chewxy (not verified) on Mon, 05/30/2011 - 09:45
So that the import and method

This is the usage shown in the official Python documentation (http://docs.python.org/library/pdb.html). The reason (in my case, at least, and probably that in the Python docs) is that the import and method call can be placed on one line, making both insertion and removal of the debugging code easy (and even partially automatable). It does not make it to production code. Remember: foolish consistency is the hobgoblin of little minds (PEP-8).

Posted by Jeet Sukumaran on Mon, 05/30/2011 - 12:14
ipdb
Posted by Julien (not verified) on Mon, 05/30/2011 - 06:57
ipdb is great!

Thanks for pointing that out!

Posted by Jeet Sukumaran on Mon, 05/30/2011 - 12:15
I often put "from pdb import

I often put "from pdb import set_trace as brake" at the top of a file I'm working on. That way I can just put "brakepoints" in (break is a reserved word), and it's much easier to type "brake()" than "import pdb; pdb.set_trace()" whenever I want to set a breakpoint. Also, thanks for reminding me about code.interact.

Posted by Keith (not verified) on Mon, 05/30/2011 - 05:11
Nice

Nice suggestion. We could go one step further and do something like "from pdb import set_trace as _DEBUG", or some such, which would also make the breakpoints easy to spot visually when scanning code.

Posted by Jeet Sukumaran on Mon, 05/30/2011 - 12:18

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a biological visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.