TL;DR
I don't want to export
my variables every time I log on. I want them in my config.
From the docs
Using the environment variables as described above is recommended. While it is possible to set ENV and DEBUG in your config or code, this is strongly discouraged.
How do I do it anyway? How is it 'possible'?
MORE - What I've Tried
I have a flask.cfg
with FLASK_ENV='development'
. I run flask run
and I get development
printed. But the production server has already started. It ignores the following:
app.config.from_pyfile("flask.cfg")
print(f"{app.config['ENV']}") => development
This is in a personal project of no consequence. That's why I am ignoring best practices for convenience.
Flask has a config attribute by default, it’s a dictionary that you can access and set the configuration to the “flask environment” But this is not the only way, you don’t need to set multiple times like: You can use a method called “ update ”. So we will basically have an initial setup to our project like this:
This gives you defaults that can be overridden from environment variables. The ENV and DEBUG config values are special because they may behave inconsistently if changed after the app has begun setting up. In order to set the environment and debug mode reliably, Flask uses environment variables.
It looks as if the FLASK_APP variable incorporates the 2 lines of code that we avoided. Similarly, the FLASK_ENV variable sets the environment on which we want our flask application to run. For example, if we put FLASK_ENV=development the environment will be switched to development. By default, the environment is set to development.
To switch Flask to the development environment and enable debug mode, set FLASK_ENV: > $ export FLASK_ENV=development > $ flask run (On Windows, use set instead of export.) Using the environment variables as described above is recommended. While it is possible to set ENV and DEBUG in your config or code, this is strongly discouraged.
What about this: install python-dotenv package, create a .flaskenv file in your project root folder and add, for example, this:
FLASK_APP=app.py (or whatever you named it)
FLASK_ENV=development (or production)
Save. Do flask run.
If you move your config into Python, things get a little easier. Consider
from flask import Flask
from config import Config
app = Flask(__name__)
app.config.from_object(Config)
where config.py
looks like
import os
class Config:
SECRET_KEY = os.environ.get('SECRET_KEY', 'default sekret')
This gives you defaults that can be overridden from environment variables.
Update march 2020:
According to the Flask devs, you cannot do this anymore:
The ENV and DEBUG config values are special because they may behave inconsistently if changed after the app has begun setting up. In order to set the environment and debug mode reliably, Flask uses environment variables.
The environment is used to indicate to Flask, extensions, and other programs, like Sentry, what context Flask is running in. It is controlled with the FLASK_ENV environment variable and defaults to production.
Setting FLASK_ENV to development will enable debug mode. flask run will use the interactive debugger and reloader by default in debug mode. To control this separately from the environment, use the FLASK_DEBUG flag.
To switch Flask to the development environment and enable debug mode, set FLASK_ENV:
> $ export FLASK_ENV=development
> $ flask run (On Windows, use set instead of export.)
Using the environment variables as described above is recommended. While it is possible to set ENV and DEBUG in your config or code, this is strongly discouraged. They can’t be read early by the flask command, and some systems or extensions may have already configured themselves based on a previous value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With