title: How to avoid circular imports in a Flask app with Flask SQLAlchemy models?
body: <p>I'm getting into Flask and building out an app that uses Flask SQLAlchemy. I've created a basic API that works when all the code is in a single file, but would like to better organize it, like so:</p>
  
  <p><strong>app/models/user.py</strong></p>
  
  <pre><code>from datetime import datetime
  from app.app import db
  
  
  class User(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      public_id = db.Column(db.String, unique=True)
      admin = db.Column(db.Boolean)
      username = db.Column(db.String(50), unique=True)
      email = db.Column(db.String(50), unique=True)
      password = db.Column(db.String(100))
      subscription_plan = db.Column(db.Integer)
      created_at = db.Column(db.DateTime, index=True,
                             default=datetime.utcnow())
      updated_at = db.Column(db.DateTime, index=True, default=datetime.utcnow())
  </code></pre>
  
  <p><strong>app/app.py</strong></p>
  
  <pre><code>from flask import Flask, request, jsonify
  from flask_sqlalchemy import SQLAlchemy
  from werkzeug.security import generate_password_hash
  import uuid
  
  from app.models.user import User
  
  app = Flask(__name__)
  
  app.config['SQLALCHEMY_DATABASE_URI'] = #SQLALCHEMY_DATABASE_URI
  
  db = SQLAlchemy(app)
  
  # ... CRUD routes that will use User
  
  @app.route('/user', methods=['POST'])
  def create_user():
  data = request.get_json()
  hashed_password = generate_password_hash(data['password'])
  new_user = User(
      public_id=str(uuid.uuid4()),
      username=data['username'],
      password=hashed_password,
      email=data['email'],
      admin=data['admin'],
      subscription_plan=data['subscription_plan']
  )
  db.session.add(new_user)
  db.session.commit()
  return jsonify({'message': 'User successfully created.'})
  
  
  if __name__ == '__main__':
      app.run(port=5000, debug=True)
  </code></pre>
  
  <p>I would like to import <code>db</code> from <strong>app/app.py</strong> into my <strong>app/models/user.py</strong> file, but when I then try to import the <code>User</code> model into <strong>app/app.py</strong>, it gives me an error because of the circular import. I have no idea how to get around this because it seems like <code>User</code> needs <code>db</code> after it gets passed an instance of <code>app</code> from <strong>app/app.py</strong>. </p>
  
  <p>I'd also like to move my routes out of here and into separate files for better organization, so trying to understand the best way to avoid circular imports all around. Any and all help is greatly appreciated!</p>