运行“ flask db init”时出现“ KeyError:<flask.cli.scriptinfo object =“” at =“” 0x0000000003c05b70 =“”>“

时间:2019-01-09 10:23:32

标签: python flask pycharm

这里是python的新手。使用CRUD-PythonFlask网站上的PyCharm Community版本进行基础项目。执行迁移步骤时,在命令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文件:

# 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

需要帮助解决问题。任何帮助表示赞赏。

5 个答案:

答案 0 :(得分:0)

我认为问题不在于db变量的初始化。确保您的虚拟环境在flask项目文件夹中

答案 1 :(得分:0)

我在使用FLASK_APP环境变量的python文件而不是使用模块(或烧瓶术语中的app)时遇到过这种情况,即:

$ export FLASK_APP=flask_app.py

代替

$ export FLASK_APP=flask_app:app

我认为您可能会完全丢失FLASK_APP,而flask db init表现出了这种行为。

答案 2 :(得分:0)

从您的代码中删除## app.config.from_object(app_config [config_name])行,它将开始工作。.

答案 3 :(得分:0)

您的pycharm编辑器将目标设置为flask.py,也许您的目标不是flask.py或其他,但不会是__init __。py

答案 4 :(得分:0)

如果您的 app_config 和 config_name 正确,这通常是因为您没有正确配置环境变量 FLASK_APP。 你的 runpy.py 文件如何调用你的函数 create_app()? 如果喜欢:

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)

只需修改如下:

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)

另外,如果你的runpy.py中有多个flask应用,你可以在配置环境变量的时候选择你想在flask shell中使用哪一个:

export FLASK_APP=runpy.py:app1