Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'NoneType' object has no attribute 'drivername'

I get this error when i start my app. Obviously it has something with SQLAlchemy. I 've been working this example with help of Corey Schaffer Flask tutorial.

> File "C:\Users\Matea\venv\lib\site-packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\Matea\venv\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\Matea\venv\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Matea\venv\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\Matea\venv\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Matea\venv\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Matea\venv\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Matea\venv\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\Matea\venv\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Matea\venv\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Matea\myblog\myblog\main\routes.py", line 11, in home
posts = Post.query.order_by(Post.date_posted.desc()).paginate(page=page, per_page=5)
File "C:\Users\Matea\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 514, in __get__
return type.query_class(mapper, session=self.sa.session())
File "C:\Users\Matea\venv\lib\site-packages\sqlalchemy\orm\scoping.py", line 74, in __call__
return self.registry()
File "C:\Users\Matea\venv\lib\site-packages\sqlalchemy\util\_collections.py", line 1001, in __call__
return self.registry.setdefault(key, self.createfunc())
File "C:\Users\Matea\venv\lib\site-packages\sqlalchemy\orm\session.py", line 2950, in __call__
return self.class_(**local_kw)
File "C:\Users\Matea\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 143, in __init__
bind = options.pop('bind', None) or db.engine
File "C:\Users\Matea\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 877, in engine
return self.get_engine()
File "C:\Users\Matea\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 896, in get_engine
return connector.get_engine()
File "C:\Users\Matea\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 556, in get_engine
self._sa.apply_driver_hacks(self._app, info, options)
File "C:\Users\Matea\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 830, in apply_driver_hacks
if info.drivername.startswith('mysql'):
AttributeError: 'NoneType' object has no attribute 'drivername'

here is some code: This is from my init.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
from flask_mail import Mail
from myblog.config import Config


db = SQLAlchemy()
bcrypt = Bcrypt()
login_manager = LoginManager()
login_manager.login_view = 'users.login'
login_manager.login_message_category = 'info'
mail = Mail()


def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(Config)

    db.init_app(app)
    bcrypt.init_app(app)
    login_manager.init_app(app)
    mail.init_app(app)

    from myblog.users.routes import users
    from myblog.posts.routes import posts
    from myblog.main.routes import main
    from myblog.errors.handlers import errors
    app.register_blueprint(users)
    app.register_blueprint(posts)
    app.register_blueprint(main)
    app.register_blueprint(errors)

    return app

this piece code is from config.py

import os


class Config:
    SECRET_KEY = os.environ.get('SECRET_KEY')
    SQLALCHEMY_DATABASE_URI = os.environ.get('SQLALCHEMY_DATABASE_URL')
    MAIL_SERVER = 'smtp.googlemail.com'
    MAIL_PORT = 587
    MAIL_USE_TLS = True
    MAIL_USERNAME = os.environ.get('EMAIL_USER')
    MAIL_PASSWORD = os.environ.get('EMAIL_PASS')

I hope that you will help me and if something is missing,please tell me, Thank you!

like image 211
Ivan M Avatar asked Aug 07 '18 13:08

Ivan M


3 Answers

if you have correctly set the environment variable in the .bash_proflie

    export EMAIL_USER='[email protected]'
    export EMAIL_PASS='Yourpassword'
    export SQLALCHEMY_DATABASE_URI='sqlite:///site.db'
    export SECRET_KEY='secretkeyxxxxxxxxxxxxxxxx'

then try restarting your computer.
if it still fails *Try to create the database in python *

from your yourflaskapp import db
db.create_all()

If your app is in blueprints

    use from yourapp import create_app
    app = create_app()
    app.app_context().push()

Always refresh your text editor and the terminal after making changes. Also try to make sure you are running your app in the right directory.

for more info visit on blueprints use

http://flask-sqlalchemy.pocoo.org/2.3/contexts/

like image 188
Eric Muhwezi Avatar answered Nov 15 '22 23:11

Eric Muhwezi


@Ivan M, I'm going through the same tutorial and had this problem as well. This happens because instructor moved the database URI and other params into environment variables (see Part 11 at about minute 27). It is a safety measure and good dev practice.
In order to make things work through config file, revert the changes in init.py and update config.py to have the direct link instead of square brackets:
SQLALCHEMY_DATABASE_URI = 'sqlite:///site.db'
UPD: same goes for SECRET_KEY and other params you wish to hard code:
SECRET_KEY = '5791628bb0b13ce0c676dfde280ba245'

like image 32
Nick Avatar answered Nov 15 '22 22:11

Nick


I was following the same tutorial and got into this issue while setting up the config.py file as guided by the instructor.

Case with me

I was using PyCharm. PyCharm has the functionality to set project-specific environment variables. This means you can set environment variables w/o modifying .bash or .bash_profile, etc. files.

Mistake I Did

While setting up environment variables, we aren't supposed to have:

  1. Spaces before and after the variable name
  2. Quotes surrounding the value, for example:
    • sqlite:///site.db βœ…
    • 'sqlite:///site.db'❌
  3. Quotes surrounding the variable name, for example:
    • SQLALCHEMY_DATABASE_URI βœ…
    • 'SQLALCHEMY_DATABASE_URI'❌
  4. Spaces before and after the =
  5. Spaces before and after the value

Note: If you are setting environment variables by modifying or creating .bash_profile or similar files, some of the above five points might not apply.

I did the 1st and 2nd mistake. So, silly. πŸ˜…

NamasteπŸ™

like image 33
Deepam Gupta Avatar answered Nov 16 '22 00:11

Deepam Gupta