HerrErik
HerrErik

Reputation: 241

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

Upvotes: 8

Views: 6890

Answers (4)

AdrienC
AdrienC

Reputation: 1

More details about the debug cell solution:

First you need to split your code into cells using the #%% separator, then you can either choose to run or debug a cell. In the example below you run the first cell then debug the second cell:

#%% SOME TIME CONSUMING CODE TO EXECUTE
...
...
#%% CODE TO DEBUG
...

Upvotes: 0

Joooeey
Joooeey

Reputation: 3886

You can use the pdb command j(ump) to get to the right line.

e.g.

debugfile()
jump 100

brings the debugger to line 100 of your script (or a few lines later if line 100 is not executable). From there you can continue as usual.

Upvotes: 0

Lucho
Lucho

Reputation: 239

Now you have Debug -> Debug Cell option. Or Alt+Shift+Return

Upvotes: 4

KPLauritzen
KPLauritzen

Reputation: 1869

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.

Upvotes: 3

Related Questions