Setting and retrieving environmental variables in flask applications Setting and retrieving environmental variables in flask applications flask flask

Setting and retrieving environmental variables in flask applications


python-dotenv actually has nothing to do with Flask. It is for your .env file to be translated into actual env variables. So if you're going to have actual env variables without it, your os.getenv should still work.

Sidenote: You can also use os.environ:

os.environ.get("SECRET")


Set your environment variable in the interpreter:

export SECRET_KEY=123

Call the variable with environ.get():

from os import environfrom flask import Flaskapp = Flask(__name__)app.config['SECRET_KEY'] = environ.get('SECRET_KEY')

Verify:

@app.route('/verify')def verify():    return '<p>' + app.config['SECRET_KEY'] + '</p>'