Search code examples
python-3.xpep8flake8

flake8: F821 undefined name for a global variable


I've edited my script to remove all of the flake8 warnings except one:

/tmp/flake8.tempfile.py:535:26: F821 undefined name 'salt'

/tmp/flake8.tempfile.py:536:29: F821 undefined name 'salt'

/tmp/flake8.tempfile.py:554:22: F821 undefined name 'salt'

/tmp/flake8.tempfile.py:602:21: F821 undefined name 'salt'

/tmp/flake8.tempfile.py:602:32: F821 undefined name 'salt'

/tmp/flake8.tempfile.py:609:21: F821 undefined name 'salt'

/tmp/flake8.tempfile.py:609:32: F821 undefined name 'salt'

/tmp/flake8.tempfile.py:623:44: F821 undefined name 'salt'

/tmp/flake8.tempfile.py:646:39: F821 undefined name 'salt'

/tmp/flake8.tempfile.py:651:39: F821 undefined name 'salt'

/tmp/flake8.tempfile.py:654:39: F821 undefined name 'salt'

...

salt is a hashlib ojbect that is used by many functions. It is defined in the main section of the script (at the bottom) with the line:

salt = hashlib.sha512() 

Before being updated with data. All of my other globals work fine, so I don't know why this one makes flake8 flake out.


Solution

  • I wrapped the entire main section of the file in a:

    if __name__ == "__main__":
    

    and the problem magically went away. No explanation why, but at least this might help someone else with this same problem someday.