Python – How to debug efficiently with Spyder in Python

debuggingpythonspyder

I like Python and I like Spyder but I find debugging with Spyder terrible!

  • Every time I put a break point, I need to press two buttons: first
    the debug and then the continue button (it pauses at first line
    automatically) which is annoying.
  • Moreover, rather than having the standard iPython console with auto completion etc I have a lousy ipdb>> console which is just garbage.
  • The worst thing is that this console freezes very frequently even if I write prints or simple evaluation to try to figure out what is the bug. This is much worse than MATLAB.
  • Last but not least, if I call a function from within the
    ipdb>> console, and put a breakpoint in it, it will not stop there.
    It seems like I have to put the breakpoint there before I start the
    debugging (Ctrl+F5).

Do you have a solution or maybe can you tell me how you debug Python scripts and functions?

I am using fresh install of Anaconda on a Windows 8.1 64bit.

Best Answer

(Spyder maintainer here) After our 4.2.0 version, released in November 2020, the debugging experience in Spyder is quite good. What we provide now is what people coming from Matlab would expect from a debugger, i.e. something that works like IPython and lets you inspect and plot variables at the current breakpoint or frame.

Now about your points:

  1. If there is a breakpoint present in the file you're trying to debug, then Spyder enters in debug mode and continues until the first breakpoint is met. If it's present in another file, then you still need to press first Debug and then Continue.

  2. IPdb is the IPython debugger console. In Spyder 4.2.0 or above it comes with code completion, syntax highlighting, history browsing of commands with the up/down arrows (separate from the IPython history), multi-line evaluation of code, and inline and interactive plots with Matplotlib.

  3. This is fixed now. Also, to avoid clashes between Python code and Pdb commands, if you have (for instance) a variable called n and write n in the prompt to see its value, we will show it instead of running the n Pdb command. To run that command instead, you have to prefix it with an exclamation mark, like this: !n

  4. This is fixed too. You can set breakpoints in IPdb and they will be taken into account in your current session.