简体   繁体   中英

Debug and run mode in Jupyter

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM