模块之间导入问题

时间:2016-10-11 04:07:59

标签: python flask flask-sqlalchemy python-3.5

|--------FlaskApp
|----------------FlaskApp
|----------------__init__.py
|----------------view.py
|----------------models.py
|----------------db_create.py
|-----------------------static
|-----------------------templates
|-----------------------venv
|-----------------------__init__.py
|----------------flaskapp.wsgi

这是我的模块和文件夹布局我在init中有导入问题

db = sqlalchemy
我试图将 db 导入 views.py,models。 py和db_create.py ,但我得到各种导入错误,因为我没有正确导入thr现在我的问题是,如果我想导入数据库我指定的模块我将如何做而不会出现错误

导入数据库的一些代码

models.py

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)

views.py

from FlaskApp import db

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

__初始化__。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/

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

2 个答案:

答案 0 :(得分:1)

对于Flask学习者来说,这实际上是最痛苦的时刻。基本上,您的FlaskApp依赖于FlaskApp.main然后FlaskApp.models,然后返回FlaskApp,这会创建一个完美的循环依赖案例。

通过将db变量附加到app中的__init__.py,我认为这是优雅和pythonic的方式:

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy(app)
app.db = db

with app.app_context():
    import FlaskApp.main

main.py

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 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.dbcurrent_app.db,而不必担心循环依赖。

答案 1 :(得分:0)

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

RuntimeError: Working outside of application context. This typically means that you attempted to use functionality that needed to interface with the current application object in a way.  To solve this set up an application context with app.app_context().  See the documentation for more information.
    tail -n 40 /var/log/apache2/error.log
[Wed Oct 12 00:20:02.315500 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784]     import FlaskApp.main
[Wed Oct 12 00:20:02.315510 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784]   File "/var/www/FlaskApp/FlaskApp/main.py", line 2, in <module>
[Wed Oct 12 00:20:02.315516 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784]     from .models import UserInfo
[Wed Oct 12 00:20:02.315526 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784]   File "/var/www/FlaskApp/FlaskApp/models.py", line 7, in <module>
[Wed Oct 12 00:20:02.315532 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784]     db = app.db
[Wed Oct 12 00:20:02.315542 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784]   File "/usr/local/lib/python3.5/dist-packages/werkzeug/local.py", line 343, in __getattr__
[Wed Oct 12 00:20:02.315547 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784]     return getattr(self._get_current_object(), name)
[Wed Oct 12 00:20:02.315557 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784]   File "/usr/local/lib/python3.5/dist-packages/werkzeug/local.py", line 302, in _get_current_object
[Wed Oct 12 00:20:02.315563 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784]     return self.__local()
[Wed Oct 12 00:20:02.315573 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784]   File "/usr/local/lib/python3.5/dist-packages/flask/globals.py", line 51, in _find_app
[Wed Oct 12 00:20:02.315579 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784]     raise RuntimeError(_app_ctx_err_msg)
[Wed Oct 12 00:20:02.315611 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784] RuntimeError: Working outside of application context.
[Wed Oct 12 00:20:02.315620 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784] 
[Wed Oct 12 00:20:02.315628 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784] This typically means that you attempted to use functionality that needed
[Wed Oct 12 00:20:02.315634 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784] to interface with the current application object in a way.  To solve
[Wed Oct 12 00:20:02.315641 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784] this set up an application context with app.app_context().  See the
[Wed Oct 12 00:20:02.315648 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784] documentation for more information.
[Wed Oct 12 00:20:02.813916 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] mod_wsgi (pid=19326): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module., referer: http://localhost/
[Wed Oct 12 00:20:02.814030 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] mod_wsgi (pid=19326): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'., referer: http://localhost/
[Wed Oct 12 00:20:02.814917 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] Traceback (most recent call last):, referer: http://localhost/
[Wed Oct 12 00:20:02.814972 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786]   File "/var/www/FlaskApp/flaskapp.wsgi", line 14, in <module>, referer: http://localhost/
[Wed Oct 12 00:20:02.814981 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786]     from FlaskApp import app as application, referer: http://localhost/
[Wed Oct 12 00:20:02.814993 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786]   File "/var/www/FlaskApp/FlaskApp/__init__.py", line 4, in <module>, referer: http://localhost/
[Wed Oct 12 00:20:02.814998 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786]     import FlaskApp.main, referer: http://localhost/
[Wed Oct 12 00:20:02.815008 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786]   File "/var/www/FlaskApp/FlaskApp/main.py", line 2, in <module>, referer: http://localhost/
[Wed Oct 12 00:20:02.815014 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786]     from .models import UserInfo, referer: http://localhost/
[Wed Oct 12 00:20:02.815024 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786]   File "/var/www/FlaskApp/FlaskApp/models.py", line 7, in <module>, referer: http://localhost/
[Wed Oct 12 00:20:02.815029 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786]     db = app.db, referer: http://localhost/
[Wed Oct 12 00:20:02.815040 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786]   File "/usr/local/lib/python3.5/dist-packages/werkzeug/local.py", line 343, in __getattr__, referer: http://localhost/
[Wed Oct 12 00:20:02.815046 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786]     return getattr(self._get_current_object(), name), referer: http://localhost/
[Wed Oct 12 00:20:02.815055 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786]   File "/usr/local/lib/python3.5/dist-packages/werkzeug/local.py", line 302, in _get_current_object, referer: http://localhost/
[Wed Oct 12 00:20:02.815061 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786]     return self.__local(), referer: http://localhost/
[Wed Oct 12 00:20:02.815071 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786]   File "/usr/local/lib/python3.5/dist-packages/flask/globals.py", line 51, in _find_app, referer: http://localhost/
[Wed Oct 12 00:20:02.815077 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786]     raise RuntimeError(_app_ctx_err_msg), referer: http://localhost/
[Wed Oct 12 00:20:02.815107 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] RuntimeError: Working outside of application context., referer: http://localhost/
[Wed Oct 12 00:20:02.815114 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] , referer: http://localhost/
[Wed Oct 12 00:20:02.815118 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] This typically means that you attempted to use functionality that needed, referer: http://localhost/
[Wed Oct 12 00:20:02.815123 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] to interface with the current application object in a way.  To solve, referer: http://localhost/
[Wed Oct 12 00:20:02.815139 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] this set up an application context with app.app_context().  See the, referer: http://localhost/
[Wed Oct 12 00:20:02.815143 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] documentation for more information., referer: http://localhost/