How to pause a script when it ends on Windows?

PythonWindowsCmdCommand Line

Python Problem Overview


I am running command-line Python scripts from the Windows taskbar by having a shortcut pointing to the Python interpreter with the actual script as a parameter.

After the script has been processed, the interpreter terminates and the output window is closed which makes it impossible to read script output.

What is the most straightforward way to keep the interpreter window open until any key is pressed?

In batch files, one can end the script with pause. The closest thing to this I found in python is raw_input() which is sub-optimal because it requires pressing the return key (instead of any key).

Python Solutions


Solution 1 - Python

One way is to leave a raw_input() at the end so the script waits for you to press Enter before it terminates.

Solution 2 - Python

Try os.system("pause") — I used it and it worked for me.

Make sure to include import os at the top of your script.

Solution 3 - Python

There's no need to wait for input before closing, just change your command like so:

cmd /K python <script>

The /K switch will execute the command that follows, but leave the command interpreter window open, in contrast to /C, which executes and then closes.

Solution 4 - Python

The best option: os.system('pause') <-- this will actually display a message saying 'press any key to continue' whereas adding just raw_input('') will print no message, just the cursor will be available.

not related to answer:

os.system("some cmd command") is a really great command as the command can execute any batch file/cmd commands.

Solution 5 - Python

> One way is to leave a raw_input() at the end so the script waits for you to press enter before it terminates.

The advantage of using raw_input() instead of msvcrt.* stuff is that the former is a part of standard Python (i.e. absolutely cross-platform). This also means that the script window will be alive after double-clicking on the script file icon, without the need to do

cmd /K python <script>

Solution 6 - Python

On Windows you can use the msvcrt module.

> * msvcrt.kbhit()
> Return True if a keypress is waiting to be read. > > * msvcrt.getch()
> Read a keypress and return the resulting character as a byte string. Nothing is echoed to the console. This call will block if a keypress is not already available, but will not wait for Enter to be pressed. If the pressed key was a special function key, this will return '\000' or '\xe0'; the next call will return the keycode. The Control-C keypress cannot be read with this function.

If you want it to also work on Unix-like systems you can try this solution using the termios and fcntl modules.

Solution 7 - Python

As to the "problem" of what key to press to close it, I (and thousands of others, I'm sure) simply use input("Press Enter to close").

Solution 8 - Python

There's a simple way to do this, you can use keyboard module's wait function. For example, you can do:

import keyboard
print("things before the pause")
keyboard.wait("esc") # esc is just an example, you can obviously put every key you want
print("things after the pause")

Solution 9 - Python

Getting python to read a single character from the terminal in an unbuffered manner is a little bit tricky, but here's a recipe that'll do it:

Recipe 134892: getch()-like unbuffered character reading from stdin on both Windows and Unix (Python)

Solution 10 - Python

On Windows 10 insert at beggining this:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

Strange, but it works for me! (Together with input() at the end, of course)

Solution 11 - Python

An external WConio module can help here: http://newcenturycomputers.net/projects/wconio.html

import WConio
WConio.getch()

Solution 12 - Python

import pdb
pdb.debug()

This is used to debug the script. Should be useful to break also.

Solution 13 - Python

If you type

input("")

It will wait for them to press any button then it will continue. Also you can put text between the quotes.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBerndView Question on Stackoverflow
Solution 1 - PythonreganView Answer on Stackoverflow
Solution 2 - PythonNGUYEN Q.YenView Answer on Stackoverflow
Solution 3 - PythonDavid GrantView Answer on Stackoverflow
Solution 4 - PythonKingMakView Answer on Stackoverflow
Solution 5 - PythonBoris GorelikView Answer on Stackoverflow
Solution 6 - PythondF.View Answer on Stackoverflow
Solution 7 - PythonThinking MonkeyView Answer on Stackoverflow
Solution 8 - PythonVincenzoView Answer on Stackoverflow
Solution 9 - PythonSingleNegationEliminationView Answer on Stackoverflow
Solution 10 - PythonRadim AkudoView Answer on Stackoverflow
Solution 11 - PythonСычView Answer on Stackoverflow
Solution 12 - PythonlprsdView Answer on Stackoverflow
Solution 13 - PythonmerfmanView Answer on Stackoverflow