Flask: AttributeError: 'function' object has no attribute 'name'

Created on 25 Jan 2015  路  5Comments  路  Source: pallets/flask

when i register a blueprint in my flask app,an error occurred,the error msg is :
`

             /usr/bin/python3.4.2 /data/AppCode/05CodeNet/MicroStone_flask/runServer.py
             Traceback (most recent call last):
             File "/data/AppCode/05CodeNet/MicroStone_flask/runServer.py", line 11, in <module>
             app.register_blueprint(login)
             File "/home/zhgk/.local/lib/python3.4/site-packages/flask/app.py", line 62, in wrapper_func
             return f(self, *args, **kwargs)
             File "/home/zhgk/.local/lib/python3.4/site-packages/flask/app.py", line 880,
             in register_blueprint
            if blueprint.name in self.blueprints:
            AttributeError: 'function' object has no attribute 'name'

`

and my py code is :

runserver.py:
`

       # -*- coding: utf-8 -*-
        """
             Flask搴旂敤鍚姩鍏ュ彛
       """

        from flask import Flask

        from apps.login.ctrl.login import login

        app = Flask(__name__)
        app.register_blueprint(login)
        if __name__ == "__main__":
               app.run()
        __author__ = 'zhgk'

`

login.py :

`

          # -*- coding: utf-8 -*-
               """

            """

            from flask.blueprints import Blueprint

             login = Blueprint('login', __name__)


             @login.route("/")
            def login():
                 return "Login"


              __author__ = 'zhgk'

`

anyone can tell me why?

Most helpful comment

yes , i have find the reson,the function name is the same with buleprint name,thaks very much,

At the time of use github editor, code has the wrong line, but in the I IDE, its format is correct

All 5 comments

Why is your code such a mess indentation-wise? Also, why do you have empty docstrings e.g. in login.py?

Anyway, you are overwriting the login blueprint with the login function. Use different names and the error goes away. I'd suggest login_blueprint or just blueprint for the blueprint.

yes , i have find the reson,the function name is the same with buleprint name,thaks very much,

At the time of use github editor, code has the wrong line, but in the I IDE, its format is correct

Ah and FYI, the issue tracker is not the place for support questions. Use e.g. the IRC channel or the mailing list for that.

~I changed flask version back to 0.10.1 and it worked.~

@miguelHx "downgrade to an ancient unsupported version" is really bad advice. The issue, which was clearly already resolved, was that they overwrote the blueprint with a function with the same name. The solution was to not shadow names, which is just a Python thing in general, not specific to Flask.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

KeyC0de picture KeyC0de  路  4Comments

lnielsen picture lnielsen  路  3Comments

tomjaguarpaw picture tomjaguarpaw  路  3Comments

sungjinp11 picture sungjinp11  路  3Comments

jab picture jab  路  4Comments