I'm trying to import database and get an error "cannot import name 'db'". I've seen answers on such problem, but none of them helped in my case. Tree of my project:
chat
| chat.py
└─ app
│ models.py
│ __init__.py
│
├─── main
│ events.py
│ forms.py
│ routes.py
│ __init__.py
chat.py:
from app import create_app, socketio
app = create_app(debug=True)
if __name__ == '__main__':
socketio.run(app)
app\init.py:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_socketio import SocketIO
socketio = SocketIO()
def create_app(debug=False):
app = Flask(__name__)
app.debug = debug
app.config['SECRET_KEY'] = 'gjr39dkjn344_!67#'
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql:///site.db'
db = SQLAlchemy(app)
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
socketio.init_app(app)
return app
app\main\init.py:
from flask import Blueprint
main = Blueprint('main', __name__)
from . import routes, events
app\models.py:
from app import db
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(15), nullable=False)
session_id = db.Column(db.String(200), nullable=False)
isready = db.Column(db.Boolean, default='False')
room = db.relationship('Room', backref='spy')
def __repr__(self):
return f"User('{self.username}')"
So, when I'm trying from app import db
in python terminal I get
ImportError: cannot import name 'db'
Since I'm new to flask (and web at all), I have no idea, what to do. I've tried this solution (kinda closest to my case) and all alike answers but it didn't work. What can I try?