简体   繁体   中英

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. I have tried importing the error_handlers module into the app module but i keep getting an 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

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.
By this way, the compiler won't get confuse what the function to call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM