AttributeError when running a flask app in flask shell


I have finished a flask app. When I run it by python, the app can work perfectly.

But when I want to open flask shell by flask shell or even just flask, it tell me:

Traceback (most recent call last):
  File "", line 556, in list_commands
  File "", line 388, in load_app
    app = locate_app(self, import_name, name)
  File "", line 257, in locate_app
    return find_best_app(script_info, module)
  File "", line 83, in find_best_app
    app = call_factory(script_info, app_factory)
  File "", line 117, in call_factory
    return app_factory(script_info)
  File "", line 41, in create_app
    app.config['SECRET_KEY'] = config.get('secret', '!secret!')
AttributeError: 'ScriptInfo' object has no attribute 'get'

The last sentence is here:

def create_app(config):
    app = Flask(
    app.config['SECRET_KEY'] = config.get('secret', '!secret!')

The config is a dictionary, which is given by:

def start_server(run_cfg=None, is_deploy=False):
    config = {
        'use_cdn': False,
        'debug': run_cfg.get('debug', False),
        'secret': md5('!secret!'),
        'url_prefix': None,
        'debugtoolbar': True
    app = create_app(config)

I am confused with how the dictionary config is transformed to be a ScriptInfo?

And what should I do to solve the problem?

Asked By: blueice



Now I solve the problem.

I have a file to deal all shell command lines. So the right operation is input:

python shell

And now it works normally. (OK, I still don’t know why……)

Answered By: blueice

seeing that you’ve already resolved your initial query, i wanted to suggest a better structured config write up for your future flask apps that would also make it easier to add more config variables in the case your app becomes bigger.

Consider having the configs in a module of their own, preferably in a folder name instance in the app’s root folder. Here’s a sample.

This module sets the configurations for the application
import os

class Config(object):
    """Parent configuration class."""
    DEBUG = False
    SECRET_KEY = os.getenv("SECRET_KEY")
    DATABASE_URL = os.getenv("DATABASE_URL")

class DevelopmentConfig(Config):
    """Development phase configurations"""
    DEBUG = True

class TestingConfig(Config):
    """Testing Configurations."""
    TESTING = True
    DEBUG = True

class ReleaseConfig(Config):
    """Release Configurations."""
    DEBUG = False
    TESTING = False

app_config = {
    'development': DevelopmentConfig,
    'testing': TestingConfig,
    'release': ReleaseConfig,
Answered By: mburii_ya_mwitu
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.