This blog describes some techniques/tips on how to debug Python using the Python debugger pdb.

My "Vim Python IDE"The following screen shot shows VIM configured as an almost full function Python IDE - there are 3 windows in VIM. I edit source code in the upper left window and open the lower left window for ipython, which shows imported objects in current namespace; finally, I have pdb debugging the source code at the right side window. All these can be done with a single key stroke inside VIM! (click the image to enlarge and click again to maximize it)

First, I'll show how to use basic Python pdb commands to debug Python code; then I'll show how to setup pdb breakpoints from the source code.

Then, I'll list vim key mapping that inserts breakpoints by just pressing F1 in vim and use F5 key to debug Python code without leaving Vim editor. Use this technique, you can debug Python code and travel back-and-forth between vim and pdb quickly.

At the end, I'll list additional vim key mappings (Thanks ConqueShell!!) that enable a single key stroke to trigger a pdb/ipython/ipdb in a split window inside Vim. This will turn your Vim into a full function Python IDE, you can edit and debug source code, evaluate attributes, objects in one vim screen!

Sometimes, you don't want to go thru the entire code line by line to debug. All you need is to jump into certain point in your code and debug that section only, you can do that by setting up a breakpoint in Python code.

Just add 'import pdb;pdb.set_trace()' at the point where you want to mark as breakpoint then run your code as normal, it will stop at the breakpoint and gives you pdb prompt.

Now, at Vim's insert mode, find the place you want to insert a breakpoint, hit F1 key. To debug your code, (save it first) and hit F5 key, you will be stopped at the breakpoint in a shell, once done debugging just type 'exit' to close pdb and go back to vim continue your coding.

If you have conqueshell installed in vim, using the following Vim mappings, you will be able to open ipython and debug your code from a new buffer in a split window on the same screen on vim. Basically, you turn Vim into a dynamic Python/iPython IDE with a full function debugger!