importing issues between modules
<pre><code>|--------FlaskApp
|----------------FlaskApp
|----------------__init__.py
|----------------view.py
|----------------models.py
|----------------db_create.py
|-----------------------static
|-----------------------templates
|-----------------------venv
|-----------------------__init__.py
|----------------flaskapp.wsgi
</code></pre>

<p>this is my modules and folder layout i have a import problem in init i have <pre>db = sqlalchemy</pre> im trying to import <strong>db</strong> to <strong>views.py, models.py and db_create.py</strong> but im getting all kinds of importing errors because im not importing thr proper way now my question is if i want to import db to the modules i specified how would i do it without getting a error</p>

<p><strong>some of the code importing db</strong></p>

<p><strong>models.py</strong></p>

<pre><code>from FlaskApp import db

class UserInfo(db.Model):

__tablename_ = 'user_info'

id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(80), unique=True)
password = db.Column(db.String(100), nullable=False)
posts = relationship('UserPosts', backref='posts')

def __init__(self, username, email, password):
    self.username = username
    self.email = email
    self.password = password

def __repr__(self):
        return '{}-{}'.format(self.username, self.email)
</code></pre>

<p><strong>views.py</strong></p>

<pre><code>from FlaskApp import db

@app.route('/')
@login_required     
def home():
    user = db.session.query(UserInfo).all()
    return render_template('home.html', user=user)
</code></pre>

<p><strong>__init__.py</strong></p>

<pre><code>from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy(app)


tail /var/log/apache2/error.log
[Tue Oct 11 04:17:20.925573 2016] [wsgi:error] [pid 5049:tid 139870897813248] [client 174.58.31.189:55884] Traceback (most recent call last):, referer: http://localhost/
[Tue Oct 11 04:17:20.925618 2016] [wsgi:error] [pid 5049:tid 139870897813248] [client 174.58.31.189:55884]   File "/var/www/FlaskApp/flaskapp.wsgi", line 14, in &lt;module&gt;, referer: http://localhost/
[Tue Oct 11 04:17:20.925626 2016] [wsgi:error] [pid 5049:tid 139870897813248] [client 174.58.31.189:55884]     from FlaskApp import app as application, referer: http://localhost/
[Tue Oct 11 04:17:20.925638 2016] [wsgi:error] [pid 5049:tid 139870897813248] [client 174.58.31.189:55884]   File "/var/www/FlaskApp/FlaskApp/__init__.py", line 4, in &lt;module&gt;, referer: http://localhost/
[Tue Oct 11 04:17:20.925644 2016] [wsgi:error] [pid 5049:tid 139870897813248] [client 174.58.31.189:55884]     import FlaskApp.main, referer: http://localhost/
[Tue Oct 11 04:17:20.925653 2016] [wsgi:error] [pid 5049:tid 139870897813248] [client 174.58.31.189:55884]   File "/var/www/FlaskApp/FlaskApp/main.py", line 7, in &lt;module&gt;, referer: http://localhost/
[Tue Oct 11 04:17:20.925658 2016] [wsgi:error] [pid 5049:tid 139870897813248] [client 174.58.31.189:55884]     from models import UserInfo, referer: http://localhost/
[Tue Oct 11 04:17:20.925668 2016] [wsgi:error] [pid 5049:tid 139870897813248] [client 174.58.31.189:55884]   File "/var/www/FlaskApp/FlaskApp/models.py", line 2, in &lt;module&gt;, referer: http://localhost/
[Tue Oct 11 04:17:20.925673 2016] [wsgi:error] [pid 5049:tid 139870897813248] [client 174.58.31.189:55884]     from FlaskApp import db, referer: http://localhost/
[Tue Oct 11 04:17:20.925707 2016] [wsgi:error] [pid 5049:tid 139870897813248] [client 174.58.31.189:55884] ImportError: cannot import name 'db', referer: http://localhost/
</code></pre>

<p>my goal is not just to solve one error is to be able to import the right way from now on</p>

<p>This is actually the most painful moment for a Flask learner. Basically, your <code>FlaskApp</code> is dependent on <code>FlaskApp.main</code> then <code>FlaskApp.models</code> and then back to <code>FlaskApp</code> which create a perfect case of circular dependency.</p>

<p>Here's the way which I think is elegant and pythonic, by attaching the <code>db</code> variable to <code>app</code> in <code>__init__.py</code>:</p>

<pre><code>from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy(app)
app.db = db

with app.app_context():
    import FlaskApp.main
</code></pre>

<p>In <code>main.py</code>:</p>

<pre><code>from flask import current_app as app
from .models import UserInfo


@app.route('/')
@login_required     
def home():
    user = app.db.session.query(UserInfo).all()
    return render_template('home.html', user=user)
</code></pre>

<p>In <code>models.py</code>:</p>

<pre><code>from flask import current_app as app

db = app.db


class UserInfo(db.Model):

    __tablename_ = 'user_info'

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)
    email = db.Column(db.String(80), unique=True)
    password = db.Column(db.String(100), nullable=False)
    posts = relationship('UserPosts', backref='posts')

    def __init__(self, username, email, password):
        self.username = username
        self.email = email
        self.password = password

    def __repr__(self):
        return '{}-{}'.format(self.username, self.email)
</code></pre>

<p>Then you can use <code>app.db</code> or <code>current_app.db</code> inside of any functions/methods in your application and don't need to worry about the circular dependency.</p>

python flask flask-sqlalchemy python-3.5