AttributeError:“'NoneType'对象在<function _remove =”“at =”中没有属性'path'“0x10c49a668 =”“>忽略</function>

时间:2014-06-05 16:27:58

标签: python flask blueprint

我正在尝试在flask环境中实现我的coursera python项目。此外,我正在使用https://github.com/miguelgrinberg/flasky(分支7a)来了解蓝图的工作原理。现在,我定义了2个蓝图:main_blueprint&amp; rpsls_blueprint。运行应用程序后出现以下错误:

Traceback (most recent call last):
  File "manage.py", line 8, in <module>
    app = create_app(os.getenv('FLASK_CONFIG') or 'default')
  File "..../app/__init__.py", line 29, in create_app
    app.register_blueprint(rpsls_blueprint)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 62, in wrapper_func
    return f(self, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 880, in register_blueprint
    if blueprint.name in self.blueprints:
AttributeError: 'module' object has no attribute 'name'
Exception AttributeError: "'NoneType' object has no attribute 'path'" in <function _remove at 0x10c49a668> ignored

有人知道问题出在哪里吗? 以下是我的app/__init__.py文件的相关部分:

def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    bootstrap.init_app(app)
    mail.init_app(app)
    moment.init_app(app)
    db.init_app(app)

    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint)

    from .rpsls import rpsls as rpsls_blueprint
    app.register_blueprint(rpsls_blueprint)

    return app

这是我的app / rpsls / rpsls.py文件:

import random

class RpslsGame():
    def __init__(self):
        pass

    def name_to_number(self, name):
        if name == "rock":
            return 0
        elif name == "Spock":
            return 1
        elif name == "paper":
            return 2
        elif name == "lizard":
            return 3
        elif name == "scissors":
            return 4
        else:
            return name + " does not match any\
                            of the five correct input strings"

    def number_to_name(self, number):
        if number == 0:
            return "rock"
        elif number == 1:
            return "Spock"
        elif number == 2:
            return "paper"
        elif number == 3:
            return "lizard"
        elif number == 4:
            return "scissors"
        else:
            return str(number) + \
                "  is not in the correct range"

    def rpslsMethod(self, player_choice):
        result = ""
        result += "Player chooses " + str(player_choice) + "\n"
        player_number = self.name_to_number(player_choice)
        comp_number = random.randrange(0, 5)
        comp_choice = self.number_to_name(comp_number)
        result += "Computer chooses " + comp_choice + "\n"
        differene = (comp_number - player_number) % 5
        if (differene == 1 or differene == 2):
            result += "Computer wins\n"
        elif (differene == 3 or differene == 4):
            result += "Player wins\n"
        elif (differene == 0):
            result += "Try again, It's a tie\n"

        return result

我的app / rpsls / init .py文件:

from flask import Blueprint
rpsls = Blueprint('rpsls', __name__)
from . import views

1 个答案:

答案 0 :(得分:2)

您从rpls包中导入了rpls模块。这是模块,而不是蓝图对象。

您无法将模块注册为蓝图;你只能注册flask.Blueprint() instances。您可能想了解Flask Blueprints work的方式。

rpls包中有rpls个对象,是子模块。导入app.rpls.rpls模块后,替换 rpls Blueprint文件中的__init__.py Blueprint个实例。命名空间不是分开的。

重命名其中一个;模块或{{1}}对象。