Sylvia Onwukwe
Sylvia Onwukwe

Reputation: 153

AttributeError: 'datetime.time' object has no attribute 'time'

For test, I wrote a program that prints out time difference and it worked out perfectly. Here is the code in test.py

import time

start = time.time()

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

As I said, it works here; but when I copy the same code into my main code in main.py, it throws this error

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'

Why does it work on my terminal and test.py but throws an error in main.py and I'm not even importing datetime?

I have searched online for causes yet nothing and I need to use the code in my main.py. This is my 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)

Upvotes: 2

Views: 9672

Answers (1)

DirtyBit
DirtyBit

Reputation: 16772

Using 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)

OUTPUT:

652238.8331313
56259
1550652238.8331313
56260
1550652238.8331313
56261
1550652238.8331313
.
.
.

Upvotes: 2

Related Questions