Sometimes, we want to fix secret key not set in Python Flask session, using the Flask-Session extension.
In this article, we’ll look at how to fix secret key not set in Python Flask session, using the Flask-Session extension.
How to fix secret key not set in Python Flask session, using the Flask-Session extension?
To fix secret key not set in Python Flask session, using the Flask-Session extension, we set it outside of the if __name__ == '__main__' block.
For instance, we write
from flask import Flask, session
app = Flask(__name__)
app.secret_key = "super secret key"
# ...
if __name__ == "__main__":
app.debug = True
app.run()
to set the app.secret_key property before we call app.run in the if __name__ == "__main__" block.
This is because flask run skips the if __name__ == "__main__" block.
We can run python app.py to start the app if we want to run the if __name__ == "__main__" block.
Conclusion
To fix secret key not set in Python Flask session, using the Flask-Session extension, we set it outside of the if __name__ == '__main__' block.