6

Is there a way to find out if the python script is running in the IDLE interpreter or the terminal?

Works cross-platform if possible, or if needed a different way for each platform.

Work with Python 2 and Python 3 if possible, or if needed a different way for each version.

The only way I could think of is checking the processes running for IDLE but I don't know how to do that right.

If IDLE is open for another script and my script is running in the terminal, a process check would return true even if my script is not running in the IDLE.

My script needs to run differently depending on if it is running in IDLE or a terminal.

The Matt
  • 1,423
  • 1
  • 12
  • 22
freeforall tousez
  • 836
  • 10
  • 26
  • 1
    Related : [What code can I use to check if Python is running in IDLE?](http://stackoverflow.com/questions/3431498/what-code-can-i-use-to-check-if-python-is-running-in-idle) – Ashwini Chaudhary Jun 16 '13 at 13:26

2 Answers2

13

This seems to work on Python3/Linux

import sys

print("idlelib" in sys.modules)

If will return True if the script is run from Idle, False otherwise. Please test for other combination of Python/OS !

Sylvain Leroux
  • 50,096
  • 7
  • 103
  • 125
0

From https://stackoverflow.com/a/1077118/2544441:

sys.stdout.isatty()

is true if your script can write to a "true" terminal (at least since Python 2.7).

Mimmo
  • 153
  • 1
  • 6