Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask app not using config file settings properly

Tags:

python

flask

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?

like image 749
Darius Cosden Avatar asked Sep 21 '18 21:09

Darius Cosden


People also ask

What is app config [' SECRET_KEY ']?

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"

What is config file in Flask?

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.


3 Answers

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.

like image 60
Roger Avatar answered Oct 24 '22 12:10

Roger


I had the same issue. I found out that all works fine using python run.py instead of flask run.

like image 36
Ivan El Mochilero Avatar answered Oct 24 '22 12:10

Ivan El Mochilero


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..

like image 29
wk14 Avatar answered Oct 24 '22 12:10

wk14