Background
I would like my Python script to pause before exiting using something similar to:
raw_input("Press enter to close.")
but only if it is NOT run via command line. Command line programs shouldn’t behave this way.
Question
Is there a way to determine if my Python script was invoked from the command line:
$ python myscript.py
verses double-clicking myscript.py to open it with the default interpreter in the OS?
I don’t think there’s any reliable way to detect this (especially in a cross-platform manner). For example on OS X, when you double-click a
.pyfile and it tuns with “Python Launcher”, it runs in a terminal, identically to if you execute it manually.Although it may have other issues, you could package the script up with something like py2exe or Platypus, then you can have the double-clickable icon run a specific bit of code to differentiate (
import mycode; mycode.main(gui = True)for example)