228

I've been using the input function as a way to pause my scripts:

print("something")
wait = input("Press Enter to continue.")
print("something")

Is there a formal way to do this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RandomPhobia
  • 4,698
  • 7
  • 25
  • 22

17 Answers17

288

It seems fine to me (or raw_input() in Python 2.X). Alternatively, you could use time.sleep() if you want to pause for a certain number of seconds.

import time
print("something")
time.sleep(5.5)    # Pause 5.5 seconds
print("something")
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mhawke
  • 84,695
  • 9
  • 117
  • 138
  • 1
    I know about the sleep function in the time module but what if I have a long block of text I want the user to read? – RandomPhobia Jul 19 '12 at 00:34
  • 9
    Simply use `print` to display the long block of text and then `input()` or `raw_input('Press to continue')` as appropriate for your version of Python. – mhawke Jul 19 '12 at 00:39
  • 10
    For a long block of text, it is best to use `input()` (or `raw_input()` on Python 2.x) to prompt the user, rather than a time delay. Fast readers won't want to wait for a delay, slow readers might want more time on the delay, someone might be interrupted while reading it and want a lot more time, etc. Also, if someone uses the program a lot, he/she may become used to how it works and not need to even read the long text. It's just friendlier to let the user control how long the block of text is displayed for reading. – steveha Jul 19 '12 at 02:00
43

For Windows only, use:

import os
os.system("pause")
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chan Tzish
  • 1,108
  • 9
  • 7
  • 44
    This is Windows specific, whereas all of the existing answers are platform independent. – lvc Oct 02 '15 at 14:26
  • 3
    why would python implement a function that is OS specific? It makes no sense. – Denis Oct 26 '20 at 14:22
  • 2
    os.system() calls a system function called "pause" – a ubuntu for example doesn't know what "pause" is, so it will return with: _sh: 1: pause: not found_ – ato Nov 10 '20 at 11:16
  • 4
    I know this is probably not what the question wanted, but it helped me a lot. – Stream Summer Feb 23 '21 at 03:05
  • 1
    If you are calling the python script from a Windows Batch File, you can add `pause` to the end of the batch file instead of putting a command at the end of the python file, and you will get the same effect. This way the python file is OS independent, while the batch file could only ever be used in Windows anyway. – Leland Hepworth Feb 23 '21 at 22:54
  • Is this equivalent to `signal.pause()`? (Which I believe works on Linux) – Xbox One Jul 24 '22 at 04:54
22

So, I found this to work very well in my coding endeavors. I simply created a function at the very beginning of my program,

def pause():
    programPause = raw_input("Press the <ENTER> key to continue...")

and now I can use the pause() function whenever I need to just as if I was writing a batch file. For example, in a program such as this:

import os
import system

def pause():
    programPause = raw_input("Press the <ENTER> key to continue...")

print("Think about what you ate for dinner last night...")
pause()

Now obviously this program has no objective and is just for example purposes, but you can understand precisely what I mean.

NOTE: For Python 3, you will need to use input as opposed to raw_input

Cether
  • 631
  • 1
  • 6
  • 8
18

I assume you want to pause without input.

Use:

time.sleep(seconds)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
8bitwide
  • 2,071
  • 1
  • 17
  • 24
10

I have had a similar question and I was using signal:

import signal

def signal_handler(signal_number, frame):
    print "Proceed ..."

signal.signal(signal.SIGINT, signal_handler)
signal.pause()

So you register a handler for the signal SIGINT and pause waiting for any signal. Now from outside your program (e.g. in bash), you can run kill -2 <python_pid>, which will send signal 2 (i.e. SIGINT) to your python program. Your program will call your registered handler and proceed running.

olean
  • 117
  • 1
  • 2
8
print ("This is how you pause")

input()

wilson
  • 411
  • 2
  • 16
1byanymeans
  • 105
  • 1
  • 3
  • This seems to result in an error: `NameError: name 'Print' is not defined` – Peter Mortensen Nov 08 '20 at 04:29
  • @PeterMortensen That is due to the blank space between "Print" and "("This is how you pause")". – isak Dec 21 '20 at 14:48
  • 1
    Python is case sensitive, so lowercase `print` is required to call the function. You can even keep the space between the function name and the opening parenthesis without getting an error. – Leland Hepworth Feb 23 '21 at 23:00
8

I use the following for Python 2 and Python 3 to pause code execution until user presses Enter

import six

