Python ';功能';对象没有属性';名称';注册蓝图时

Python ';功能';对象没有属性';名称';注册蓝图时,python,flask,Python,Flask,以下是我的项目布局: baseflask/ baseflask/ __init__.py views.py resources/ health.py/ wsgi.py/ 这是我的指纹 from flask import Blueprint from flask import Response health = Blueprint('health', __name__) @health.route("/health", meth

以下是我的项目布局:

baseflask/
   baseflask/
      __init__.py
      views.py
      resources/
          health.py/
   wsgi.py/
这是我的指纹

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
如何在
\uuuu init\uuuuuuy.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)
以下是错误:

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'
回溯(最近一次呼叫最后一次):
文件“/home/ubuntu/workspace/baseflask/wsgi.py”,第10行,在
从baseflask导入应用程序
文件“/home/ubuntu/workspace/baseflask/baseflask/_init__.py”,第18行,在
应用程序注册蓝图(健康)
文件“/usr/local/lib/python2.7/dist packages/flask/app.py”,第62行,在wrapper_func中
返回f(自,*args,**kwargs)
文件“/usr/local/lib/python2.7/dist-packages/flask/app.py”,第880行,在注册表中
如果self.blueprints中的blueprint.name:
AttributeError:“函数”对象没有属性“名称”

通过重新使用查看函数的名称,您屏蔽了引用
Blueprint
实例的
health
全局名称:

health = Blueprint('health', __name__)
@health.route("/health", methods=["GET"])
def health():
不能让路线视图功能和蓝图使用相同的名称;您替换了引用蓝图的全局名称
health
,并尝试为同一全局名称注册路由功能

为蓝图使用不同的名称:

health_blueprint = Blueprint('health', __name__)
并登记:

from baseflask.health import health_blueprint
app.register_blueprint(health_blueprint)
或者为视图函数使用不同的名称(此时端点名称也会更改,除非在
@health.route(…)
decorator中显式使用
endpoint='health'

蓝图名称与函数名称相同,请尝试重命名函数名称

health = Blueprint('health', __name__)
@health.route("/health", methods=["GET"])
def check_health():

请注意,您所犯的错误并非特定于Flask或其蓝图。但是,如果您认为文档需要改进,请务必通过项目的名称建设性地参与项目。在flask1.0中,如果这些名称相同,则没有问题。@gold kou:这是一个问题。在
health=Blueprint
之后,
def health
将名称
health
重新绑定到路线视图功能。您现在不能再使用
health
引用blueprint对象。这在任何版本的烧瓶中都是一个问题。
health = Blueprint('health', __name__)
@health.route("/health", methods=["GET"])
def check_health():