title: How can I import app from __init__.py and why I'm getting this error?
body: <p>I want to import <code>db</code> from <code>__init__.py</code> int the <code>model.py</code> file but I can't import, an error appears that says:</p>
  
  <pre class="lang-py prettyprint-override"><code>Traceback (most recent call last):
    File "/home/rohit/Desktop/flask_app/run.py", line 1, in &lt;module&gt;
      from bytewar import create_app
    File "/home/rohit/Desktop/flask_app/bytewar/__init__.py", line 5, in &lt;module&gt;
      from bytewar.user.views import user_blueprint
    File "/home/rohit/Desktop/flask_app/bytewar/user/views.py", line 9, in &lt;module&gt;
      from bytewar.model import Login_form, Login, Post, Contacts, Signup_form, Contacts
    File "/home/rohit/Desktop/flask_app/bytewar/model.py", line 5, in &lt;module&gt;
      from bytewar import db
  ImportError: cannot import name 'db'
  </code></pre>
  
  <p><strong>My application tree:</strong></p>
  
  <pre><code>___ run.py     # &lt;&lt;------- ImportError in this file.
  ___ bytewar/
    |__ __init__.py
    |__ config.py
    |__ model.py       
    |__ user/
    |   |__ __init__.py
    |   |__ views.py
    |   |__ static/
    |   |    |__ img/
    |   |    |__ ....
    |   |__ template/
    |       |__ index.html
    |       |__ about.html
    |
    |__ admin/
        |__ __init__.py
        |__ views.py
        |__ template/
            |__ index.html
            |__ about.html
  
  </code></pre>
  
  <p><strong>__init__.py:</strong></p>
  
  <pre><code>from flask import Flask
  from flask_sqlalchemy import SQLAlchemy
  from flask_bootstrap import Bootstrap 
  from flask_login import LoginManager
  from bytewar.user.views import user_blueprint
  from bytewar.admin.views import admin_blueprint
  
  db = SQLAlchemy()
  
  def create_app():
      app = Flask('bytewar')
      app = Flask(__name__.split('.')[0])
      app.config.from_pyfile('config.py')
      app.config['DEBUG'] = True
  
      app.register_blueprint(user_blueprint)
      app.register_blueprint(admin_blueprint)
  
      login_manager = LoginManager()
      bootstrap = Bootstrap(app)
  
      db.init_app(app)
      login_manager.init_app(app)
      login_manager.session_protection = 'strong'
  
      return app
  </code></pre>
  
  <p>Here you can see I've <code>db.init_app(app)</code> but still I'm getting <code>ImportError</code>.</p>
  
  <p><strong>After adding <code>__init__.py</code> file inside the <code>bytewar.user/</code> and <code>bytewar.admin/</code>(beside <code>views.py</code>), I'm getting error in <code>run.py</code>:</strong></p>
  
  <pre><code>Traceback (most recent call last):
    File "/home/rohit/Desktop/flask_app/run.py", line 1, in &lt;module&gt;
      from bytewar import app
    File "/home/rohit/Desktop/flask_app/bytewar/__init__.py", line 5, in &lt;module&gt;
      from bytewar.user.views import user_blueprint
    File "/home/rohit/Desktop/flask_app/bytewar/user/views.py", line 9, in &lt;module&gt;
      from bytewar.model import Login_form, Login, Post, Contacts, Signup_form, Contacts
    File "/home/rohit/Desktop/flask_app/bytewar/model.py", line 5, in &lt;module&gt;
      from run import db
    File "/home/rohit/Desktop/flask_app/run.py", line 1, in &lt;module&gt;
      from bytewar import app
  ImportError: cannot import name 'app'
  </code></pre>
  
  <p><strong>run.py:</strong></p>
  
  <pre><code>from bytewar import app
  
  
  if __name__ == "__main__":
      app.run()
  </code></pre>
  
  <p><strong>My <code>bytewar/user/__init__.py</code> and as it is <code>bytewar/admin/__int__.py</code>:</strong></p>
  
  <pre><code>from flask import Blueprint
  
  user_blueprint = Blueprint(
      'user', __name__,
      static_folder='static',
      template_folder='template',
   )
  </code></pre>
  
  <p>Why is this happening?</p>