penguin
penguin

Reputation: 846

AttributeError: 'NoneType' object has no attribute 'drivername' for Flask Web App

I am currently doing the Python Flask Tutorial by Corey Schafer. In the blueprint video, I'm having trouble with exporting the variables. I have already written to ~/.bash_profile and ~/.profile but it wasn't successful.

In my .profile file:

export SECRET_KEY='5791628bb0b13ce0c676dfde280ba245'
export SQLALCHEMY_DATABASE_URI='sqlite:///sitetest.db'
export EMAIL_USER='email'
export EMAIL_PASS='password'

In my config.py file:

import os

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

In my __init__.py file

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 flaskblog.config import Config

app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
login_manager= LoginManager(app)
login_manager.login_view = 'users.login'
login_manager.login_message_category = 'info'

mail = Mail(app)

from flaskblog.users.routes import users
from flaskblog.posts.routes import posts
from flaskblog.main.routes import main

app.register_blueprint(users)
app.register_blueprint(posts)
app.register_blueprint(main)

I have already figured out it is more than likely the variables are not being exported. Any suggestions?

Traceback (most recent call last):
  File "/home/michael/anaconda3/lib/python3.7/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/michael/anaconda3/lib/python3.7/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/home/michael/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/michael/anaconda3/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/michael/anaconda3/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/michael/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/michael/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/michael/anaconda3/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/michael/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/michael/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/michael/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 "/home/michael/anaconda3/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 519, in __get__
    return type.query_class(mapper, session=self.sa.session())
  File "/home/michael/anaconda3/lib/python3.7/site-packages/sqlalchemy/orm/scoping.py", line 78, in __call__
    return self.registry()
  File "/home/michael/anaconda3/lib/python3.7/site-packages/sqlalchemy/util/_collections.py", line 1012, in __call__
    return self.registry.setdefault(key, self.createfunc())
  File "/home/michael/anaconda3/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 3206, in __call__
    return self.class_(**local_kw)
  File "/home/michael/anaconda3/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 138, in __init__
    bind = options.pop('bind', None) or db.engine
  File "/home/michael/anaconda3/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 937, in engine
    return self.get_engine()
  File "/home/michael/anaconda3/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 956, in get_engine
    return connector.get_engine()
  File "/home/michael/anaconda3/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 560, in get_engine
    options = self.get_options(sa_url, echo)
  File "/home/michael/anaconda3/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 575, in get_options
    self._sa.apply_driver_hacks(self._app, sa_url, options)
  File "/home/michael/anaconda3/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 877, in apply_driver_hacks
    if sa_url.drivername.startswith('mysql'):
AttributeError: 'NoneType' object has no attribute 'drivername'

Upvotes: 1

Views: 2277

Answers (3)

Aditi
Aditi

Reputation: 367

Are you using a virtual environment for the flask project? Below solution is here just for you.

So when you are using a virtual environment, it works in its own world with its different perks of installed packages. Same works for the environment variable as well. When you are adding your environment variables in the bash_profile and run the project (using virtual environment), it simply do not find the variables. Solution is..

Create an own variable file (say, var_file; as shown below) inside your virtual environment path. Refer below command in the terminal for more clear explanation.

> vim ~/.virtualenvs/your_project_virtual_environment_name/bin/var_file

here the shell file opens up

# add your environment variables
export abc='xyz'
export xyz='wtf' 

now exit the file (esc + : + wq!)


> source ~/.virtualenvs/your_project_virtual_environment_name/bin/new_shell_file

Voila..!! Try running the project now. It should work smooth like honey. Let me know if it works. :)

Upvotes: 2

Jimmy Le
Jimmy Le

Reputation: 1

I'm going through the same tutorial and wrestled a bit with this as well.

I tried adding assert statements to see which environment variable was returning None. Both the SECRET_KEY and DATABASE_URI were giving me trouble. I tried changing the variable names and even removing the quotes.

I'm not exactly quite sure which change solved the issue, but I was able to get the environment variables to work by restarting my terminal.

I hope this helps others in the future.

Upvotes: 0

abdusco
abdusco

Reputation: 11111

You need to set a proper DSN for SQLALCHEMY_DATABASE_URI environment variable. It needs to have a valid dialect prefix, like mysql, postgresql, or sqlite, as per docs:

https://docs.sqlalchemy.org/en/13/core/engines.html#database-urls

sa_url.drivername.startswith('mysql'):

This line in the stack trace implies that the DSN you supply is most likely missing a driver name.

Make sure that os.environ.get('SQLALCHEMY_DATABASE_URI') returns a valid DSN.

Upvotes: 1

Related Questions