2

Like in matlab, is there a possiblity in Jupyter to run a function in debug mode where execution is suspended at breakpoints and in run mode the function ignores the breakpoints? In a simple example like

from IPython.core.debugger import set_trace

def debug(y):
    x = 10
    x = x + y 
    set_trace()
    for i in range(10):
        x = x+i
    return x

debug(10)

is it possible that we call the function such that the set_trace is ignored and function is run normally?

The reason I want to have this is that in my function I have placed a lot of set traces and when I just want to run without the traces I need to comment all the set traces. Is there an easier way?

2
  • Have you considered Spyder? It has comparable interface as Matlab. Commented Nov 16, 2017 at 21:49
  • I am actually using notebook for my work. Commented Nov 29, 2017 at 19:59

1 Answer 1

5

I don't know of a way you can do this with Jupyter directly, but what you could do is monkey patch set_trace() out by like this (I'd recommend putting this in its own cell so you can re-run it for when you want to turn debugging back on):

from IPython.core.debugger import set_trace
debug_mode = False #switch this to True if you want debugging back on
if not debug_mode:
  def pass_func():
    pass
  set_trace = pass_func

What this does is rebind the name set_trace to be a function that simply does nothing, so every time set_trace() is called, it will just pass.

If you want the debugging back on, just switch the debug_mode flag to True and re-run the cell. This will then rebind the name set_trace to be the set_trace imported from IPython.core.debugger.

Sign up to request clarification or add additional context in comments.

1 Comment

Works for me! Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.