Home > Enterprise >  AttributeError: 'NoneType' object has no attribute 'drivername' URI config imple
AttributeError: 'NoneType' object has no attribute 'drivername' URI config imple

Time:07-08

I see a lot of people with this error but nothing fixes it for me. I'm using a config.py file to hold my config classes. This seems to be where the issue lies. I'm also using the application factory pattern. I call the object in my __init__.py:

from flask import Flask, request
from flask_restful import Api, Resource, reqparse, abort
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_marshmallow import Marshmallow

import config

# Globally accessible libraries
db:SQLAlchemy = SQLAlchemy()
api:Api = Api()
migrate:Migrate = Migrate()
ma:Marshmallow = Marshmallow()


def init_app():
    """Initialize the core application."""
    app = Flask(__name__, instance_relative_config=False)
    app.config.from_object(config.Config)

    # Initialize Plugins
    db.init_app(app)
    api.init_app(app)
    migrate.init_app(app, db)
    ma.init_app(app)

    with app.app_context():

        # Include our Routes

And when I run the program I get this error

  File "/home/sai/.local/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 933, in apply_driver_hacks
    if sa_url.drivername.startswith('mysql'):
AttributeError: 'NoneType' object has no attribute 'drivername'

Here is my config.py:

import os
from pickle import TRUE

class Config(object):
    DEBUG = False
    TESTING = False
    CSRF_ENABLED = True
    SQLALCHEMY_DATABASE_URI = os.environ.get("DATABASE_URL")
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    SESSION_COOKIE_SECURE = True
    SESSION_COOKIE_HTTPONLY = True
    SECRET_KEY = os.environ.get("SECRET_KEY")

class ProductionConfig(Config):
    DEBUG = False
    SQLALCHEMY_TRACK_MODIFICATIONS = False

class DevelopmentConfig(Config):
    ENV = "development"
    DEBUG=True
    TESTING = True
    DEVELOPMENT = True
    SQLALCHEMY_TRACK_MODIFICATIONS = False

class SetupConfig(DevelopmentConfig):
    SQLALCHEMY_DATABASE_URI = "sqlite:///database.db"

I'm using the default .env loader that flask provides described here and I made sure my uri in my .env didn't have quotes around it just like the guide I used. Here is a snippet of .env:

DEBUG=True
FLASK_ENV=development
FLASK_APP=wsgi.py
DATABASE_URL = mysql pymysql://user:password@host/main

The only thing that gets my app to run is if I hard code the URI with app.config['SQLALCHEMY_DATABASE_URI'} = ... but that solution doesn't help me as I don't want to push the URI to a public repo. I've spent 4 hours of my day just moving things around hoping to fix it and I'm genuinely just lost. Any help would be hugely appreciated thank you!

CodePudding user response:

I think you might need to put load_env() in your Config class. I understand that flask has it's own way to reading .env files but maybe try it.

  • Related