The Python Oracle

Find out if a python script is running in IDLE or terminal/command prompt

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Meadow

--

Chapters
00:00 Find Out If A Python Script Is Running In Idle Or Terminal/Command Prompt
00:45 Accepted Answer Score 13
01:03 Answer 2 Score 0
01:15 Thank you

--

Full question
https://stackoverflow.com/questions/1713...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #python3x #terminal #crossplatform #pythonidle

#avk47



ACCEPTED ANSWER

Score 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 !




ANSWER 2

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