[英]raise KeyError(key) KeyError: 'formatters'
我正在创建一个简单的 Flask API。 我正在使用 alembic 进行数据库迁移(Postgres)。 我的 alembic.ini 文件出现以下错误。
Traceback (most recent call last):
File "manage.py", line 18, in <module>
manager.run()
File "/Users/omorgan/.local/share/virtualenvs/cp-api-CkIfmfOt/lib/python3.6/site-packages/flask_script/__init__.py", line 417, i
n run
result = self.handle(argv[0], argv[1:])
File "/Users/admin/.local/share/virtualenvs/cp-api-CkIfmfOt/lib/python3.6/site-packages/flask_script/__init__.py", line 386, i
n handle
res = handle(*args, **config)
File "/Users/admin/.local/share/virtualenvs/cp-api-CkIfmfOt/lib/python3.6/site-packages/flask_script/commands.py", line 216, in __call__
return self.run(*args, **kwargs)
File "/Users/admin/.local/share/virtualenvs/cp-api-CkIfmfOt/lib/python3.6/site-packages/flask_migrate/__init__.py", line 95, in wrapped
f(*args, **kwargs)
File "/Users/admin/.local/share/virtualenvs/cp-api-CkIfmfOt/lib/python3.6/site-packages/flask_migrate/__init__.py", line 215,in migrate
version_path=version_path, rev_id=rev_id)
File "/Users/admin/.local/share/virtualenvs/cp-api-CkIfmfOt/lib/python3.6/site-packages/alembic/command.py", line 176, in revision
script_directory.run_env()
File "/Users/admin/.local/share/virtualenvs/cp-api-CkIfmfOt/lib/python3.6/site-packages/alembic/script/base.py", line 427, inrun_env
util.load_python_file(self.dir, 'env.py')
File "/Users/admin/.local/share/virtualenvs/cp-api-CkIfmfOt/lib/python3.6/site-packages/alembic/util/pyfiles.py", line 81, inload_python_file
module = load_module_py(module_id, path)
File "/Users/admin/.local/share/virtualenvs/cp-api-CkIfmfOt/lib/python3.6/site-packages/alembic/util/compat.py", line 82, in load_module_py
spec.loader.exec_module(module)
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "migrations/env.py", line 12, in <module>
fileConfig(config.config_file_name)
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/config.py", line 76, in fileConfig
formatters = _create_formatters(cp)
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/config.py", line 109, in _create_formatters
flist = cp["formatters"]["keys"]
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/configparser.py", line 959, in __getitem__
raise KeyError(key)
KeyError: 'formatters'
我的环境:Python 3.6.5 Pipenv Mac OSX Mojave
我的 Alembic 文件如下所示:
# A generic, single database configuration.
[alembic]
# path to migration scripts
script_location = alembic
# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s
# timezone to use when rendering the date
# within the migration file as well as the filename.
# string value is passed to dateutil.tz.gettz()
# leave blank for localtime
# timezone =
# max length of characters to apply to the
# "slug" field
#truncate_slug_length = 40
# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false
# set to 'true' to allow .pyc and .pyo files without
# a source .py file to be detected as revisions in the
# versions/ directory
# sourceless = false
# version location specification; this defaults
# to alembic/versions. When using multiple version
# directories, initial revisions must be specified with --version-path
# version_locations = %(here)s/bar %(here)s/bat alembic/versions
# the output encoding used when revision files
# are written from script.py.mako
# output_encoding = utf-8
sqlalchemy.url = postgres://crowdplan:cr0wdpl@n@localhost:5432/crowdplan
# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
qualname =
[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = INFO
handlers =
qualname = alembic
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
我已经进行了全面搜索,似乎字典中缺少该值,但是,它被传递给文件中列出的解释器。 此文件在初始化时由 alembic 自动生成。
如果有人能指出我正确的方向,那将不胜感激。
谢谢,
当我遇到这个问题时,它是文件路径中的一个错字。 我建议您验证文件是否存在以防万一,然后尝试加载配置文件。
有很多方法可以检查文件是否存在,一个快速的谷歌提出了这个 - https://www.pythontutorial.net/python-basics/python-check-if-file-exists/
这是 3 年前提出的一个老问题,但我遇到了同样的问题,并且从堆栈跟踪中解决方案对我来说并不直观。 所以,我会留下一个答案,以防其他人觉得它有用。
遇到同样的错误,我正在使用 docker 构建应用程序,根本原因是缺少“alembic.ini”文件。
我遇到了同样的问题。 我意识到我可以使用 Flask-migrate 而不是直接使用 alembic 并且能够顺利运行迁移:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.