title: How to protect the python interpreter against termination when a called module/program issues sys.exit()
body: <p>In an <em>interactively</em> running python session I kick off modules/programs I wrote using <a href="http://docs.python.org/2.7/library/argparse.html" rel="nofollow"><code>argparse</code></a> (because these modules are meant to be used as commands from the shell prompt as well).</p>
  
  <p>If I call the module with the wrong <code>args</code> parameters, <code>argparse</code> correctly spits out an error as expected, but unfortunately <code>arparse</code> then calls <code>sys.exit()</code> which in turn terminates the outer python session. </p>
  
  <p>That's not what I want. How can I protect the outer interactive python session from being terminated without changing the <code>sys.exit()</code> code in the inner module and without wrapping my module in code. - I'm looking for something like a switch I can set or something I can do to the interactive python session before I start it, so that <code>sys.exit()</code> does not terminate it. </p>
  
  <p><strong>Update from the comments:</strong></p>
  
  <p>The resaon why I ask this question is emacs's python-mode (<a href="https://github.com/fgallina/python.el/blob/master/python.el" rel="nofollow">python.el</a>): It effectively "pastes" the complete code of the module file in the python session. If I want to wrap this in a <code>try..except</code> I need to indent the whole module before I wrap it and hand over to the python-session buffer in emacs.</p>
  
  <p>Here a sample module I use (most of my command line utilities use a similar template):</p>
  
  <pre><code>#!/usr/bin/env python
  """\
  tool to do stuff
  """
  __author__ = """halloleo"""
  __version__ = """0.1"""
  
  import logging
  import sys
  import os
  
  import clitools # my own helpers
  
  #
  # Options
  #    
  def argParser(locArgs = None):
      parser = clitools.HelpLogArgParser(description=__doc__)
      parser.add_argument('files', metavar='FILE', nargs='+',
                          help='file to work on')
      parser.add_loglevel_group()
      return parser.parse_args(locArgs)
  
  def doStuff(file)
      # do stuff
      print file
  
  #
  # main
  #
  if __name__ == '__main__':
  
      args = argParser()     
      clitools.infoLoggerConfig(args.loglevel)
      logging.debug("args = %s" % args)          
  
      for f in args.files
          dostuff(f)
  
  # Local Variables:
  # leo-python-args-to-send: "--debug c:/tmp/testfile"
  # End:
  </code></pre>
  
  <p>BTW, I know now, this approach works. I have implemented it, but it is kind of scary to indent <em>the whole module code</em>... Here is what at the end I handover to the python session:</p>
  
  <pre><code>import sys; sys.argv = '''scraper.py --debug c:/tmp/testfile'''.split()
  try:
      #!/usr/bin/env python
      """\
      tool to do stuff
      """
  
      .
      .
      .
  
      # Local Variables:
      # leo-python-args-to-send: "--debug c:/tmp/testfile"
      # End:
  except SystemExit, e:
      print "Terminated  with exit code", e
  </code></pre>