Python 在模块之间导入问题

Python 在模块之间导入问题,python,flask,flask-sqlalchemy,python-3.5,Python,Flask,Flask Sqlalchemy,Python 3.5,这是我的模块和文件夹布局,我在init中有一个导入问题 |--------FlaskApp |----------------FlaskApp |----------------__init__.py |----------------view.py |----------------models.py |----------------db_create.py |-----------------------static |-----------------------templates |-

这是我的模块和文件夹布局,我在init中有一个导入问题
|--------FlaskApp
|----------------FlaskApp
|----------------__init__.py
|----------------view.py
|----------------models.py
|----------------db_create.py
|-----------------------static
|-----------------------templates
|-----------------------venv
|-----------------------__init__.py
|----------------flaskapp.wsgi
视图.py

db = sqlalchemy
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)
from FlaskApp import db

@app.route('/')
@login_required     
def home():
    user = db.session.query(UserInfo).all()
    return render_template('home.html', user=user)
\uuuu init\uuuuu.py

db = sqlalchemy
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)
from FlaskApp import db

@app.route('/')
@login_required     
def home():
    user = db.session.query(UserInfo).all()
    return render_template('home.html', user=user)
从flask\u sqlalchemy导入sqlalchemy
db=SQLAlchemy(应用程序)
tail/var/log/apache2/error.log
[星期二10月11日04:17:20.925573 2016][wsgi:error][pid 5049:tid 139870897813248][client 174.58.31.189:55884]回溯(最近一次呼叫):参考:http://localhost/
[2016年10月11日星期二04:17:20.925618][wsgi:error][pid 5049:tid 139870897813248][client 174.58.31.189:55884]文件“/var/www/FlaskApp/FlaskApp.wsgi”,第14行,in,参考:http://localhost/
[周二10月11日04:17:20.925626 2016][wsgi:error][pid 5049:tid 139870897813248][client 174.58.31.189:55884]来自FlaskApp导入应用程序作为应用程序,请参考:http://localhost/
[10月11日星期二04:17:20.925638 2016][wsgi:error][pid 5049:tid 139870897813248][client 174.58.31.189:55884]文件“/var/www/FlaskApp/FlaskApp/_uuinit_uuuuuuuuuuuuuuuuuuuuuu.py”,第4行,in,参考:http://localhost/
[10月11日星期二04:17:20.925644 2016][wsgi:error][pid 5049:tid 139870897813248][client 174.58.31.189:55884]进口烧瓶app.main,参考:http://localhost/
[10月11日星期二04:17:20.925653 2016][wsgi:error][pid 5049:tid 139870897813248][client 174.58.31.189:55884]文件“/var/www/FlaskApp/FlaskApp/main.py”,第7行,in,参考:http://localhost/
[周二10月11日04:17:20.925658 2016][wsgi:error][pid 5049:tid 139870897813248][client 174.58.31.189:55884]从模型导入用户信息,参考:http://localhost/
[2016年10月11日星期二04:17:20.925668][wsgi:error][pid 5049:tid 139870897813248][client 174.58.31.189:55884]文件“/var/www/FlaskApp/FlaskApp/models.py”,第2行,in,参考:http://localhost/
[周二10月11日04:17:20.925673 2016][wsgi:error][pid 5049:tid 139870897813248][client 174.58.31.189:55884]来自FlaskApp导入数据库,参考:http://localhost/
[周二10月11日04:17:20.925707 2016][wsgi:error][pid 5049:tid 139870897813248][client 174.58.31.189:55884]导入错误:无法导入名称“db”,请参考:http://localhost/

我的目标不仅仅是解决一个错误,而是从现在起能够以正确的方式导入

这实际上是学习者最痛苦的时刻。基本上,您的
FlaskApp
依赖于
FlaskApp.main
然后是
FlaskApp.models
,然后返回到
FlaskApp
,这创造了循环依赖的完美案例

我认为这是一种优雅的python方式,它将
db
变量附加到
中的
app

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 <module>, 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 <module>, 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 <module>, 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 <module>, 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/
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy(app)
app.db = db

