Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ipython -pylab debugging: Can I stop the execution at a specific line and drop into the shell?

When writing python code (mostly numpy + matplotlib), I usually just type the code in vim and run the program to test it:

python2 foo.py

Occasionally, when this is not sufficient and I need to inspect the problem more thoroughly, I just launch the program in ipython: ipython -pylab foo.py, and then inspect the variables, test some commands and so on. I like ipython, because of the tab completion and the availability of bash commands.

This worked well enough for me, but now my programs grew bigger and include many subroutines (in multiple files). The ipython approach doesn't work any more, because it always runs the complete code till the end of foo.py (when it drops into the pylab shell). What I'd like to do instead is, stop execution at a given line in a subroutine (could be in another file) and inspect variables there. I.e. set a break point at which the pylab shell kicks in.

Is there an easy way to adapt my ipython way of working? E.g. stop at a line in bar.py

ipython -pylab --stop-at bar.py:423 foo.py

or, stop at a subroutine name in bar.py

ipython -pylab --stop-at bar.py:subroutine-name foo.py
like image 844
Sebastian Avatar asked Apr 27 '11 09:04

Sebastian


1 Answers

You can import the pdb module into the code and then add a pdb.set_trace() call where you would like to have the code stop. Ipython will drop into the interactive debugger, and you are free to step though your code as you wish.

like image 65
talonmies Avatar answered Sep 24 '22 05:09

talonmies