8

I'm receiving a strange error when running my program? I'm not sure why it wont let me sleep.

Traceback (most recent call last):
Not an add minute at all.
  File "C:/Users/admin/PycharmProjects/test/odd.py", line 15, in <module>
    time.sleep(0.05)
AttributeError: 'builtin_function_or_method' object has no attribute 'sleep'

Code:

from datetime import datetime
from time import time
from random import random

odds = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59]

right_this_minute = datetime.today().minute

for i in range(0, 11):
    if right_this_minute in odds:
        print("This minute seems a little odd.")
    else:
        print("Not an add minute at all.")

    time.sleep(random.randint(1, 60))
2
  • 2
    from time import time is making time in your code the time function in the time module, not the module itself. Do import time instead. Commented Jul 22, 2018 at 14:27
  • Using Spyder, with import time when calling time.time() I received the error builtin_function_or_method' object has no attribute 'time' which was silly. However I previously tested from time import time and for some reason Spyder or Python didn't forget this import. I had to reset the kernel to get rid of the error. Commented Jun 8, 2021 at 11:21

5 Answers 5

17

Change from time import time to from time import sleep

Then you can use sleep directly instead of time.sleep

Sign up to request clarification or add additional context in comments.

Comments

4

So I had the same awkward problem. I say awkward because a couple of days ago when I solved an exercise and used the following import statement: "from time import sleep", my code worked just fine when using time.sleep(n). After a couple of days of adding code to the same .py file, my previously working function definition that used time.sleep(n) did not work anymore.

AttributeError: 'builtin_function_or_method' object has no attribute 'sleep'

This is the error it threw at me. I did not change anything to the import statement used when I have initially imported sleep from the time module.

Resolve: I ended up having to delete time from time.sleep(n) and used only sleep(n) as Agile_Eagle suggested. Thx for that! But why would it work initially and then crash with that error...?

from time import sleep
from time import *             #meaning from time import EVERYTHING
import time`

All of the above import methods work just fine for importing the time module/or for importing just "sleep" from the time module, but now I can only use sleep(n) instead of time.sleep(n) (as I did initially when I solved the exercise).

Comments

2

snakecharmeb is right, and also, you need to import random rather than from random import random.

from datetime import datetime
import time
import random


odds = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59]

right_this_minute = datetime.today().minute

for i in range(0, 11):
    if right_this_minute in odds:
        print("This minute seems a little odd.")
    else:
        print("Not an add minute at all.")

    time.sleep(random.randint(1, 60))

1 Comment

Thank you, that's fixed it.
2

you are importing wrong way "time" package...

import time
time.sleep(5) #sleeps for 5 seconds

That is all.

Comments

1

You should write like this:

import time

you can see the full doc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.