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?
From the idea behind this answer, adding for Win10 compatibility (Ripped from Python 2.7 script; modify as needed):
There is a number of switch like statements you could add to be more universal or handle different defaults.
I also had that question and, for me, the best solution is to set an environment variable in my IDE (PyCharm) and check if that variable exists to know if the script is being executed either via the command line or via the IDE.
To set an environment variable in PyCharm check: How to set environment variables in PyCharm?
Example code (environment variable: RUNNING_PYCHARM = True):
I hope it works for you.
I believe this CAN be done. At least, here is how I got it working in Python 2.7 under Ubuntu 14.04:
Note that -- in Ubuntu 14 with the Gnome desktop (aka Nautilus) -- you might need to do this:
To see how this works, you can fiddle with this (based on the answer by from @EduardoIvanec):
Although this isn't a very good solution, it does work (in windows at least).
You could create a batch file with the following contents:
If you want to be able to do this with a single file, you could try the following:
Batch code from: Pausing a batch file when double-clicked but not when run from a console window? Multi-line in batch from: DOS: Working with multi-line strings