Here is my project layout:
baseflask/ baseflask/ __init__.py views.py resources/ health.py/ wsgi.py/
Here is my print
from flask import Blueprint from flask import Response health = Blueprint('health', __name__) @health.route("/health", methods=["GET"]) def health(): jd = {'status': 'OK'} data = json.dumps(jd) resp = Response(data, status=200, mimetype='application/json') return resp
How I register in __init__.py
:
import os basedir = os.path.abspath(os.path.dirname(__file__)) from flask import Blueprint from flask import Flask from flask_cors import CORS, cross_origin app = Flask(__name__) app.debug = True CORS(app) from baseflask.health import health app.register_blueprint(health)
Here is the error:
Traceback (most recent call last): File "/home/ubuntu/workspace/baseflask/wsgi.py", line 10, in <module> from baseflask import app File "/home/ubuntu/workspace/baseflask/baseflask/__init__.py", line 18, in <module> app.register_blueprint(health) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 62, in wrapper_func return f(self, *args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 880, in register_blueprint if blueprint.name in self.blueprints: AttributeError: 'function' object has no attribute 'name'
You masked the health
global name referring to Blueprint
instance, by re-using the name for the view function:
health = Blueprint('health', __name__) @health.route("/health", methods=["GET"]) def health():
You can't have both the route view function and the blueprint use the same name; you replaced the global name health
that referred to the blueprint and are trying to register the route function for the same global name.
Use a different name for the blueprint:
health_blueprint = Blueprint('health', __name__)
and register that:
from baseflask.health import health_blueprint app.register_blueprint(health_blueprint)
or use a different name for the view function (at which point the endpoint name changes too unless you explicitly use endpoint='health'
in the @health.route(...)
decorator).
health = Blueprint('health', __name__) @health.route("/health", methods=["GET"]) def health():
Your blueprint name is same with your function name, try to rename the function name instead.
health = Blueprint('health', __name__) @health.route("/health", methods=["GET"]) def check_health():
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With