7

I want to know if Jupyter Notebook has a command to stop subsequent cells from running once the command has been given.

I found this question and tried sys.exit() but some of the cells below the command are still being executed.

The structure of my script looks as follows:

# tons of cells above
if df["target"].mean() == 1:
    sys.exit("Condition met. Do not execute the cells below.")
# tons of cells below

Ideally, none of the cells represented by tons of cells below should be executed if df["target"].mean() is 1. Otherwise, I do want to the subsequent cells executed.

I suppose one solution would be to write the code of all the subsequent cells in an else statement inside the same cell where sys.exit() is written. However, I do not want to do this.

1 Answer 1

9

try this instead of sys.exit():

raise SystemExit("Stop right there!")
Sign up to request clarification or add additional context in comments.

Comments

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.