简体   繁体   English

如何将我的 error_handler 模块导入烧瓶中的 app.py?

[英]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.

相关问题 如何在我的烧瓶 app.py 中导入 jupyter notebook 文件? - How to import jupyter notebook file in my flask app.py? 我可以为我的 Flask 应用程序工厂使用 app.py 而不是 __init__.py 吗? - Can I use app.py instead of __init__.py for my Flask application factory? 访问作为 html 中我的 flask app.py 文件名的变量时出错 - Getting an error while accessing a variable that is the filename from my flask app.py in the html 为什么我的 app.py Flask 文件中的 .env 变量为 None? - Why am I getting .env variables as None in my app.py Flask file? 如何将 socketio 对象从 Flask 的 app.py 传递到 Python 中的视图模块? - How can I pass the socketio object from Flask's app.py to a views module in Python? 如何在不传递db对象的情况下将Flask模型从app.py中分离出来? - How do I split Flask models out of app.py without passing db object all over? %%writefile app.py 在 vscode 上不起作用。 我如何制作 app.py 文件? - %%writefile app.py not working on vscode. how do i make an app.py file? 如何将 Flask 应用程序导入 Pytest 测试? - How do I import my Flask app into my Pytest tests? 如何将模型从models.py导入app.py? - How to import models from models.py into app.py? Python 我无法在 flask 中打开文件“app.py” - Python I can't open file 'app.py' in flask
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM