pythonpython-import

Why does "import time" not work for time() but works for time.sleep()?


If I use from time import time, Python does not recognize time.sleep(60). But if I use import time, then Python does not recognize t=time(). Why does this happen? Is there any way I can use time() and time.sleep(x) in the same program?

from time import time
#import time

intervalInMinute = 1
t = time()
while 1:
    time.sleep(60)

The kind of error I get is:

Traceback (most recent call last):
  File "myProg.py", line 9, in <module>
    time.sleep(60)
AttributeError: 'builtin_function_or_method' object has no attribute 'sleep'

Solution

  • You need to decide what you want the name time to refer to, the module or the function called time in the module. You can write:

    >>> from time import time, sleep
    >>> time()
    1347806075.148084
    >>> sleep(3)
    >>>
    

    or

    >>> import time 
    >>> time.time()
    1347806085.739065
    >>> time.sleep(2)
    >>>