Debugging with breakpoints from console in Python

debugging, ipython, matlab, python

Solution

I would like to recommend using Python Tools for Visual Studio. It is free and open source, although Visual Studio itself is obviously not open source, the free version is very functional with commercial use permitted. Additionally, students and staff of most academic institutions will often have free access to Visual Studio Ultimate.

If your program is stopped at a breakpoint, you can open "Python Debug Interactive" (from tools->python tools), which will open an interactive python shell with access to all of the variables available in your program namespace at the breakpoint, in the same way that you can do in Matlab.

Hovering over the variables with your mouse in the source code also shows the value, bringing up the "locals" window more or less simulates the workspace viewer in Matlab, and you can also "watch" specific variables. I don't know if it's safe to edit the variables through this interface though, use with caution!

Unfortunately PTVS doesn't have nested breakpoints, which is a quite useful feature in the Matlab debugger. So if you are stopped at a breakpoint and you call a method from the debug interactive window, any breakpoints in the method will not work. See this related question.

The arrow key based command history in the debug shell is quite primitive compared to Matlab or ipython, and the Intellisense is not as good as it is for native .net languages, but I've been using it solidly for the last half-year or so now, and don't really feel like I'm missing much from Matlab, other than the excellent documentation.

One other thing to be aware of is that the code execution performance when in debug mode is MUCH slower, so I recommend either running your code without debug mode (using "Ctrl+F5" instead of "F5") for best performance, or the new mixed mode debugger if you need both breakpoints and good performance.

Problem

I'm trying to migrate from Matlab to python. One of the things that is nice about Matlab is that when debugging I can put a breakpoint in some code and do something to call that code form the command line. Using PyCharm + IPython I haven't found a way to do this in Python. It seems I have to run an entire script in debug mode to do any debugging rather than being able to do so from a simple command. I suppose I could write a one line script with the command I'm interested in, but it seems like there should be a better way. What is the Python way of doing this?

Original source

Related problems