Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask Config File - 'DEBUG=True' Do Nothing

I have a large flask application built inside a package called "MyApp" (exactly as shown here: http://flask.pocoo.org/docs/0.12/patterns/packages/)

According to the Flask documentation, the debug mode should enables the following features:

  1. it activates the debugger
  2. it activates the automatic reloader
  3. it enables the debug mode on the Flask application.

At the beginning I've run my flask application with the following command and everything were worked fine:

export FLASK_APP=MyApp
export FLASK_DEBUG=1 
flask run

Then I read about the correct way to setup a configuration system (including the debug mode). So I created the following config.py file:

class Config(object):
    DEBUG = False
    ...

class ProductionConfig(Config):
    ...

class DevelopmentConfig(Config):
    DEVELOPMENT = True
    DEBUG = True
    ...

CONFIGS = {
    "development": DevelopmentConfig,
    "production": ProductionConfig,
    "default": DevelopmentConfig
}

And in my application __init__.py file, I wrote:

app = Flask(__name__)
config_name = os.getenv('FLASK_CONFIGURATION', 'default')
app.config.from_object(CONFIGS[config_name])

Now, to run the application I enter a new command:

export FLASK_APP=MyApp
export FLASK_CONFIGURATION=development 
flask run

Unfortunately, this time the debug mode did not activated at all..

No debugger or automatic reloader has been activated. The only thing that has been changed was that app.debug is now equals to True.

I don't get it.. It looks like the DEBUG = TRUE is not working correctly.

Do you have any idea why does it happen?

like image 290
AlonP Avatar asked Aug 26 '17 14:08

AlonP


People also ask

What does the flask debug mode actually do?

According to the Flask documentation, the debug mode should enables the following features: it enables the debug mode on the Flask application. At the beginning I've run my flask application with the following command and everything were worked fine: Then I read about the correct way to setup a configuration system (including the debug mode).

What is config in flask?

Flask config | Learn How to perform config in Flask? Flask config is defined as the system of arrangements that enables the running of functional units together in accordance with the nature of the run, number, and chief characteristics of the web application being built.

Is there a way to debug flask_debug env ARG?

And the same, only command line would read the FLASK_DEBUG env arg, and if you using manage.py to run your app, it also not affect the debug mode. Yes, but the issue is that there seems to be no way to enter debug mode when using a custom management script.

What is the difference between app debug and flask_debug?

app.debug just turns on debug behavior for the application, while FLASK_DEBUG turns on debugging (and reloading) behavior for the server as well as setting app.debug = True.


1 Answers

Running with the debugger is different than setting the DEBUG config. You have to do both. Running the server in debug mode sets the config automatically. Typically, you should rely on that rather than setting the config directly.

The "correct way to configure" you read about is a) just another way, not the "correct" way, and b) only sets the config, not the FLASK_DEBUG environment variable, which is what controls the debug mode for the server.

Setting the environment variable FLASK_DEBUG=1, or passing the --debug option to the flask command as of Flask 2.2, tells flask run to wrap the application with the debugger and reloader. (app.run(debug=True) does the same, but the flask run command is preferred). app.debug switches some internal behavior in the Flask app, such as passing through errors to the interactive debugger that development mode enabled.

like image 155
davidism Avatar answered Oct 20 '22 17:10

davidism