Fernando Velasco Borea
Fernando Velasco Borea

Reputation: 183

Flask ignores config Class

I'm trying to set up a config file using classes and for some reason Flask just ignores the settings placed on the config class.

config.py:

class Config:
    FLASK_ENV = 'development'

__init__.py:

from flask import Flask

def init_app():
    # Set up core app
    app = Flask(__name__)
    app.config.from_object('config.Config')

    with app.app_context():
        # Import app routes
        from .index import routes

        # Register blueprints
        app.register_blueprint(routes.index_bp)

    return app

The file structure is:

.
├── config.py
├── requirements.txt
├── application
│   ├── __init__.py
│   └── index
│       ├── routes.py
│       ├── static
│       └── templates
└── wsgi.py

When I use environment variables to set the flask environment, it just works perfectly, however, I've seen some tutorials, like this one, that make use of classes to define the configuration using a Python file.

Update: I added a print statement to the config dictionary as I wanted to check if the configuration is being loaded, and indeed, it is loaded:

 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
**development**
 * Running on http://127.0.0.1:5000 (Press CTRL+C to quit)

I also tried to specify the settings using app.config['FLASK_ENV']='development' and it still ignores it. It seems that Flask is straight ignoring my configuration settings, this is my entry point file, in case it helps out:

from application import init_app


app = init_app()

if __name__ == "__main__":
    app.run()

Upvotes: 0

Views: 339

Answers (1)

rajat yadav
rajat yadav

Reputation: 383

Quoting from Flask Configuration Handling 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.

https://flask.palletsprojects.com/en/1.1.x/config/

As you can see, the app needs the variable right at the start and your system might be configured otherwise.

You may try unsetting the previous environment variable and that might work.

Upvotes: 1

Related Questions