Below is the code from my json.launch file in my flask application. I have various environment variables defined in the "env": {}
portion. However, when I run my flask application from the run.py script, it doesn't seem to recognize the variables. Although the "FLASK_DEBUG" is set to "1", the application still runs on Debug mode: off.
Does anyone know why this may be ?
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "run.py",
"FLASK_ENV": "development",
"FLASK_DEBUG": "1",
"EMAIL_USER": "[email protected]",
"EMAIL_PASS": "password",
},
"args": [
"run",
//"--no-debugger"
],
"jinja": true,
}
],
}
If I set:
if __name__ == '__main__':
app.run(debug=True)
then the app does run in Debug mode. However, I still cannot get the other environment variables:
>>> import os
>>> print(os.environ.get('FLASK_DEBUG'))
None
CodePudding user response:
A good way to manage environment variables in flask is by using a .flaskenv
file. You will need python-dotenv
to use this, and the file needs to be in the root directory of your project.
In your .flaskenv
file, simply type in your variables like so:
FLASK_APP=run.py
FLASK_ENV=development
FLASK_DEBUG=1
Then you can just run flask using flask run
and the envs will be loaded.
Doing it this way will mean that you keep your env settings across IDEs if you change or load the project on a different machine.
See here for more info - Flask environment variables being ignored (FLASK_ENV and FLASK_APP) WINDOWS 10