Python: NameError: free variable 're' referenced before assignment in enclosing scope
Most likely, you are assigning to re
(presumably inadvertently) at some point below line 561, but in the same function. This reproduces your error:
import re
def main():
term = re.compile("foo")
re = 0
main()
"free variable" in the traceback suggests that this is a local variable in an enclosing scope. something like this:
baz = 5
def foo():
def bar():
return baz + 1
if False:
baz = 4
return bar()
so that the baz
is referring to a local variable (the one who's value is 4), not the (presumably also existing) global. To fix it, force baz
to a global:
def foo():
def bar():
global baz
return baz + 1
so that it won't try to resolve the name to the nonlocal version of baz. Better yet, find where you're using re
in a way that looks like a local variable (generator expressions/list comprehensions are a good place to check) and name it something else.