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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With