title: Exit code when python script has unhandled exception
body: <p>I need a method to run a python script file, and if the script fails with an unhandled exception python should exit with a non-zero exit code. My first try was something like this:</p>
  
  <pre><code>import sys
  if __name__ == '__main__':
      try:
          import &lt;unknown script&gt;
      except:
          sys.exit(-1)
  </code></pre>
  
  <p>But it breaks a lot of scripts, due to the <code>__main__</code> guard often used. Any suggestions for how to do this properly?</p>