12
votes

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?

1

1 Answers

9
votes

Running with the debugger is different than setting the DEBUG config. You have to do both. Running the server in development 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_ENV or FLASK_DEBUG environment variables, which is what controls the debug mode for the server.

Setting the environment variable FLASK_ENV=development tells flask run to wrap the application with the debugger and reloader. (app.run(debug=True) does the same but can't set FLASK_ENV, which only has an env var. The flask run command is preferred now). app.debug switches some internal behavior in the Flask app, such as passing through errors to the interactive debugger that development mode enabled.