[英]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.