EJ Singh
EJ Singh

Reputation: 43

Got "KeyError: <flask.cli.ScriptInfo object at 0x0000000003C05B70>" when running "flask db init"

New to python here. Working on a basic project using PyCharm Community edition from CRUD-PythonFlask the site. While performing migration steps got errors as below after the command flask db init:

Traceback (most recent call last):
File "C:\Python27\Lib\runpy.py", line 174, in_run_module_as_main
"__main__", fname, loader, pkg_name)
File "C:\Python27\Lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "C:\Users\elkkanah\PycharmProjects\aquauk\venv\Scripts\flask.exe\__main__.py", line 9, in <m
odule>
File "c:\users\elkkanah\pycharmprojects\aquauk\venv\lib\site-packages\flask\cli.py", line 894, in
main
cli.main(args=args, prog_name=name)
File "c:\users\elkkanah\pycharmprojects\aquauk\venv\lib\site-packages\flask\cli.py", line 557, in
main
return super(FlaskGroup, self).main(*args, **kwargs)
File "c:\users\elkkanah\pycharmprojects\aquauk\venv\lib\site-packages\click\core.py", line 717, i
n main
rv = self.invoke(ctx)
File "c:\users\elkkanah\pycharmprojects\aquauk\venv\lib\site-packages\click\core.py", line 1137,
in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "c:\users\elkkanah\pycharmprojects\aquauk\venv\lib\site-packages\click\core.py", line 1137,
in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "c:\users\elkkanah\pycharmprojects\aquauk\venv\lib\site-packages\click\core.py", line 956, i
n invoke
return ctx.invoke(self.callback, **ctx.params)
File "c:\users\elkkanah\pycharmprojects\aquauk\venv\lib\site-packages\click\core.py", line 555, i
n invoke
return callback(*args, **kwargs)
File "c:\users\elkkanah\pycharmprojects\aquauk\venv\lib\site-packages\click\decorators.py", line
17, in new_func
return f(get_current_context(), *args, **kwargs)
File "c:\users\elkkanah\pycharmprojects\aquauk\venv\lib\site-packages\flask\cli.py", line 411, in
decorator
with __ctx.ensure_object(ScriptInfo).load_app().app_context():
File "c:\users\elkkanah\pycharmprojects\aquauk\venv\lib\site-packages\flask\cli.py", line 377, in
load_app
raise_if_not_found=False)
File "c:\users\elkkanah\pycharmprojects\aquauk\venv\lib\site-packages\flask\cli.py", line 254, in
locate_app
return find_best_app(script_info, module)
File "c:\users\elkkanah\pycharmprojects\aquauk\venv\lib\site-packages\flask\cli.py", line 76, in
find_best_app
app = call_factory(script_info, app_factory)
File "c:\users\elkkanah\pycharmprojects\aquauk\venv\lib\site-packages\flask\cli.py", line 114, in
call_factory
return app_factory(script_info)
File "C:\Users\elkkanah\PycharmProjects\aquauk\app\__init__.py", line 19, in create_app
app.config.from_object(app_config[config_name])
KeyError: <flask.cli.ScriptInfo object at 0x0000000003C05B70>

__init__.py file:

# app/__init__.py

# third-party imports
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_migrate import Migrate

 # local imports
 from config import app_config

 # db variable initialization
 db = SQLAlchemy()
 login_manager = LoginManager()


 def create_app(config_name):
     app = Flask(__name__, instance_relative_config=True)
     app.config.from_object(app_config[config_name])
     app.config.from_pyfile('config.py')
     db.init_app(app)

     login_manager.init_app(app)
     login_manager.login_message = "You must be logged in to access this page."
     login_manager.login_view = "auth.login"

     migrate = Migrate(app, db)

     from app import models

     # temporary route
     @app.route('/')
     def hello_world():
         return 'Hello, World!'

     return app

Need help in sorting the issue. Any help is appreciated.

Upvotes: 4

Views: 7083

Answers (5)

曹占铎
曹占铎

Reputation: 1

if your app_config and config_name if right,this is usually because you did not configure the environment variable FLASK_APP correctly. How does your runpy.py file call your function create_app()? if it likes:

if __name__ == '__main__':
  app = create_app('DEVELOPMENT')

  @app.shell_context_processor
  def make_shell_context():
      return dict(db=db, User=User, Role=Role)

  migrate = Migrate(app, db)
  app.run(host='0.0.0.0', port=50001)

just modify it as follows:

app = create_app('DEVELOPMENT')

@app.shell_context_processor
def make_shell_context():
    return dict(db=db, User=User, Role=Role)

migrate = Migrate(app, db)

if __name__ == '__main__':
  app.run(host='0.0.0.0', port=50001)

Also,if there are more than one flask app in your runpy.py,you can select which one you want use in flask shell when configure the environment variable:

export FLASK_APP=runpy.py:app1

Upvotes: 0

Tracy Cora
Tracy Cora

Reputation: 1

Your pycharm editor sets the target to flask.py, maybe yours is not flask.py or maybe other, but it won't be __init__.py

Upvotes: 0

Uday
Uday

Reputation: 11

Remove ## app.config.from_object(app_config[config_name]) line from your code and it will start working..

Upvotes: 1

stephendwolff
stephendwolff

Reputation: 1712

I have experienced this when using a python file for the FLASK_APP environment variable, rather than using a module (or app in flask terminology), ie:

$ export FLASK_APP=flask_app.py

instead of

$ export FLASK_APP=flask_app:app

I think you may be missing the FLASK_APP entirely though, with flask db init exhibiting that behaviour.

Upvotes: 11

Muriithi K.
Muriithi K.

Reputation: 1

I do not think the issue lies with the initialization of the db variable. Ensure that your virtual environment is inside the your flask project folder

Upvotes: 0

Related Questions