title: Access db from a separate file flask SQLAlchemy python3
body: <p>I am writing a flask application. I have two files. main.py and databases.py. I want to create a database from the database.py file. The main.py should access the databases.py file and create the database and table named "Users". But it shows import error. Help me with this issue</p>
  
  <p>main.py</p>
  
  <pre><code>from flask import Flask
  from flask_sqlalchemy import SQLAlchemy
  from databases import User
  
  app = Flask(__name__)
  app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/data_log.db'
  db = SQLAlchemy(app)
  
  if __name__ == '__main__':
      db.create_all()
      app.run(host='0.0.0.0', port=5001)  
  </code></pre>
  
  <p>databases.py</p>
  
  <pre><code>from main import db
  from passlib.apps import custom_app_context as pwd_context
  
  class User(db.Model) :
      __tablename__ = 'users'
      user_id     =   db.Column(db.Integer, primary_key = True)
      username    =   db.Column(db.String(32), index = True)
      password    =   db.Column(db.String(128))
      def hash_password(self, password) :
          self.password =pwd_context.hash(password)
      def verify_password(self, password) :
          return pwd_context.verify(password, self.password)
  </code></pre>
  
  <p>Traceback:</p>
  
  <pre><code>Traceback (most recent call last):
    File "main.py", line 3, in &lt;module&gt;
      from databases import User
    File "/home/paulsteven/stack/databases.py", line 1, in &lt;module&gt;
      from main import db
    File "/home/paulsteven/stack/main.py", line 3, in &lt;module&gt;
      from databases import User
  ImportError: cannot import name 'User'
  </code></pre>