Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging with breakpoints from console in Python

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?

like image 483
user1507844 Avatar asked Mar 22 '13 11:03

user1507844


People also ask

How do you debug a Python console?

Directly use command python pdg-debug.py without -m pdb to run the code. The program will automatically break at the position of pdb. set_trace() and enter the pdb debugging environment. You can use the command p variable to view the variables or use the command c to continue to run.

How do you set a breakpoint in a Python script?

Just use python -m pdb <your_script>. py then b <line_number> to set the breakpoint at chosen line number (no function parentheses). Hit c to continue to your breakpoint. You can see all your breakpoints using b command by itself.

How do you breakpoint in debugging?

To set a breakpoint in source code: Click in the far left margin next to a line of code. You can also select the line and press F9, select Debug > Toggle Breakpoint, or right-click and select Breakpoint > Insert breakpoint. The breakpoint appears as a red dot in the left margin.

How do I use debug console?

Debug Console REPL# To open the Debug Console, use the Debug Console action at the top of the Debug pane or use the View: Debug Console command (Ctrl+Shift+Y). Expressions are evaluated after you press Enter and the Debug Console REPL shows suggestions as you type.


2 Answers

Try using python debugger

b(reak) [[filename:]lineno | function[, condition]]

or

pdb.set_trace();

More detailed tutorial can be found here.

like image 195
arulmr Avatar answered Oct 07 '22 22:10

arulmr


I would like to recommend using Python Tools for Visual Studio. It is free and open source, and although Visual Studio itself is obviously not open source, there is ("PTVS Integrated") which comes with a free and very functional version of Visual Studio with commercial use permitted. Additionally, students and staff of most academic institutions will have free access to Visual Studio Ultimate via Dreamspark.

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.

like image 33
Tim Rae Avatar answered Oct 07 '22 23:10

Tim Rae