CoreyMSchafer/code_snippets

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

Opened this issue · 6 comments

I am testing the code on Windows 10 but I am getting the following error. Could you please tell me how to correct this issue

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

Traceback (most recent call last)

File "C:\Python38\Lib\site-packages\sqlalchemy\util\_collections.py", line 1020, in __call__

return self.registry[key]

During handling of the above exception, another exception occurred:
File "C:\Python38\Lib\site-packages\flask\app.py", line 2464, in __call__

return self.wsgi_app(environ, start_response)

File "C:\Python38\Lib\site-packages\flask\app.py", line 2450, in wsgi_app

response = self.handle_exception(e)

File "C:\Python38\Lib\site-packages\flask\app.py", line 1867, in handle_exception

reraise(exc_type, exc_value, tb)

File "C:\Python38\Lib\site-packages\flask\_compat.py", line 39, in reraise

raise value

File "C:\Python38\Lib\site-packages\flask\app.py", line 2447, in wsgi_app

response = self.full_dispatch_request()

File "C:\Python38\Lib\site-packages\flask\app.py", line 1952, in full_dispatch_request

rv = self.handle_user_exception(e)

File "C:\Python38\Lib\site-packages\flask\app.py", line 1821, in handle_user_exception

reraise(exc_type, exc_value, tb)

File "C:\Python38\Lib\site-packages\flask\_compat.py", line 39, in reraise

raise value

File "C:\Python38\Lib\site-packages\flask\app.py", line 1950, in full_dispatch_request

rv = self.dispatch_request()

File "C:\Python38\Lib\site-packages\flask\app.py", line 1936, in dispatch_request

return self.view_functions[rule.endpoint](**req.view_args)

File "C:\Users\mrsst\Desktop\Flask_Blog\flaskblog\main\routes.py", line 11, in home

posts = Post.query.order_by(Post.date_posted.desc()).paginate(page=page, per_page=5)

File "C:\Python38\Lib\site-packages\flask_sqlalchemy\__init__.py", line 514, in __get__

return type.query_class(mapper, session=self.sa.session())

File "C:\Python38\Lib\site-packages\sqlalchemy\orm\scoping.py", line 78, in __call__

return self.registry()

File "C:\Python38\Lib\site-packages\sqlalchemy\util\_collections.py", line 1022, in __call__

return self.registry.setdefault(key, self.createfunc())

File "C:\Python38\Lib\site-packages\sqlalchemy\orm\session.py", line 3286, in __call__

return self.class_(**local_kw)

File "C:\Python38\Lib\site-packages\flask_sqlalchemy\__init__.py", line 138, in __init__

bind = options.pop('bind', None) or db.engine

File "C:\Python38\Lib\site-packages\flask_sqlalchemy\__init__.py", line 943, in engine

return self.get_engine()

File "C:\Python38\Lib\site-packages\flask_sqlalchemy\__init__.py", line 962, in get_engine

return connector.get_engine()

File "C:\Python38\Lib\site-packages\flask_sqlalchemy\__init__.py", line 555, in get_engine

options = self.get_options(sa_url, echo)

File "C:\Python38\Lib\site-packages\flask_sqlalchemy\__init__.py", line 570, in get_options

self._sa.apply_driver_hacks(self._app, sa_url, options)

File "C:\Python38\Lib\site-packages\flask_sqlalchemy\__init__.py", line 883, in apply_driver_hacks

if sa_url.drivername.startswith('mysql'):

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

This issue has been fixed. In the config.py file:
change the SQLALCHEMY_DATABASE_URI line with this SQLALCHEMY_DATABASE_URI = 'sqlite:///site.db'

thank you bro

Wouldn't call this a fix as such. Want to be able to read URI from an OS variable & then register with the Flask app. I can get the app to work if I make literal strings in the Config class. No so great for MAIL_USERNAME, MAIL_PASSWORD etc. os.getenv(whatever) works OK with simple print(whatever) test. app.config[] values don't seem to make it past this point app.config.from_object(Config) in init.py

I am testing the code on Windows 10 but I am getting the following error. Could you please tell me how to correct this issue

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

Traceback (most recent call last)

File "C:\Python38\Lib\site-packages\sqlalchemy\util\_collections.py", line 1020, in __call__

return self.registry[key]

During handling of the above exception, another exception occurred:
File "C:\Python38\Lib\site-packages\flask\app.py", line 2464, in __call__

return self.wsgi_app(environ, start_response)

File "C:\Python38\Lib\site-packages\flask\app.py", line 2450, in wsgi_app

response = self.handle_exception(e)

File "C:\Python38\Lib\site-packages\flask\app.py", line 1867, in handle_exception

reraise(exc_type, exc_value, tb)

File "C:\Python38\Lib\site-packages\flask\_compat.py", line 39, in reraise

raise value

File "C:\Python38\Lib\site-packages\flask\app.py", line 2447, in wsgi_app

response = self.full_dispatch_request()

File "C:\Python38\Lib\site-packages\flask\app.py", line 1952, in full_dispatch_request

rv = self.handle_user_exception(e)

File "C:\Python38\Lib\site-packages\flask\app.py", line 1821, in handle_user_exception

reraise(exc_type, exc_value, tb)

File "C:\Python38\Lib\site-packages\flask\_compat.py", line 39, in reraise

raise value

File "C:\Python38\Lib\site-packages\flask\app.py", line 1950, in full_dispatch_request

rv = self.dispatch_request()

File "C:\Python38\Lib\site-packages\flask\app.py", line 1936, in dispatch_request

return self.view_functions[rule.endpoint](**req.view_args)

File "C:\Users\mrsst\Desktop\Flask_Blog\flaskblog\main\routes.py", line 11, in home

posts = Post.query.order_by(Post.date_posted.desc()).paginate(page=page, per_page=5)

File "C:\Python38\Lib\site-packages\flask_sqlalchemy\__init__.py", line 514, in __get__

return type.query_class(mapper, session=self.sa.session())

File "C:\Python38\Lib\site-packages\sqlalchemy\orm\scoping.py", line 78, in __call__

return self.registry()

File "C:\Python38\Lib\site-packages\sqlalchemy\util\_collections.py", line 1022, in __call__

return self.registry.setdefault(key, self.createfunc())

File "C:\Python38\Lib\site-packages\sqlalchemy\orm\session.py", line 3286, in __call__

return self.class_(**local_kw)

File "C:\Python38\Lib\site-packages\flask_sqlalchemy\__init__.py", line 138, in __init__

bind = options.pop('bind', None) or db.engine

File "C:\Python38\Lib\site-packages\flask_sqlalchemy\__init__.py", line 943, in engine

return self.get_engine()

File "C:\Python38\Lib\site-packages\flask_sqlalchemy\__init__.py", line 962, in get_engine

return connector.get_engine()

File "C:\Python38\Lib\site-packages\flask_sqlalchemy\__init__.py", line 555, in get_engine

options = self.get_options(sa_url, echo)

File "C:\Python38\Lib\site-packages\flask_sqlalchemy\__init__.py", line 570, in get_options

self._sa.apply_driver_hacks(self._app, sa_url, options)

File "C:\Python38\Lib\site-packages\flask_sqlalchemy\__init__.py", line 883, in apply_driver_hacks

if sa_url.drivername.startswith('mysql'):

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

Do one thing set variables in windows environment variable and save. then close terminal close editor and restart them. It will work.

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

    export EMAIL_USER='youremail@gmail.com'
    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/

putting the following lines back into the bottom-most init file, fixed it for me:

app.config['SECRET_KEY'] = '24b85705923df...'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'