55

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

martineau
  • 119,623
  • 25
  • 170
  • 301

13 Answers13

58

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

palswim
  • 11,856
  • 6
  • 53
  • 77
regan
  • 741
  • 5
  • 4
  • 40
    [With Python 3+](http://stackoverflow.com/questions/954834/easy-how-to-use-raw-input-in-3-1), this has changed to just `input()`. – palswim Sep 02 '11 at 16:40
  • 17
    This isn't a solution. I don't know why the answerer chose it. The specifications were "Press any key and not just Enter". This solution works with enter, only. – Wolfpack'08 Nov 10 '11 at 12:05
  • 5
    @Wolfpack'08 While I agree that this answer clearly doesn't answer the question it should be pointed out that this is a high on the list for a google search for "python pause" so it is still useful to have this answer. – CrazyCasta Feb 16 '13 at 23:53
  • 3
    Don't you think it would make sense to answer the actual question, though? If even in a comment? – Wolfpack'08 Feb 18 '13 at 01:56
45

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

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

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
NGUYEN Q.Yen
  • 459
  • 4
  • 2
14

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.

David Grant
  • 13,929
  • 3
  • 57
  • 63
  • 1
    This doesn't help in the case specified by Bernd: starting a script from a taskbar shortcut, unless the shortcut is properly configures (which is not the default case). – Boris Gorelik Feb 23 '09 at 12:55
  • @bgbg: but the whole idea is that the shortcut is changed – David Grant Feb 23 '09 at 13:12
  • /K requires to terminate cmd via "exit" or closing the window using the mouse. That's a bit more than a single keypress :-) –  Feb 23 '09 at 13:19
  • @Bernd: True, but you can use Alt + Space + C, and it prevents you from having to rewrite all your scripts! – David Grant Feb 23 '09 at 13:46
11

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.

KingMak
  • 1,378
  • 2
  • 12
  • 26
7

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>
Boris Gorelik
  • 29,945
  • 39
  • 128
  • 170
  • 1
    +1: Many people can find the "Enter" or "Return" key without making large mistakes. Demanding "any key" instead of return seems a bit silly. – S.Lott Feb 23 '09 at 12:53
5

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.

martineau
  • 119,623
  • 25
  • 170
  • 301
dF.
  • 74,139
  • 30
  • 130
  • 136
3

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

2

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")
Matheus Lacerda
  • 5,983
  • 11
  • 29
  • 45
Vincenzo
  • 67
  • 1
  • 8
1

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)

SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
1

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)

0

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.

will-hart
  • 3,742
  • 2
  • 38
  • 48
merfman
  • 11
0

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

import WConio
WConio.getch()
Сыч
  • 930
  • 1
  • 8
  • 15
0
import pdb
pdb.debug()

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

lprsd
  • 84,407
  • 47
  • 135
  • 168