10

I've recently moved from Matlab to Python. Python is a much better language (from the point of view of a computer scientist), but Python IDEs all seem to lack one important thing:

A proper interactive debugger.

I'm looking for:

  • The ability to set breakpoints graphically by clicking next to a line of code in the editor.

  • The ability to run ANY CODE while stopped in the debugger, including calling functions from my code, showing new windows, playing audio, etc.

  • When an error occurs, the debugger should automatically open an interactive console at the error line.

  • Once done with the interactive console, you can resume normal execution.

Matlab has all these features and they work incredibly well, but I can't find them anywhere in Python tools.

I've tried:

  • PyCharm: the interactive console is clunky, often fails to appear, and crashes all the time (I've tried several different versions and OSs).

  • IPython: can't set breakpoints -Launching a Python console programatically: you have to stop your code, insert an extra line of code, and run again from the beginning to do this. Plus, you can't access functions already imported without re-importing them.

Being able to debug and fix problems THE FIRST TIME THEY APPEAR is very important to me, as I work in programs that often take dozens of minutes to re-run (computational neuroscience).

CONCLUSION: there is NO way to do all of these in Python at the moment. Let us hope that PyLab development accelerates.

Louise
  • 1,063
  • 1
  • 9
  • 20
  • 5
    are you familiar with the pdb module? It doesn't do everything you ask, but it's still quite powerful. Knowing whether you're aware of it or not might help people write a more useful answer. – Bryan Oakley Dec 12 '13 at 15:10
  • Spyder also has some of the functionality you want--it might be worth a look – duhaime Dec 12 '13 at 15:13
  • 1
    Eclipse has an excellent pydev extension which does exactly what you described. I'm not a big fan of it (cause I don't like IDE's in general) but most people I know who use it are very pleased with it – yuvi Dec 12 '13 at 15:16
  • 1
    Note that even in Matlab, if the debugger opens due to an error (`dbstop if error`), you **cannot** resume normal execution. This is only possible with ordinary breakpoints. – gerrit Dec 12 '13 at 15:23
  • 1
    Hint, rather than asking a new question, you could have [improved your previous question](http://stackoverflow.com/q/20529521/974555) — "on hold" does not mean the question is gone and lost forever, questions on hold can very well be re-opened if improved. – gerrit Dec 12 '13 at 15:47
  • @gerrit True, well pointed out. You can however "fake" this by calling a function that continues your code, or (dirty) selecting the rest of your code and hitting Evaluate. It's dirty but if it saves me another three hours of re-running, I'm glad of it. – Louise Dec 13 '13 at 17:27
  • @BryanOakley yes, but no graphical breakpoints. This is really important if you have to set hundreds of them per day (and vital for dynamic breakpoints). – Louise Dec 13 '13 at 17:28
  • Actually in Pyzo you can set breakpoints very easily. I am very impressed by how similar to MATLAB it is =D – andrea Mar 22 '16 at 19:41

6 Answers6

5

At the top of your code, write

import pdb

And within your code, use the following statement wherever you want to debug.

pdb.set_trace()

You will have an interactive shell thus, whenever the set_trace() statement is met.

You can then use step(s), next(n), continue(c) and so on to check the execution flow, and print values of variables like print var

For more details on pdb, refer here

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • 1
    Have looked at this, no graphical breakpoints. When you have to set hundreds of breakpoints, inserting lines of code is a bad way to do it. You also can't insert breakpoints during execution this way. – Louise Dec 13 '13 at 17:24
  • 1
    Why would you have to set "hundreds of breakpoints"? You're most likely doing something wrong. – Henry Henrinson Feb 25 '20 at 16:06
4

There are many Python IDEs. That was a topic here: What IDE to use for Python?

  • "The ability to set breakpoints graphically by clicking next to a line of code in the editor."

PyDev has this. Double-click in the gray margin bar.

  • "The ability to run ANY CODE while stopped in the debugger, including calling functions from my code, showing new windows, playing audio, etc."

PyDev has this. It's not the only one. PyScripter's stated features seem to include this.

  • "When an error occurs, the debugger should automatically open an interactive console at the error line."

PyDev does this. (I think. Or at worst do you need to double-click on the console message that states the error's location in the code?)

  • "Once done with the interactive console, you can resume normal execution."

PyDev has this. It's called "resume". It's what the green "play" triangle in a toolbar does. Some other software calls this feature "continue".

Community
  • 1
  • 1
ajm475du
  • 361
  • 1
  • 5
  • 1
    IIRC, what's missing in PyDev is integration with the scientific tools like IPython and matplotlib. Integration with matplotlib means that if you run `plot(a, b)`, you get your prompt back before closing the plot window. This is quite essential for many interactive scientific applications. But it's been a while since I checked so I might be wrong. – gerrit Dec 12 '13 at 15:29
2

I've been searching for the same, but unfortunately Python IDEs are not as well-featured as Matlab's at this point. For scientific programming, you will also want graphics/plotting to run in an entirely different thread, so IPython integration is essential. As far as I can tell, the Matlab IDE feature to change the workspace from the debugger, which then affects code running subsequently, is quite unique. Each of the features exist in some IDE, but none exist in all:

  • Spyder has good integration with scientific tools, but its debugging is limited to the built-in pdb, which lacks the requirement to execute any code and have this code affect the namespace after continuing.
  • PyDev and PyCharm, and quite a few others, have decent debugging features, but I don't think it has good integration with scientific tools. That means, if you plot, you lose access to your prompt. Not good.

As far as I've experienced, the closest comes Wing IDE. It is a propietry product, but if you're making the transition from Matlab 89$/year for non-commercial-use should be acceptable (you can evaluate it first). But for me, I've ultimately settled to alter my workflow, and not using any sophisticated IDE at all. When I looked was some years ago, so perhaps the situation has improved.

You might also be interested in this article from April 2013, Evaluating IDEs for Scientific Python. It doesn't really reach a conclusion either.

gerrit
  • 24,025
  • 17
  • 97
  • 170
0

Seeing as you are comming from Matlab, I would suggest you give a look at

Python(x,y)

The page decribes it as follows:

Python(x,y) is a free scientific and engineering development software for numerical computations, data analysis and data visualization based on Python programming language, Qt graphical user interfaces and Spyder interactive scientific development environment.

It will not cater to all your wishes, but it certainly made me feel comfortable when I started out with Python, coming from Matlab.

Hope it helps

Kraay89
  • 919
  • 1
  • 7
  • 19
  • `Python(x,y)` uses `Spyder`, which relies on the simple `pdb` debugger, thereby not satisfying the *ability to run any code* wish. The `pdb` environment differs from the ordinary `python`/`ipython` environment. – gerrit Dec 12 '13 at 15:32
  • added that to the answer (sort of) – Kraay89 Dec 12 '13 at 15:35
0

You can do all this in the iPython Notebook. Use the magic command %pdb to stop on error.

Louise
  • 1,063
  • 1
  • 9
  • 20
0

If using the command line,

alias ipythondebug='ipython --InteractiveShell.pdb true'

in your ~/.profile will give you debug on error like Matlab. This of course requires ipython installed.

Not sure about the resuming part.

You can also edit the ipython config file if you want the debug on error to be permanent. See https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-pdb

Anton
  • 365
  • 2
  • 12