0

I'm trying to run a code, but Python is telling me that local variable 'time' referenced before assignment. This is the code:

from enlace import *
from time import time
import numpy as np

imageW = "./img/transmission_receive.png"

def process_time(time, unit):
    units={"ms":1e3, "μs": 1e6, "ns":1e9}
    return units.get(unit)*time, unit

def main():
    try:
        door="COM6"
        com2 = enlace(door)

        com2.enable()
    
        start_time = time()

        img_size_b, nRx = com2.getData(4)
        img_size=int.from_bytes(img_size_b, 'big')
        rxBuffer, nRx = com2.getData(img_size)
    
        time_to_receive=time() - start_time
        time, unit=process_time(time_to_receive, 'ms')
    
        answer=nRx.to_bytes(4, 'big')
        com2.sendData(answer)
    
        with open(imageW, 'wb') as f:
            f.write(rxBuffer)
       
        print("-------------------------\nMessage sent.\n-------------------------")
        print(f"Program took {time} {unit} to receive.")
        print(f"Message received at {size/time_to_receive} bytes/s.")
    
    except Exception as e:
        print(e)
        com2.disable()
    

if __name__ == "__main__":
    main()

The particular line giving me trouble right now is the start_time = time() one, to which Python says local variable 'time' referenced before assignment, which I don't understand, since I'm creating the variable start_time which uses the time() function from the time library.

Traceback (most recent call last):
  File "d:\Insper\CamFis\Projeto 2\server.py", line 46, in <module>
    main()
  File "d:\Insper\CamFis\Projeto 2\server.py", line 19, in main
    start_time = time()
UnboundLocalError: local variable 'time' referenced before assignment
  • 4
    Looks like you are using it as a variable `time, unit=process_time(time_to_receive, 'ms')` – Mark Mar 06 '21 at 16:18
  • Two things about that though, it's giving me an error in a line before that, and I'm assigning it a value from my function process_time(), am I not? (Not a sarcastic question, sorry if it sounds like that) – Gabriel Tkacz Mar 06 '21 at 16:20
  • Nowhere in your code do you have a function called time(), but you are calling the function time(), when in reality as Mark said it's being used as a variable – JD2775 Mar 06 '21 at 16:22
  • 1
    Please update your question with the full error traceback. – quamrana Mar 06 '21 at 16:23
  • @JD2775: The function is in the imports: `from time import time` – Bill Lynch Mar 06 '21 at 16:25
  • @BillLynch ah, got it – JD2775 Mar 06 '21 at 16:26
  • Seems like @MarkM was right, but I still don't understand why it was giving me an error on the "start_time = time()" line and not on the "time, unit=process_time(time_to_receive, 'ms')" one then. Thanks for all the help anyway! – Gabriel Tkacz Mar 06 '21 at 16:34
  • @quamrana The issue in this question is that op don't understand that functions are first-class objects in Python, more than the actual error. – user202729 Mar 06 '21 at 17:01
  • I just ran into the exact same problem. The second answer below, the non-accepted one, is the answer that got me mentally over the hump as to why this was actually an issue. The "this question already has an answer", above, doesn't actually answer this specific issue because this particular question doesn't have to do with global versus local variables. – CDahn Jun 09 '23 at 14:44

2 Answers2

2

I think your problem is that you are using the time.time() function and a variable called time. In python functions are first-class-objects. Means time.time() is treated in a similar way as your variable time. time.time() is then overwritten by your variable time. Try this in a python console:

>>> from time import time
>>> time
>>> time()
>>> time = time()
>>> time
>>> time()

Problem should be fixed by renaming the varibale time or import time.time() as from time import time as gettime() or something similar.

prog2de
  • 309
  • 2
  • 11
1

It could be to do with how python compiles your code to bytecode. It makes a decision on how it should deal with symbols in each scope. It looks like it has decided to deal with time as a local variable because it saw an assignment later on in main(). Therefore start_time = time() is referring to time as a local which has not been assigned to yet, rather than as a global which gets overwritten later.

Also see this question.

quamrana
  • 37,849
  • 12
  • 53
  • 71