Error: flask_appbuilder.security.sqla.manager:DB Creation and initialization failed when trying to start the application using gunicorn

Error: flask_appbuilder.security.sqla.manager:DB Creation and initialization failed when trying to start the application using gunicorn

我已经使用 flask_appbuilder 构建了一个应用程序。我正在尝试使用 gunicorn 为应用程序提供服务,但出现此错误:

$ gunicorn app:app
[2021-05-25 09:18:40 +0300] [1211266] [INFO] Starting gunicorn 20.1.0
[2021-05-25 09:18:40 +0300] [1211266] [INFO] Listening at: http://127.0.0.1:8000 (1211266)
[2021-05-25 09:18:40 +0300] [1211266] [INFO] Using worker: sync
[2021-05-25 09:18:40 +0300] [1211276] [INFO] Booting worker with pid: 1211276
2021-05-25 09:18:49,374:ERROR:flask_appbuilder.security.sqla.manager:DB Creation and initialization failed: Can't load plugin: sqlalchemy.dialects:None

应用程序文件如下所示

初始化.py 导入日志记录

from flask import Flask, request
from flask_graphql import GraphQLView
from flask_appbuilder import AppBuilder, SQLA
from .gql_schema import schema

"""
Logging configuration
"""

logging.basicConfig(format="%(asctime)s:%(levelname)s:%(name)s:%(message)s")
logging.getLogger().setLevel(logging.DEBUG)

app = Flask(__name__)
app.config.from_object("config")
db = SQLA(app)
appbuilder = AppBuilder(app, db.session)


from . import models, views, gql_schema, api

app.add_url_rule(
    "/graphql", view_func=GraphQLView.as_view("graphql", schema=schema, graphiql=True)
)

和cconfig.py

import os
from flask_appbuilder.security.manager import (
    AUTH_OID,
    AUTH_REMOTE_USER,
    AUTH_DB,
    AUTH_LDAP,
    AUTH_OAUTH,
)

basedir = os.path.abspath(os.path.dirname(__file__))

# Your App secret key
SECRET_KEY = "my secret key"
DB_ENGINE = os.environ.get('DB_ENGINE')
DB_USER = os.environ.get('DB_USER')
DB_PASSWORD = os.environ.get('DB_PASSWORD')
DB_NAME = os.environ.get('DB_NAME')
DB_HOST = os.environ.get('DB_HOST')
SQLALCHEMY_DATABASE_URI = f'{DB_ENGINE}://{DB_USER}:{DB_PASSWORD}@{DB_HOST}/{DB_NAME}'
SQLALCHEMY_TRACK_MODIFICATIONS = False
FAB_API_SWAGGER_UI = True
FAB_API_SHOW_STACKTRACE = True

可能是什么问题?

我明白问题出在哪里了。使用 gunicorn or flask_twisted 启动应用程序时数据库连接的 .env 变量返回 None 因此错误。

我必须安装 python_environ to support python_dotenv.

安装后现在可以访问变量了。

我正在使用 flask-twisted 运行 我的应用程序。这是我添加的。

用于 运行 应用程序的 server.py 文件。

from twisted.application.service import Application
from app import twisted


PORT = 8010

application = Application('twisted-flask')
twisted.run(run_reactor=False, port=PORT)

启动应用程序的bash (start.sh)文件

#!/bin/bash
echo "----- Starting Application -----"
source venv/bin/activate

path=${PWD}
cd $path

command pip install -r $path/requirements.txt

#
export PYTHONPATH=$PATH:$path
export CDLP_SETTINGS_MODULE=config.Config


twistd -y server.py