Press Enter to Continue

Press Enter To Continue

When you run a Python script, you might find that it runs quickly and closes. This might be because it successfully completed, or it might be because it errored out.

The problem is that the Python window closes too quickly for you to tell. In a case like this, you want the script to pause before quitting. There are a few ways this could be done

Sleep for a Time

A very simple option is just to wai a few seconds, and then continue on:

import time

print ('Waiting 5 seconds before continuing')

time.sleep(5)

Wait for the Enter Key

If you want the script to wait until you’re ready, you can ask for user input. The simplest option here is to use input, which takes characters until the enter key is pressed:

input("Press enter to continue")

Press Any Key to Continue

Press any key is more difficult than it sounds. This doesn’t work across platforms as well as you would think. That is, solutions that work on Windows don’t work on Linux, and the other way around.

On Windows, this works:

import os
os.system ('pause')

An alternative is to use a third-party module, such as readchar.