Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python spyder - debug only current cell or selection?

Similarly to these (very useful!) two functions:

"Run current cell" "Run selection or current line"

Is it possible to do this with debugging? I dont want to start from the top of my large script files every time I debug.

I'm using Spyder version 3.2.4

like image 908
HerrErik Avatar asked Dec 05 '17 13:12

HerrErik


People also ask

How do I run a selection in Spyder?

In Spyder 4.0.1, the keyboard shortcut for 'run cell' is set to Ctrl+Return but for 'run selection' it is set to F9. You can use F9 to run a selection or if you prefer to use Ctrl+Return, you can go to Tools -> Preferences -> Keyboard shortcuts. Search for 'run selection', double click and set Ctrl+Return as the 'New shortcut'

How to debug a file in Spyder?

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. IPdb is the IPython debugger console.

How does Spyder work with MATLAB?

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

How can I run the whole code in Spyder 4?

In Spyder 4, when you select lines and press Ctrl + Enter it executes runcell (0, '/your/dir/file.py') which runs the whole code. How can I run just the lines which I have selected?


1 Answers

If you are using IPython as your interpreter, you can use the magic %pdb in IPython to automatically start pdb when an error is encountered.

Then you can "Run current cell" and break out into the debugger when you need to.

For example I have a simple script:

my_var = 4
raise ValueError

Now, in the IPython terminal I first run %pdb, and then I run my script.

In [4]: my_var = 4
   ...: raise ValueError
Traceback (most recent call last):

  File "<ipython-input-4-31dc119cb1f3>", line 2, in <module>
    raise ValueError

ValueError

> <ipython-input-4-31dc119cb1f3>(2)<module>()
      1 my_var = 4
----> 2 raise ValueError


ipdb> 

and I have the debugger available.

like image 109
KPLauritzen Avatar answered Nov 15 '22 04:11

KPLauritzen