Domanda

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.

È stato utile?

Soluzione 5

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

Altri suggerimenti

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

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".

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.

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

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top