How to assign settings base on testing and production env in flask?
Question: I would like to assign different settings base on various condition such as
testing and production. Therefore I want `create_app` accept an argument
indicating settings and make `create_app` load different settings as follows.

app.py

    
    
    def create_app(config_file):
        app.config['setting'] = config_file['setting']
    
    
    if __name__ == "__main__":
        app = create_app(production_setting)
        app.run(host='0.0.0.0', port=config.SERVER_PORT, threaded=True, debug=True)
    

views.py

    
    
    import stuff
    
    
    if app.config['setting'] == 'testing':
         print app.config['setting']
    

test_views.py

    
    
    @pytest.fixture
    def client():
        testing_setting['setting'] = 'stuff'
    
        app = create_app(testing_setting)
        client = app.test_client()
        return client 
    

But I get this error when I run python app.py:

    
    
    AttributeError: 'module' object has no attribute 'config'
    

Is there any way to pass arguments from `app` to `views`?


Answer: Here is how I'd do it:

    
    
    def create_app(config=None):
      app = Flask(__name__)
    
      if not config:
        config = 'your_app.config.Production'
    
      app.config.from_object(config)
    

config.py, peered with the file containing `create_app`:

    
    
    class BaseConfig(object):
      SOME_BASE_SETTINGS = 'foo'
      DEBUG = False
    
    class Development(BaseConfig):
      DEBUG = True
    
    class Production(BaseConfig):
      pass
    

This allows your app to default to production but for instance you could pass
a different configuration name when creating the app for development:

    
    
    create_app('your_app.config.Development')
    

For more information on this and a similar example, check out the
documentation: <http://flask.pocoo.org/docs/0.10/config/#development-
production>

