title: How to stop python program once condition is met (in jupyter notebook)
body: <p>I want to know if Jupyter Notebook has a command to stop subsequent cells from running once the command has been given.</p>
  
  <p>I found <a href="https://stackoverflow.com/questions/179369/how-do-i-abort-the-execution-of-a-python-script">this question</a> and tried <code>sys.exit()</code> but some of the cells below the command are still being executed.</p>
  
  <p>The structure of my script looks as follows:</p>
  
  <pre class="lang-py prettyprint-override"><code># tons of cells above
  </code></pre>
  
  <pre class="lang-py prettyprint-override"><code>if df["target"].mean() == 1:
      sys.exit("Condition met. Do not execute the cells below.")
  </code></pre>
  
  <pre class="lang-py prettyprint-override"><code># tons of cells below
  </code></pre>
  
  <p>Ideally, none of the cells represented by <code>tons of cells below</code> should be executed if <code>df["target"].mean()</code> is <code>1</code>. Otherwise, I do want to the subsequent cells executed.</p>
  
  <p>I suppose one solution would be to write the code of all the subsequent cells in an <code>else</code> statement inside the same cell where <code>sys.exit()</code> is written. However, I do not want to do this.</p>