title: Flask python app.py doesn't run but manage.py runserver does
body: <p>I have a flask app which I am trying to run. It seems when I run python app.py, there is a circular import of some sort. Here is the code for both my models.py and app.py:</p>
  
  <p><strong>models.py</strong></p>
  
  <pre><code>import datetime
  from app import bcrypt, db
  
  class User(BaseModel, db.Model):
      __tablename__ = "users"
  
      id = db.Column(db.Integer, primary_key=True, autoincrement=True)
      email = db.Column(db.String(255), unique=True, nullable=False)
      password = db.Column(db.String(255), nullable=False)
      registered_on = db.Column(db.DateTime, nullable=False)
      admin = db.Column(db.Boolean, nullable=False, default=False)
  
      def __init__(self, email, password, admin=False):
          self.email = email
          self.password = bcrypt.generate_password_hash(password)
          self.registered_on = datetime.datetime.now()
          self.admin = admin
  </code></pre>
  
  <p><strong>app.py</strong></p>
  
  <pre><code>from flask import Flask, render_template
  from flask import request, jsonify, session
  from flask.ext.bcrypt import Bcrypt
  
  from flask_sqlalchemy import SQLAlchemy
  from models import User
  
  app = Flask(__name__)
  db = SQLAlchemy()
  
  POSTGRES = {
      'user': 'postgres',
      'db': 'postgres',
      'host': 'localhost',
      'port': '5432',
  }
  app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://%(user)s@%(host)s:%(port)s/%(db)s' % POSTGRES
  app.config['SECRET_KEY'] = 'lol'
  
  bcrypt = Bcrypt(app)
  
  db.init_app(app)
  
  @app.route('/api/register', methods=['POST'])
  def register():
      json_data = request.json
      user = User(
          email=json_data['email'],
          password=json_data['password']
      )
      try:
          db.session.add(user)
          db.session.commit()
          status = 'success'
      except:
          status = 'this user is already registered'
      db.session.close()
      return jsonify({'result': status})
  </code></pre>
  
  <p><strong>manage.py</strong></p>
  
  <pre><code>from flask_script import Manager
  from flask_migrate import Migrate, MigrateCommand
  from app import app, db
  from models import User
  
  migrate = Migrate(app, db)
  manager = Manager(app)
  
  manager.add_command('db', MigrateCommand)
  
  @manager.command
  def create_admin():
      """Creates the admin user."""
      db.session.add(User(email='admin@admin.com', password='admin', admin=True))
      db.session.commit()
  
  if __name__ == '__main__':
      manager.run()
  </code></pre>
  
  <p>I have played around with the imports but nothing seems to work, I usually will get a circular import error like this:</p>
  
  <pre><code>Traceback (most recent call last):
    File "app.py", line 7, in &lt;module&gt;
      from models import User
    File "/Users/Rishub/Desktop/apps/topten/models.py", line 3, in &lt;module&gt;
      from app import bcrypt, db
    File "/Users/Rishub/Desktop/apps/topten/app.py", line 7, in &lt;module&gt;
      from models import User
  ImportError: cannot import name User
  </code></pre>
  
  <p>Does anyone know the solution to this? 
  In addition, if I run python manage.py runserver, my app runs fine so I am curious as to why this works but python app.py does not.</p>