简体   繁体   English

Python:为什么导入时间不适用于time()但适用于time.sleep()?

[英]Python: Why does import time not work for time() but works for time.sleep()?

If I use from time import time , the Python 2.7.3 does not recognize time.sleep(60) . 如果我使用from time import time ,Python 2.7.3无法识别time.sleep(60) But if I use import time , then Python does not recognize t=time() . 但是如果我使用import time ,那么Python就不会识别t=time() Why does this happen? 为什么会这样? Is there any way I can use time() and time.sleep(x) in the same program? 有什么方法可以在同一个程序中使用time()time.sleep(x)吗?

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 time.sleep(60) AttributeError: 'builtin_function_or_method' object has no attribute 'sleep' 回溯(最近一次调用最后一次):文件“myProg.py”,第9行,time.sleep(60)AttributeError:'builtin_function_or_method'对象没有属性'sleep'

You need to decide what you want the name time to refer to, the module or the function called time in the module. 您需要确定您希望名称time引用的内容,模块或模块中称为time的函数。 You can write: 你可以写:

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

or 要么

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

from time import time imports the method time() from the module time . from time import time导入方法time()从模块time import time , on the other hand, imports the module time . import time ,而另一方面,导入模块time import time and use time.time() instead of time() . import time和使用time.time()而不是time()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM