title: Tell if Python is in interactive mode
body: <p>In a Python script, is there any way to tell if the interpreter is in interactive mode? This would be useful so that, for instance, when you run an interactive Python session and import a module, slightly different code is executed (for example, logging is turned off).</p>
  
  <p>I've looked at <a href="https://stackoverflow.com/questions/640389/tell-whether-python-is-in-i-mode">tell whether python is in -i mode</a> and tried the code there, however, that function only returns true if Python has been invoked with the -i flag and not when the command used to invoke interactive mode is <code>python</code> with no arguments.</p>
  
  <p>What I mean is something like this:</p>
  
  <pre><code>if __name__=="__main__":
      #do stuff
  elif __pythonIsInteractive__:
      #do other stuff
  else:
      exit()
  </code></pre>