Reputation: 29
Shown below is the code from my launch.json file for 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?
{
"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
Upvotes: 1
Views: 4475
Reputation: 29598
The launch.json file is only used when running your app through VS Code's debugger.
See https://code.visualstudio.com/docs/python/debugging#_initialize-configurations:
Initialize configurations
A configuration drives VS Code's behavior during a debugging session. Configurations are defined in a
launch.json
file that's stored in a.vscode
folder in your workspace.
Since you mentioned
... I run my flask application from the run.py script, ...
and you are testing using the Python shell
>>> import os >>> print(os.environ.get('FLASK_DEBUG')) None
means you never use VS Code's debugger and thus those environment variables will never be loaded/read.
I recommend going through VS Code's tutorial on Python Debugging, especially the section on Flask Debugging. You already have the first steps done, namely adding a launch.json file with the Flask configuration. The main point here is the "module": "flask"
line and adding the "--no-debugger"
arg:
{
"version": "0.2.0",
"configurations": [
{
"name": "run-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,
},
]
}
Then simply select that configuration from the debugger dropdown:
Testing with some quickstart Flask code:
import os
from flask import Flask
app = Flask(__name__)
@app.route("/")
def root():
env_names = [
"FLASK_APP",
"FLASK_ENV",
"FLASK_DEBUG",
"EMAIL_USER",
"EMAIL_PASS",
]
for env_name in env_names:
print(os.environ.get(env_name))
return "It works!"
...shows that the environment variables are read properly and that debug mode is now "on"
.
The main disadvantage of this compared to the other answer using a .flaskenv
file is sharing these env vars across IDEs, with other members of your team, or with your CI/CD pipeline. You will have to define them separately.
The advantage though is allowing you to use common debugger features, such as breakpoints on code (as seen from the screenshot above where execution is stopped on the print
statement), and seeing the values of your variables as your code runs.
Upvotes: 0
Reputation: 516
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
Upvotes: 0