From what I have read, there are two ways to debug code in Python:
With a traditional debugger such as pdb
or ipdb
. This supports c
Prefixing an "!" symbol to commands you type in pdb seems to have the same effect as doing something in an IPython shell. This works for accessing help for a certain function, or even variable names. Maybe this will help you to some extent. For example,
ipdb> help(numpy.transpose)
*** No help on (numpy.transpose)
But !help(numpy.transpose) will give you the expected help page on numpy.transpose. Similarly for variable names, say you have a variable l, typing "l" in pdb lists the code, but !l prints the value of l.
From python 3.2, you have the interact
command, which gives you access to the full python/ipython command space.
Debugging inside IPython
Cell Example:
%%debug
...: for n in range(4):
...: n>2
IPython inside debugging
pytest ... --pdbcls=IPython.terminal.debugger:TerminalPdb --pdb
breakpoint()
, python -m ipdb
, etc.I agree with the OP that many things MATLAB does nicely Python still does not have and really should since just about everything in the language favors development speed over production speed. Maybe someday I will contribute more than trivial bug fixes to CPython.
https://github.com/ipython/ipython/commit/f042f3fea7560afcb518a1940daa46a72fbcfa68
See also Is it possible to run commands in IPython with debugging?
What about ipdb.set_trace() ? In your code :
import ipdb; ipdb.set_trace()
update: now in Python 3.7, we can write breakpoint()
. It works the same, but it also obeys to the PYTHONBREAKPOINT
environment variable. This feature comes from this PEP.
This allows for full inspection of your code, and you have access to commands such as c
(continue), n
(execute next line), s
(step into the method at point) and so on.
See the ipdb repo and a list of commands. IPython is now called (edit: part of) Jupyter.
ps: note that an ipdb command takes precedence over python code. So in order to write list(foo)
you'd need print(list(foo))
, or !list(foo)
.
Also, if you like the ipython prompt (its emacs and vim modes, history, completions,…) it's easy to get the same for your project since it's based on the python prompt toolkit.
The Pyzo IDE has similar capabilities as the OP asked for. You don't have to start in debug mode. Similarly to MATLAB, the commands are executed in the shell. When you set up a break-point in some source code line, the IDE stops the execution there and you can debug and issue regular IPython commands as well.
It does seem however that step-into doesn't (yet?) work well (i.e. stopping in one line and then stepping into another function) unless you set up another break-point.
Still, coming from MATLAB, this seems the best solution I've found.
You can use IPython's %pdb magic. Just call %pdb
in IPython and when an error occurs, you're automatically dropped to ipdb
. While you don't have the stepping immediately, you're in ipdb
afterwards.
This makes debugging individual functions easy, as you can just load a file with %load
and then run a function. You could force an error with an assert
at the right position.
%pdb
is a line magic. Call it as %pdb on
, %pdb 1
, %pdb off
or %pdb 0
. If called without argument it works as a toggle.