AttributeError:“ datetime.time”对象没有属性“ time”

时间:2019-02-20 08:34:46

标签: python datetime time

为了进行测试,我编写了一个程序,该程序可以打印出时差,并且效果很好。这是test.py

中的代码
import time

start = time.time()

while True:
    if time.time() - start >= 59:
        print(time.time() - start)
        start = time.time()

正如我所说,它在这里有效;但是当我将相同的代码复制到main.py的主代码中时,会引发此错误

Traceback (most recent call last):
 File "main.py", line 81, in <module>
  if time.time() - start >= 59:
AttributeError: 'datetime.time' object has no attribute 'time'

为什么它可以在我的终端机和test.py上运行,但是在main.py中抛出错误,并且我什至没有导入datetime?

我已经在线搜索了原因,但没有找到任何东西,我需要使用main.py中的代码。这是我的main.py:

import win32com.client #pip install pywin32 if not installed
import math
import time
import PySimpleGUI as sg
import pygame as pg
from pywintypes import com_error

x = math.inf
counter = 0
start=time.time()

 while True:
       print(start)
        if time.time() - start >= 59:
            counter = 0
            start = time.time()
        counter +=1
        print(counter)

1 个答案:

答案 0 :(得分:1)

使用import time as t

if __name__ == '__main__':
    import math
    import time as t
    x = math.inf
    counter = 0
    start=t.time()

    while True:
       print(start)
       if t.time() - start >= 59:
            counter = 0
            start = t.time()
       counter +=1
       print(counter)

输出:

652238.8331313
56259
1550652238.8331313
56260
1550652238.8331313
56261
1550652238.8331313
.
.
.