AttributeError when running a flask app in flask shell
Question:
I have finished a flask app. When I run it by python run.py
, 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 "f:programsanacondaenvsweblibsite-packagesflaskcli.py", line 556, in list_commands
rv.update(info.load_app().cli.list_commands(ctx))
File "f:programsanacondaenvsweblibsite-packagesflaskcli.py", line 388, in load_app
app = locate_app(self, import_name, name)
File "f:programsanacondaenvsweblibsite-packagesflaskcli.py", line 257, in locate_app
return find_best_app(script_info, module)
File "f:programsanacondaenvsweblibsite-packagesflaskcli.py", line 83, in find_best_app
app = call_factory(script_info, app_factory)
File "f:programsanacondaenvsweblibsite-packagesflaskcli.py", line 117, in call_factory
return app_factory(script_info)
File "C:UserszkhpDesktopflask-bigger-masterbackendstartup.py", 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(
__name__,
template_folder=template_folder,
static_folder=static_folder
)
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?
Answers:
Now I solve the problem.
I have a file manage.py
to deal all shell command lines. So the right operation is input:
python manage.py shell
And now it works normally. (OK, I still don’t know why……)
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
CSRF_ENABLED = True
SECRET_KEY = os.getenv("SECRET_KEY")
DATABASE_URL = os.getenv("DATABASE_URL")
BUNDLE_ERRORS = True
class DevelopmentConfig(Config):
"""Development phase configurations"""
DEBUG = True
class TestingConfig(Config):
"""Testing Configurations."""
TESTING = True
DEBUG = True
DATABASE_URL = os.getenv("DATABASE_TEST_URL")
class ReleaseConfig(Config):
"""Release Configurations."""
DEBUG = False
TESTING = False
app_config = {
'development': DevelopmentConfig,
'testing': TestingConfig,
'release': ReleaseConfig,
}
I have finished a flask app. When I run it by python run.py
, 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 "f:programsanacondaenvsweblibsite-packagesflaskcli.py", line 556, in list_commands
rv.update(info.load_app().cli.list_commands(ctx))
File "f:programsanacondaenvsweblibsite-packagesflaskcli.py", line 388, in load_app
app = locate_app(self, import_name, name)
File "f:programsanacondaenvsweblibsite-packagesflaskcli.py", line 257, in locate_app
return find_best_app(script_info, module)
File "f:programsanacondaenvsweblibsite-packagesflaskcli.py", line 83, in find_best_app
app = call_factory(script_info, app_factory)
File "f:programsanacondaenvsweblibsite-packagesflaskcli.py", line 117, in call_factory
return app_factory(script_info)
File "C:UserszkhpDesktopflask-bigger-masterbackendstartup.py", 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(
__name__,
template_folder=template_folder,
static_folder=static_folder
)
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?
Now I solve the problem.
I have a file manage.py
to deal all shell command lines. So the right operation is input:
python manage.py shell
And now it works normally. (OK, I still don’t know why……)
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
CSRF_ENABLED = True
SECRET_KEY = os.getenv("SECRET_KEY")
DATABASE_URL = os.getenv("DATABASE_URL")
BUNDLE_ERRORS = True
class DevelopmentConfig(Config):
"""Development phase configurations"""
DEBUG = True
class TestingConfig(Config):
"""Testing Configurations."""
TESTING = True
DEBUG = True
DATABASE_URL = os.getenv("DATABASE_TEST_URL")
class ReleaseConfig(Config):
"""Release Configurations."""
DEBUG = False
TESTING = False
app_config = {
'development': DevelopmentConfig,
'testing': TestingConfig,
'release': ReleaseConfig,
}