得到了“KeyError: “在运行时”flask db init“

问题描述 投票:0回答:2

这里是python的新手。使用来自CRUD-PythonFlask网站的PyCharm社区版本开展基础项目。执行迁移步骤时,在命令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

需要帮助来排序问题。任何帮助表示赞赏。

python flask pycharm
2个回答
0
投票

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


0
投票

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

$ export FLASK_APP=flask_app.py

代替

$ export FLASK_APP=flask_app:app

我认为你可能完全错过了FLASK_APP,因为flask db init表现出这种行为。


0
投票

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


0
投票

你的pycharm编辑器将目标设置为flask.py,也许你的不是flask.py或其他,但它不会是__init__.py

© www.soinside.com 2019 - 2024. All rights reserved.