flask - blueprint - sqlalchemy - cannot import name 'db' into moles file flask - blueprint - sqlalchemy - cannot import name 'db' into moles file flask flask

flask - blueprint - sqlalchemy - cannot import name 'db' into moles file


This is actually a simple, yet frustrating issue. The problem is you are importing main BEFORE you are creating the instance of db in your __init__.py

If move the import to after your db = SQLAlchemy(app), it will work:

from flask import Flaskfrom flask_sqlalchemy import SQLAlchemyapp = Flask(__name__)app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://uername:password@localhost/test'db = SQLAlchemy(app)from bookshelf.main.controllers import main #<--move this hereapp.register_blueprint(main, url_prefix='/')