if six.PY2:
    raw_input("Press the <Enter> key to continue...")
else:
    input("Press the <Enter> key to continue...")
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adewole Adesola
  • 179
  • 2
  • 3
7

As pointed out by mhawke and steveha's comments, the best answer to this exact question would be:

Python 3.x:

input('Press <ENTER> to continue')

Python 2.x:

raw_input('Press <ENTER> to continue')

For a long block of text, it is best to use input('Press <ENTER> to continue') (or raw_input('Press <ENTER> to continue') on Python 2.x) to prompt the user, rather than a time delay. Fast readers won't want to wait for a delay, slow readers might want more time on the delay, someone might be interrupted while reading it and want a lot more time, etc. Also, if someone uses the program a lot, he/she may become used to how it works and not need to even read the long text. It's just friendlier to let the user control how long the block of text is displayed for reading.

Anecdote: There was a time where programs used "press [ANY] key to continue". This failed because people were complaining they could not find the key ANY on their keyboard :)

ntg
  • 12,950
  • 7
  • 74
  • 95
5

Very simple:

raw_input("Press Enter to continue ...")
print("Doing something...")
Community
  • 1
  • 1
Bu Saeed
  • 1,173
  • 1
  • 16
  • 27
  • 1
    I've used this method a bit, however I also like using it as a pause() function and I don't think there's a need for exit(). def pause(): return raw_input("Press Enter to continue ...") – jacktrader Feb 12 '18 at 18:51
  • `NameError: name 'raw_input' is not defined` – bers Apr 03 '20 at 10:03
3

By this method, you can resume your program just by pressing any specified key you've specified that:

import keyboard
while True:
    key = keyboard.read_key()
    if key == 'space':  # You can put any key you like instead of 'space'
        break

The same method, but in another way:

import keyboard
while True:
    if keyboard.is_pressed('space'):  # The same. you can put any key you like instead of 'space'
        break

Note: you can install the keyboard module simply by writing this in you shell or cmd:

pip install keyboard
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
3

cross-platform way; works everywhere

import os, sys

if sys.platform == 'win32':
    os.system('pause')
else:
    input('Press any key to continue...')
Mujeeb Ishaque
  • 2,259
  • 24
  • 16
  • 1
    If the platform is windows, use the pause command of windows batch scripting. If it's not windows, use the python input() function to pause for input. After the input, the program will close. – Mujeeb Ishaque Nov 08 '20 at 17:53
2

I work with non-programmers who like a simple solution:

import code
code.interact(banner='Paused. Press ^D (Ctrl+D) to continue.', local=globals()) 

This produces an interpreter that acts almost exactly like the real interpreter, including the current context, with only the output:

Paused. Press ^D (Ctrl+D) to continue.
>>>

The Python Debugger is also a good way to pause.

import pdb
pdb.set_trace() # Python 2

or

breakpoint() # Python 3
Walter Nissen
  • 16,451
  • 4
  • 26
  • 27
1

I think I like this solution:

import getpass
getpass.getpass("Press Enter to Continue")

It hides whatever the user types in, which helps clarify that input is not used here.

But be mindful on the OS X platform. It displays a key which may be confusing.

It shows a key, like I said


Probably the best solution would be to do something similar to the getpass module yourself, without making a read -s call. Maybe making the foreground color match the background?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Samie Bencherif
  • 1,285
  • 12
  • 27
1

user12532854 suggested using keyboard.readkey() but the it requires specific key (I tried to run it with no input args but it ended up immediately returning 'enter' instead of waiting for the keystroke).

By phrasing the question in a different way (looking for getchar() equivalent in python), I discovered readchar.readkey() does the trick after exploring readchar package prompted by this answer.

import readchar
readchar.readkey()
Hoi Wong
  • 63
  • 5
0

I think that the best way to stop the execution is the time.sleep() function.

If you need to suspend the execution only in certain cases you can simply implement an if statement like this:

if somethinghappen:
    time.sleep(seconds)

You can leave the else branch empty.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mbiella
  • 51
  • 6
0

For cross Python 2/3 compatibility, you can use input via the six library:

import six
six.moves.input( 'Press the <ENTER> key to continue...' )
BuvinJ
  • 10,221
  • 5
  • 83
  • 96
0

On POSIX:

import signal

try:
    signal.pause()
except KeyboardInterrupt:
    pass
finally:
    print("Waiting time is over!")
heiner
  • 598
  • 6
  • 11