Tell if Python is in interactive mode

PythonInteractivePython 2.5Python 2.x

Python Problem Overview


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).

I've looked at https://stackoverflow.com/questions/640389/tell-whether-python-is-in-i-mode 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 python with no arguments.

What I mean is something like this:

if __name__=="__main__":
    #do stuff
elif __pythonIsInteractive__:
    #do other stuff
else:
    exit()

Python Solutions


Solution 1 - Python

__main__.__file__ doesn't exist in the interactive interpreter:

import __main__ as main
print hasattr(main, '__file__')

This also goes for code run via python -c, but not python -m.

Solution 2 - Python

I compared all the methods I found and made a table of results. The best one seems to be this:

hasattr(sys, 'ps1')

enter image description here

If anyone has other scenarios that might differ, comment and I'll add it

Solution 3 - Python

sys.ps1 and sys.ps2 are only defined in interactive mode.

Solution 4 - Python

Use sys.flags:

if sys.flags.interactive:
    #interactive
else:
    #not interactive 

Solution 5 - Python

From TFM: If no interface option is given, -i is implied, sys.argv[0] is an empty string ("") and the current directory will be added to the start of sys.path.

If the user invoked the interpreter with python and no arguments, as you mentioned, you could test this with if sys.argv[0] == ''. This also returns true if started with python -i, but according to the docs, they're functionally the same.

Solution 6 - Python

The following works both with and without the -i switch:

#!/usr/bin/python
import sys
# Set the interpreter bool
try:
    if sys.ps1: interpreter = True
except AttributeError:
    interpreter = False
    if sys.flags.interactive: interpreter = True

# Use the interpreter bool
if interpreter: print 'We are in the Interpreter'
else: print 'We are running from the command line'

Solution 7 - Python

Here's something that would work. Put the following code snippet in a file, and assign the path to that file to the PYTHONSTARTUP environment variable.

__pythonIsInteractive__ = None

And then you can use

if __name__=="__main__":
    #do stuff
elif '__pythonIsInteractive__' in globals():
    #do other stuff
else:
    exit()

http://docs.python.org/tutorial/interpreter.html#the-interactive-startup-file

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChinmay KanchiView Question on Stackoverflow
Solution 1 - PythonIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 2 - PythonpyjamasView Answer on Stackoverflow
Solution 3 - PythonChristopheDView Answer on Stackoverflow
Solution 4 - PythonPychView Answer on Stackoverflow
Solution 5 - PythonwersimmonView Answer on Stackoverflow
Solution 6 - PythonjdinesView Answer on Stackoverflow
Solution 7 - PythonJABView Answer on Stackoverflow