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

后端 未结 1 1662
余生分开走
余生分开走 2020-12-08 17:00

I\'m new in bluprint, and have problem with importing db into mydatabase.py file which is models file.

I\'ve faced with this error:

ImportErro

相关标签:
1条回答
  • 2020-12-08 17:26

    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 Flask
    
    from flask_sqlalchemy import SQLAlchemy
    
    app = Flask(__name__)
    app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://uername:password@localhost/test'
    
    db = SQLAlchemy(app)
    
    from bookshelf.main.controllers import main #<--move this here
    
    app.register_blueprint(main, url_prefix='/')
    
    0 讨论(0)
提交回复
热议问题