Python's time module has a handy function called sleep(). Essentially, as the name implies, it pauses your Python program. time.sleep() is the equivalent to the Bash shell's sleep command. Almost all programming languages have this feature, and is used in many use-cases.

Python's time.sleep() Syntax

This is the syntax of the time.sleep() function:

time.sleep() Arguments

  • secs - The number of seconds the Python program should pause execution. This argument should be either an int or a float.

Using Python's time.sleep()

Here's a quick, simple example to the syntax:

More Advanced Syntax

Here's a more advanced example. It takes user input, and asks you how long you want to sleep(). It also proves how it works, by printing out the timestamp before, and after the time.sleep() call. Note that Python 2.x uses the raw_input() function to get user input, whereas Python 3.x uses the input() function.

The Accuracy of time.sleep()

The time.sleep() function uses the underlying operating system's sleep() function. Ultimately there are limitations of this function. For example on a standard Windows installation, the smallest interval you may sleep is 10 - 13 milliseconds. The Linux kernels tend to have a higher tick rate, where the intervals are generally closer to 1 millisecond. Note that in Linux, you can install the RT_PREEMPT patch set, which allows you to have a semi-realtime kernel. Using a real-time kernel will further increase the accuracy of the time.sleep() function. Generally however, unless you want to sleep for a very small period, you can generally ignore this information.

Here's a few links on the information above:

To Practice: Try this interactive course on the basics of Lists, Functions, Packages and NumPy in Python.

  • W. Patrick Jones

    I have a small script that, when I press a button, turns on an LED and a few other things, then timer.sleep(1200), then takes more action and turns off the LED. I’d like to add some code so that, if the user presses the button again, the timer ends and the next steps happen immediately. Is a For loop with a short timer and a test for button press the best approach?

    • If it’s a GUI I’d go with an asynchronous/button press event callback solution. The time.sleep() call would be in a thread, and the button press event callback would do a check to see if the sleep thread is active, and if so end it.

      • W. Patrick Jones

        Not a GUI. Physical button. I’m brand new to Python and still learning a lot. Just using straight Python on Raspian. Thanks for the quick response.

    • This may be of use if you’re using PySide or PyQt:

    • Jerry Webb

      Well, I don’t know about a physical button, but definitely this would work:

      And, Ver 3.4.4 of python

      b1press = 1

      light1 = None
      light2 = None
      light3 = None
      light4 = None

      if(b1press ==1):
      b1press =2
      print (“2”)

      if(b1press == 2):
      b1press = 3

      if (b1press == 3):
      b1press = 4 # Also known as off if there is 4 functions/ settings.

  • Vaidotas

    In python 2.x print is not a fuction.

  • Dimitri Mies

    I don’t know why, but for me when I run it it just says ” time.sleep(1)
    AttributeError: ‘int’ object has no attribute ‘sleep’ “

    • Elieroz

      Maybe you’ve got a variable called “time”. In that case, you should rename it to something else, avoiding names of other module, functions, classes…, already in use.

  • sawan kumar

    time.sleep(2) gives the below error for the second time and works fine for the first time, why this? can anybody help with this?
    SystemError: ..Objectslistobject.c:169: bad argument to internal function