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?
Okay, the easiest way I found and made was to simply run the program in the command line, even if it was ran in the Python IDLE.
Add this to the bottom of your script and once ran from either IDLE or Command Prompt, it will create a file, re-run the program in the CMD, and exits the first instance. Hope that helps! :)
This is typically done manually/, I don't think there is an automatic way to do it that works for every case.
You should add a
--pause
argument to your script that does the prompt for a key at the end.When the script is invoked from a command line by hand, then the user can add
--pause
if desired, but by default there won't be any wait.When the script is launched from an icon, the arguments in the icon should include the
--pause
, so that there is a wait. Unfortunately you will need to either document the use of this option so that the user knows that it needs to be added when creating an icon, or else, provide an icon creation function in your script that works for your target OS.If you run python IDLE then "pythonw.exe" is being used to run coding while when you run the command line "python.exe" is used to run coding. The python folder path can vary so you have to revert the path to the python folder. m = '\\' and m = m[0] is to get m to be '\' because of escaping.
My solution was to create command line scripts using setuptools. Here are a the relevant parts of myScript.py:
And the relevant parts of setup.py:
Now if I open myScript.py with the Python interpreter (on Windows), the console window waits for the user to press enter if an error occurs. On the command line, if I run 'myScript', the program will never wait for user input before closing.
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
.py
file 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)If you're running it without a terminal, as when you click on "Run" in Nautilus, you can just check if it's attached to a tty:
But, as ThomasK points out, you seem to be referring to running it in a terminal that closes just after the program finishes. I think there's no way to do what you want without a workaround; the program is running in a regular shell and attached to a terminal. The decision of exiting immediately is done just after it finishes with information it doesn't have readily available (the parameters passed to the executing shell or terminal).
You could go about examining the parent process information and detecting differences between the two kinds of invocations, but it's probably not worth it in most cases. Have you considered adding a command line parameter to your script (think
--interactive
)?