title: How do I drop back to the python shell after exiting a script?
body: <p>My python script has a couple of sys.exit() calls to terminate under certain conditions. To test it, I open up a python shell and run <code>exec(open('script.py').read())</code>. Whenever I hit a <code>sys.exit()</code> call, the script terminates, along with the python shell, which is irritating.</p>
  
  <p>So I try to get creative and attempt to determine whether my script is run from the commandline or from the shell by examining the stack. Source : <a href="https://stackoverflow.com/questions/23883394/detect-if-python-script-is-run-from-an-ipython-shell-or-run-from-the-command-li">Detect if python script is run from an ipython shell, or run from the command line</a></p>
  
  <pre><code>def exit_now(exit_status):
      os.system('pause')
  
      if PythonShell:  # how do I return to the python shell ?
          ???
      else:  # exit if we are running from the commandline
          sys.exit(exit_status)
  
  ...
  
  # check if this was run from inside a python shell
  frames = len(inspect.stack())
  if frames &gt; 1:
      PythonShell = True
  else:
      PythonShell = False
  
  ...
  
  if condition1:
      exit_now(1)
  
  if condition2:
      exit_now(2)
  
  ...
  
  exit_now(0)
  </code></pre>
  
  <p>How do I drop back to the python shell after terminating a script ?</p>