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?
7 Answers
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.

- 3,167
- 2
- 28
- 35
-
I just edited to fix some broken links in this answer, however I haven't actually used PTVS for years as it's Windows only. It's probably still a pretty good option if you're using Windows, but I think most people use vscode these days, which works on other platforms too: https://code.visualstudio.com/docs/python/jupyter-support-py – Tim Rae Feb 28 '23 at 06:14
Try using python debugger
b(reak) [[filename:]lineno | function[, condition]]
or
pdb.set_trace();
More detailed tutorial can be found here.

- 8,620
- 9
- 54
- 69
-
For python3.7+, you can use `breakpoint()`, see [my answer](https://stackoverflow.com/a/71021836/2987828) below. – user2987828 May 11 '22 at 14:54
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...

- 439
- 1
- 4
- 8
-
1Yeah I had a look at it. Seems like a step in the right direction but nowhere near as polished as Matlab. I think a lot of people are going to be migrating to Python from Matlab in the next few years. There is a huge opportunity to make a nice user friendly polished environment for Python that mimics Matlab. Doubt it would take a massive amount of development work b/c unlike Matlab, no work on the language itself is necessary, only combining packages and making a nice IDE/research environment – user1507844 May 06 '13 at 22:40
-
PS: I downloaded Spyder :) I was missing a good debugger and seeing my workspace variables. – Eder Santana May 09 '13 at 17:59
-
How are you finding it compared to Matlab for an IDE? How are you finding Python generally? – user1507844 May 09 '13 at 22:50
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.

- 17,067
- 37
- 114
- 188
-
I've been using PyCharm and am quite happy with it. There are some outstanding tickets to ad support for iPython, real time variables window, etc which would give it Matlab or RStudio like functionality in addition to the already awesome IDE that it is. – user1507844 May 12 '14 at 20:36
-
-
Yeah, it didn't work for graphing last I tried and seemed to choke on some parallel processing stuff, but seemed to work fine for everything else. – user1507844 May 13 '14 at 00:05
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 :)

- 21
- 2
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 .

- 1
- 1

- 15,679
- 27
- 85
- 143
-
Ok thanks, that works as you say. Is there any way to make it work with an IDEs graphical debugger. I'm quite used to the nice graphical debugger in Matlab and find the console debugger a bit unfriendly... – user1507844 Mar 22 '13 at 12:05
-
-
Sorry, misinterpreted your question. Didn't know pycharm vas an IDE, thought it was just some framework you were using :) – TheMeaningfulEngineer Mar 22 '13 at 12:10
-
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 after 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 continue filename.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)
, and plots while debugging.

- 1,116
- 1
- 10
- 33
-
I would like to add "statement" before "with exclamation", but there are currently too many [pending edits on Stack Overflow](https://stackoverflow.com/review/suggested-edits). – user2987828 Feb 22 '23 at 05:14