[英]How do I import my error_handler module into my app.py in flask?
I separated my flask errorhandlers into a separate module called 'error_handlers.py' within my 'app.py' directory.我将我的烧瓶错误处理程序分离到我的“app.py”目录中一个名为“error_handlers.py”的单独模块中。 I have tried importing the error_handlers module into the app module but i keep getting an ImportError:我曾尝试将 error_handlers 模块导入应用程序模块,但我不断收到 ImportError:
from app import app ImportError: cannot import name 'app' from partially initialized module 'app' (most likely due to a circular import)
I looked up a similar question and added blueprint but i'm still getting the same error.我查了一个类似的问题并添加了蓝图,但我仍然遇到同样的错误。 Please is there anything I'm doing wrong.请问有什么我做错了。 My code below:我的代码如下:
app.py应用程序
import os
from error_handler import *
from flask import Flask, flash, jsonify, redirect, render_template, request, session, abort
from flask_session import Session
from tempfile import mkdtemp
from helpers import lookup, login_required
# Configure application
app = Flask(__name__)
app.register_blueprint(error_handlers.blueprint)
...
error_handlers.py错误处理程序.py
from app import app
from flask import render_template, Blueprint
blueprint = Blueprint('error_handlers', __name__)
@blueprint.app_errorhandler(404)
def page_not_found(e):
return render_template("errors/404.html")
Hey you are getting Circular Import Error
so change the name of your file or the function name must be different from the file name.嘿,您遇到Circular Import Error
因此请更改文件名或函数名必须与文件名不同。
By this way, the compiler won't get confuse what the function to call.通过这种方式,编译器不会混淆要调用的函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.