with app.app_context():
    import FlaskApp.main
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)
main.py
中:

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 <module>, 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 <module>, 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 <module>, 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 <module>, 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/
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy(app)
app.db = db

with app.app_context():
    import FlaskApp.main
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)
models.py中

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 <module>, 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 <module>, 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 <module>, 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 <module>, 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/
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy(app)
app.db = db

with app.app_context():
    import FlaskApp.main
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)

然后,您可以在应用程序中的任何函数/方法中使用
app.db
current\u app.db
,而无需担心循环依赖性。

在使用上述答案后得到了这一点

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)
运行时错误:在应用程序上下文之外工作。这通常意味着您试图以某种方式使用与当前应用程序对象接口所需的功能。要解决此问题,请使用app.app\u context()设置应用程序上下文。有关更多信息,请参阅文档。
tail-n40/var/log/apache2/error.log
[2016年10月12日星期三00:20:02.315500][wsgi:error][pid 19327:tid 139821662463744][client 174.58.31.189:51784]import FlaskApp.main
[2016年10月12日星期三00:20:02.315510][wsgi:error][pid 19327:tid 139821662463744][client 174.58.31.189:51784]文件“/var/www/FlaskApp/FlaskApp/main.py”,第2行,in
[Wed Oct 12 00:20:02.315516 2016][wsgi:error][pid 19327:tid 139821662463744][client 174.58.31.189:51784]来自。模型导入用户信息
[2016年10月12日星期三00:20:02.315526][wsgi:error][pid 19327:tid 139821662463744][client 174.58.31.189:51784]文件“/var/www/FlaskApp/FlaskApp/models.py”,第7行,in
[2016年10月12日星期三00:20:02.315532][wsgi:error][pid 19327:tid 139821662463744][client 174.58.31.189:51784]db=app.db
[2016年10月12日星期三00:20:02.315542][wsgi:error][pid 19327:tid 139821662463744][client 174.58.31.189:51784]文件“/usr/local/lib/python3.5/dist packages/werkzeug/local.py”,第343行,在__
[2016年10月12日星期三00:20:02.315547][wsgi:error][pid 19327:tid 139821662463744][client 174.58.31.189:51784]返回getattr(self.\u get\u current\u object(),name)
[2016年10月12日星期三00:20:02.315557][wsgi:error][pid 19327:tid 139821662463744][client 174.58.31.189:51784]文件“/usr/local/lib/python3.5/dist packages/werkzeug/local.py”,第302行,在“获取当前”对象中
[2016年10月12日星期三00:20:02.315563][wsgi:error][pid 19327:tid 139821662463744][client 174.58.31.189:51784]返回自我
[2016年10月12日星期三00:20:02.315573][wsgi:error][pid 19327:tid 139821662463744][client 174.58.31.189:51784]文件“/usr/local/lib/python3.5/dist packages/flask/globals.py”,第51行,在“查找”应用程序中
[Wed Oct 12 00:20:02.315579 2016][wsgi:error][pid 19327:tid 139821662463744][client 174.58.31.189:51784]引发运行时错误(_app_ctx_err_msg)
[Wed Oct 12 00:20:02.315611 2016][wsgi:error][pid 19327:tid 139821662463744][client 174.58.31.189:51784]运行时错误:在应用程序上下文之外工作。
[2016年10月12日星期三00:20:02.315620][wsgi:error][pid 19327:tid 139821662463744][client 174.58.31.189:51784]
[2016年10月12日星期三00:20:02.315628][wsgi:error][pid 19327:tid 139821662463744][client 174.58.31.189:51784]这通常意味着您试图使用所需的功能
[Wed Oct 12 00:20:02.315634 2016][wsgi:error][pid 19327:tid 139821662463744][client 174.58.31.189:51784]以某种方式与当前应用程序对象进行接口。解决
[2016年10月12日星期三00:20:02.315641][wsgi:error][pid 19327:tid 139821662463744][client 174.58.31.189:51784]这将使用app.app_context()设置应用程序上下文。见
[2016年10月12日星期三00:20:02.315648][wsgi:error][pid 19327:tid 139821662463744][client 174.58.31.189:51784]文档了解更多信息。
[2016年10月12日星期三00:20:02.813916][wsg