Coding tips
UnboundLocalError: cannot access local variable where it is not associated with a value in Python: 10 Causes and Fixes (Tested Examples)
Python raises UnboundLocalError: cannot access local variable when a function reads a local name before it has a value. Here are 10 real causes, from += on a global to closures, late imports and except-as, with the exact tracebacks from my tests and the fixes that work.