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

Total
1
Shares

I have built an application using flask_appbuilder. I am trying to serve the application using gunicorn and am getting this error:

$ 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

The app file looks like this

init.py
import logging

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)
)

and the 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

What could be the issue?


Solution

I figured out what the problem was. The .env variables for the db connection when starting the app with gunicorn or flask_twisted where returning None hence the error.

enter image description here
I had to install python_environ to support python_dotenv.

After installation now the variables can be accessed.
enter image description here

I am using flask-twisted to run my application. Here is what I added.

The server.py file for running the application.

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


PORT = 8010

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

The bash (start.sh) file to start the application

#!/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
Leave a Reply

Your email address will not be published. Required fields are marked *