user1507844
user1507844

Reputation: 6543

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?

Upvotes: 10

Views: 8331

Answers (7)

user2987828
user2987828

Reputation: 1137

To enter a permanent breakpoint in python3.7+, insert the expression breakpoint() in the source file. No more need to import anything, even pdb. In non-compiled Matlab the equivalent to breakpoint() is the statement keyboard.

The global picture is that after python's debug prompt (Pdb) (shown by breakpoint()) you may prefix python statements looking like debugging statements with exclamation mark (!), whereas Matlab's debugging commands starts with db to lift ambiguity, and some of them only works after debug prompt K>.

I am also migrating from Matlab to Python. I am used to debugging inside matlab -nodisplay, and am switching to python3.10 without IDE. These are pretty similar, as can be seen in table below, which is a debugging-related rosetta stone.

You can create other breakpoints, using command tb or b, from that (Pdb) prompt (or in your file $HOME/.pdbrc). As their Matlab's counterpart dbstop, you can set them for another file, another line and let them be conditional.

Here is a short rosetta stone:

python python's available shortcut Matlab
breakpoint() keyboard
where w dbstack
cont c dbcont
up u dbup
down d dbdown
step s dbstep
next n dbnext
print expression p expression expression
!nonlocal var;var= var=
break b dbstop and dbstatus
clear cl dbclear
list l dbtype
display d variable window
help h help
pp vars() save('b.mat');disp(load('b.mat'))
python -m pdb -c continuefilename.py dbstop if error

Matlab does have no equivalent for:

  • ignore n p (n-th breakpoint will only trigger prompt after p-th execution)
  • prettyprint expression (shortcut: pp)
  • jump codelinenumber (shortcut: j)

I still don't know if Python has any usable equivalents for:

  • save and load except for pp vars()
  • evalin("caller",expr) but sympy.var knows to alter the caller namespace
  • plots while debugging.

Upvotes: 0

Tim Rae
Tim Rae

Reputation: 3186

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.

Upvotes: 5

jakvb
jakvb

Reputation: 21

In console create function where you use pdb.set_trace(), then function you want debug.

>>> import pdb

>>> def f():
...     pdb.set_trace()
...     my_function()
... 

Then call created function:

>>> f()
> <stdin>(3)f()
(Pdb) s
--Call--
> <stdin>(1)my_function()
(Pdb) 

Happy debugging :)

Upvotes: 1

Donbeo
Donbeo

Reputation: 17617

I have been moved from matlab and R to python. I have tried different editors so I can give you some advices.

1- Spyder is the closer to matlab but my impression is that it is not very good. It often crash when I start to run long simulation with a lot of data. If you are new to python I suggest you to use this one for a while and then move to something else.

2- emacs python mode. Works very well . In my opinion it is difficult to configure and probably not the best choice if you are not familiar with python.

3- pycharm. I have just started to use pycharm and it seems to be very good (this reminds my Rstudio). I do not think that this supports an interactive console like the one inside spyder or emacs. You can still obtain something similar in the debug mode

4- A lot of people love ipython notebook but I think that this is not a good choice for long code. It is good if you want something easy to visualize.

Upvotes: 2

Eder Santana
Eder Santana

Reputation: 449

Have you tried Spyder??? This is an open source IDE that looks very similar to Matlab. It also provides the debugger that you want. https://code.google.com/p/spyderlib/

PS: I'm also migrating to python, but I'm avoiding things like that because I want to start with an empty mind. :) But I read lots of those from Matlab to Numpy texts...

Upvotes: 2

TheMeaningfulEngineer
TheMeaningfulEngineer

Reputation: 16329

Since you mentioned you are using ipython, you can also check ipdb.

You have to install it first via pip or easy_install. Etc:

pip install ipdb

The usage is the same as pdb. The ipython console will popout where you placed ipdb.set_trace() from where you can check/change local an global variables, check their documentation and types, step into the code of incoming functions (with 's' you'll go to the definition of code123()), etc.

import ipdb;

code000()
ipdb.set_trace();
code123()

Also a tip on how to get the functionalities of ? from ipython (regarding getting the documentation of functions and modules while in the debugger). This answer .

Upvotes: 0

arulmr
arulmr

Reputation: 8836

Try using python debugger

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

or

pdb.set_trace();

More detailed tutorial can be found here.

Upvotes: 4

Related Questions