title: Generate non-zero exit code when IPython encounters an exception running a script
body: <p>I would like to run a python script with IPython and be able to tell whether the script was successful (ran all the way through) or unsuccessful (an exception was raised).</p>
  
  <p>Normally one can tell whether a command is successful by inspecting its return value, where the convention is 0 for success and some other integer for an error (the value indicating which error). This is the behaviour when running scripts with <code>python script.py</code>, but when using <code>ipython script.py</code>, IPython automatically captures the error and (unhelpfully) returns exit code 0.</p>
  
  <p>How can I disable this behaviour when running the script with IPython?</p>
  
  <p>Incidentally, I need to run the script in IPython instead of Python because the script has been generated from an IPython notebook (.ipynb) and contains some IPython magic commands.</p>
  
  <p>Here is a minimal working example.</p>
  
  <p><strong>fail.py</strong></p>
  
  <pre><code>#! /usr/bin/env python
  mytext = 'Hello World!'
  if __name__=="__main__":
      print(missing_variable)
  </code></pre>
  
  <p>When running the script with IPython, the exception is printed to stdout and the return value is 1, correctly denoting failure of the script.</p>
  
  <pre><code>~$ python fail.py
  Traceback (most recent call last):
    File "fail.py", line 4, in &lt;module&gt;
      print(missing_variable)
  NameError: name 'missing_variable' is not defined
  ~$ echo $?
  1
  </code></pre>
  
  <p>When running the script with IPython, the exception is caught and the traceback is printed to stdout, then IPython exits and returns 0 (which is not desirable, since the script did not succeed).</p>
  
  <pre><code>~$ ipython fail.py
  ---------------------------------------------------------------------------
  NameError                                 Traceback (most recent call last)
  ~/fail.py in &lt;module&gt;()
        2 mytext = 'Hello World!'
        3 if __name__=="__main__":
  ----&gt; 4     print(missing_variable)
  
  NameError: name 'missing_variable' is not defined
  ~$ echo $?
  0
  </code></pre>