216

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?

1
  • 3
    Work in a debugger and use breakpoints?
    – g.d.d.c
    Jul 19, 2012 at 0:29

16 Answers 16

279

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")
3
  • 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? Jul 19, 2012 at 0:34
  • 8
    Simply use print to display the long block of text and then input() or raw_input('Press <ENTER> to continue') as appropriate for your version of Python.
    – mhawke
    Jul 19, 2012 at 0:39
  • 9
    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, 2012 at 2:00
42

For Windows only, use:

import os
os.system("pause")
6
  • 43
    This is Windows specific, whereas all of the existing answers are platform independent.
    – lvc
    Oct 2, 2015 at 14:26
  • 3
    why would python implement a function that is OS specific? It makes no sense.
    – Denis
    Oct 26, 2020 at 14:22
  • 1
    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, 2020 at 11:16
  • 4
    I know this is probably not what the question wanted, but it helped me a lot. Feb 23, 2021 at 3: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. Feb 23, 2021 at 22:54
21

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

1
  • 1
    How is this different from what is stated in the question? Nov 8, 2020 at 4:26
18

I assume you want to pause without input.

Use:

time.sleep(seconds)

2
  • Yes, the search engines leads to this question. Apr 10, 2022 at 16:40
  • 3
    This requires import time. Apr 10, 2022 at 16:43
9

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.

2
  • 4
    Note that this is not portable across platforms, eg. on Windows there is no signal.pause.
    – kyrill
    Apr 28, 2019 at 15:40
  • 1
    What platform did you try it on? Linux? Nov 8, 2020 at 4:25
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...")
8
print ("This is how you pause")

input()

3
  • This seems to result in an error: NameError: name 'Print' is not defined Nov 8, 2020 at 4:29
  • @PeterMortensen That is due to the blank space between "Print" and "("This is how you pause")".
    – isak
    Dec 21, 2020 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. Feb 23, 2021 at 23:00
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 :)

5

Very simple:

raw_input("Press Enter to continue ...")
print("Doing something...")
2
  • 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, 2018 at 18:51
  • NameError: name 'raw_input' is not defined
    – bers
    Apr 3, 2020 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
0
3

cross-platform way; works everywhere

import os, sys

if sys.platform == 'win32':
    os.system('pause')
else:
    input('Press any key to continue...')
1
  • 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. Nov 8, 2020 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
0
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?

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()
0
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...' )
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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.