Python 使用烧瓶蓝图解决此循环导入错误的正确方法是什么?
我在循环导入时遇到问题,所以我将blueprint导入移到应用程序定义下方。但是,我仍然有一个导入错误Python 使用烧瓶蓝图解决此循环导入错误的正确方法是什么?,python,flask,Python,Flask,我在循环导入时遇到问题,所以我将blueprint导入移到应用程序定义下方。但是,我仍然有一个导入错误 Traceback (most recent call last): File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 2217, in <module> globals = debugger.run(setup['file'], None, None) File "
Traceback (most recent call last):
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 2217, in <module>
globals = debugger.run(setup['file'], None, None)
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1643, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Users/benjamin/Documents/Projects/website/server/app/app.py", line 15, in <module>
from views import site
File "/Users/benjamin/Documents/Projects/website/server/app/views.py", line 2, in <module>
from models import User
File "/Users/benjamin/Documents/Projects/website/server/app/models.py", line 3, in <module>
from database_setup import db
File "/Users/benjamin/Documents/Projects/website/server/app/database_setup.py", line 1, in <module>
from app import app
File "/Users/benjamin/Documents/Projects/website/server/app/app.py", line 15, in <module>
from views import site
ImportError: cannot import name site
这是解决问题的正确方法,还是有其他解决方案
原始
app.py
无\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
views.py
:
from flask import Blueprint, render_template
site = Blueprint('site', __name__, template_folder='templates', static_folder='static')
@site.route('/', methods=['GET', 'POST'])
def index():
return render_template('index.html')
from app import app
from flask_mongoengine import MongoEngine
app.config['MONGODB_SETTINGS'] = {'db': 'mst_website'}
db = MongoEngine(app)
from database_setup import db
class User(db.Document):
# ...
数据库\u setup.py
:
from flask import Blueprint, render_template
site = Blueprint('site', __name__, template_folder='templates', static_folder='static')
@site.route('/', methods=['GET', 'POST'])
def index():
return render_template('index.html')
from app import app
from flask_mongoengine import MongoEngine
app.config['MONGODB_SETTINGS'] = {'db': 'mst_website'}
db = MongoEngine(app)
from database_setup import db
class User(db.Document):
# ...
models.py
:
from flask import Blueprint, render_template
site = Blueprint('site', __name__, template_folder='templates', static_folder='static')
@site.route('/', methods=['GET', 'POST'])
def index():
return render_template('index.html')
from app import app
from flask_mongoengine import MongoEngine
app.config['MONGODB_SETTINGS'] = {'db': 'mst_website'}
db = MongoEngine(app)
from database_setup import db
class User(db.Document):
# ...
我的文件结构是:
/server
|-- requirements.txt
|-- env/ (virtual environment)
|-- app/ (my main app folder)
|-- static/
|-- templates/
|-- __init__.py
|-- app.py
|-- database_setup.py
|-- models.py
|-- views.py
您的代码中有一个循环导入。基于回溯:
app.py
是否从视图导入站点
views.py
是否从模型导入用户
models.py
是否从数据库导入数据库
database\u setup.py
从应用程序导入应用程序
app.py
是否从视图导入站点
app.py
并不是导致您出现问题的原因。目前,app
在导入views
之前还没有定义,因此当它在链的下游尝试获取app
时,它还不可用
您需要重新构造项目,以便在定义app
后导入依赖于app
的所有内容。从你的问题来看,似乎你认为你做到了,但也许上面写的app
中还有一个导入内容你错过了
可能不相关,但您当前正在使用“相对”导入,这是不鼓励的。与从视图导入站点等执行
操作不同,您应该执行绝对路径:从app.views导入站点
,或相对路径:从.views导入站点
要回答“使用\uuuu main\uuuu
导入蓝图是一个好主意吗?”这不是一个好主意。问题在于,只有在直接运行模块时才会执行\uuuuu main\uuuu
保护。当您使用真正的应用程序服务器(如uWSGI或Gunicorn)部署此应用程序时,将不会导入或注册任何蓝图。通过将导入和调用放置在if中,并调用register\u blueprint
,如果您运行应用程序而不直接执行文件(例如,使用uWSGI),则不会注册蓝图@dirn这不是我在这里所做的吗?我将导入放在中,如果
?