I'm following the guide here trying to create a config.py file for my flask app's settings but it is not working.
from flask import Flask
# creates the application
def create_app():
app = Flask(__name__)
app.config.from_object('instance.config.DevelopmentConfig')
# home
@app.route('/')
def home():
return str(app.config['DEBUG']) # displays 'False'
# return str(app.config['SECRET_KEY']) -- displays 'dev'
return app
my instance/config.py
file
class DevelopmentConfig(object):
ENV = 'development'
DEBUG = True
SECRET_KEY = 'dev'
When I do flask run
it runs the server in production with debug off. I'm not using export FLASK_ENV
because I am under the impression that it isn't needed when you have a config.py
file.
Is my assumption wrong? Why is it correctly recording the app.config['SECRET_KEY']
but not the DEBUG
mode?
secret key is a random key used to encrypt your cookies and save send them to the browser. This error is because of this line in the Flask-Debugtoolbar code. To fix this you just need to set a SECRET_KEY in your config file. app.config['SECRET_KEY'] = "Your_secret_string"
Flask gives us the ability to choose a configuration file on load based on the value of an environment variable. This means that we can have several configuration files in our repository and always load the right one. Once we have several configuration files, we can move them to their own config directory.
According to the documentation here you shouldn't set Debug and ENV via code, otherwise, some extension can misunderstand that and act unexpectedly.
You still can set it via code and it may work using something like "python main.py" but again according to the documentation...
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.
The correct way of doing that is by using your terminal
Linux:
export FLASK_ENV=development
flask run
Windows:
set FLASK_ENV=development
flask run
Your config.py files should be more related to your database and API secrets keys.
I had the same issue. I found out that all works fine using python run.py
instead of flask run
.
Was having the same issue when following many tutorials online. Tried set FLASK_ENV=development
but still didn't work.
Ended up removing the ENV
and DEBUG
statements inside the config.py
, and typed $env:FLASK_ENV='development'
in the terminal and it finally worked with 'flask run'
Suggest to try $env:FLASK_ENV='development'
if you're new to Flask like me.
maybe some expert could help explain why only this worked for me..
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