[Solved] Got UnboundLocalError: Local Variable referenced before assignment, but it wasn’t


Your findall('CRC-START(.*?)CRC-END', PIDFile.read(), re.S) on line 202 didn’t find anything, PID didn’t get declared, boom, UnboundLocalError.

This happens because python interpreter makes a preliminary pass on the code, marking encountered variables as local, but it does not (and cannot) check if code that declares them will actually be executed.
Minimal reproducible example of that effect would be this:

>>> def foo():
    if 0:
        a = 1
    print a


>>> foo()

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    foo()
  File "<pyshell#4>", line 4, in foo
    print a
UnboundLocalError: local variable 'a' referenced before assignment

1

solved Got UnboundLocalError: Local Variable referenced before assignment, but it wasn’t