0

I have a separate file called config.py, in it I have my environment classes, then I import to python, set the config file to app.config using the from_object method, but when I call flask run it still says production even though debug mode is set to true.

from flask import Flask
from config import DevConfig

 app = Flask(__name__)
 app.config.from_object(DevConfig)
 print(DevConfig.DEBUG)
 @app.route('/')
 def home():
    return '<h1>Hello World</h1>'

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

config.py

class Config(object):
    pass

class ProdConfig(Config):
    pass
class DevConfig(Config):
    DEBUG = True

result

 * Serving Flask app "main.py"
 * 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
True
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Pari Baker
  • 696
  • 4
  • 19

1 Answers1

2

According to Flask documentation for Builtin configuration values:

The env attribute maps to this config key. This is set by the FLASK_ENV environment variable and may not behave as expected if set in code.

The configuration best practices documentation shows an example of using multiple configurations for different environment.

Following the documentation, I got Debug mode: on but the environment is still shown as production.

Folder structure:

.
├── app.py
└── configurations.py

app.py:

from flask import Flask
from configurations import DevelopmentConfig


app = Flask(__name__)
app.config.from_object(DevelopmentConfig)

@app.route('/')
def home():
    return '<h1>Hello World</h1>'

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

configurations.py:

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

class ProductionConfig(Config):
    pass

class DevelopmentConfig(Config):
    DEBUG = True

class TestingConfig(Config):
    TESTING = True

Running the app:

(venv) python app.py
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 312-674-275
127.0.0.1 - - [18/May/2019 23:10:36] "GET / HTTP/1.1" 200 -

Installed packages:

Click==7.0
Flask==1.0.3
itsdangerous==1.1.0
Jinja2==2.10.1
MarkupSafe==1.1.1
pkg-resources==0.0.0
Werkzeug==0.15.4

As debug mode is on, the app runs in development environment. But Flask documentation suggests to use flask method to run the project.

To switch Flask to the development environment and enable debug mode, set FLASK_ENV:

$ export FLASK_ENV=development
$ flask run

(On Windows, use set instead of export.)

From Flask doc:

Example:

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

This works well for the common case but it does not work well for development which is why from Flask 0.11 onwards the flask method is recommended. The reason for this is that due to how the reload mechanism works there are some bizarre side-effects (like executing certain code twice, sometimes crashing without message or dying when a syntax or import error happens).

arshovon
  • 13,270
  • 9
  • 51
  • 69