Python 烧瓶蓝图属性错误:“模块”对象没有属性“名称”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26550180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:38:37  来源:igfitidea点击:

Flask Blueprint AttributeError: 'module' object has no attribute 'name' error

pythonattributeerrorflask

提问by NewGuy

My API is being built to allow developers to extend it's functionality. My plan is to do this by providing an "extensions" directory where they can drop in Blueprints and they will be dynamically loaded. This is the code I am utilizing to import (modifed from this tutorial)

我的 API 正在构建以允许开发人员扩展其功能。我的计划是通过提供一个“扩展”目录来实现这一点,它们可以放入蓝图中并动态加载。这是我用来导入的代码(从本教程修改)

from flask import Flask

import pkgutil
import sys

app = Flask(__name__)

EXTENSIONS_DIR = "extensions"
modules = pkgutil.iter_modules(path=[EXTENSIONS_DIR])
for loader, mod_name, ispkg in modules: 
    if mod_name not in sys.modules:
        # It imports fine
        loaded_mod = __import__(EXTENSIONS_DIR+"."+mod_name+"."+mod_name, fromlist=[mod_name])
        # It does not register
        app.register_blueprint(loaded_mod)

This is the directory layout of my project. The extensionsdirectory is where developers drop in their expanded functionality.

这是我项目的目录布局。该extensions目录是开发人员放置扩展功能的地方。

/root
    /extensions
        /extension1
            __init__.py
            extension1.py
        /extension2
            __init__.py
            extension2.py
    simple_example.py

The problem is that I get this error and am not sure what it is telling me.

问题是我收到此错误,但不确定它在告诉我什么。

>python simple_example.py
Traceback (most recent call last):
  File "simple_example.py", line 14, in <module>
    app.register_blueprint(loaded_mod)
  File "C:\Python27\lib\site-packages\flask\app.py", line 62, in wrapper_func
    return f(self, *args, **kwargs)
  File "C:\Python27\lib\site-packages\flask\app.py", line 880, in register_blueprint
    if blueprint.name in self.blueprints:
AttributeError: 'module' object has no attribute 'name'

A simple extension looks like this

一个简单的扩展看起来像这样

from flask import Blueprint

extension1 = Blueprint('extension1', __name__)

@extension1.route("/my_route")
def treasure_list():
    return "list of objects"

How do I solve the AttributeErrorin a way that allows my app.register_blueprintcall to succeed?

我如何AttributeError以一种让我的app.register_blueprint呼叫成功的方式解决问题?

采纳答案by Martijn Pieters

You are trying to register the moduleand not the contained Blueprintobject.

您正在尝试注册模块而不是包含的Blueprint对象。

You'll need to introspect the module to find Blueprintinstances instead:

您需要内省模块以查找Blueprint实例:

if mod_name not in sys.modules:
    loaded_mod = __import__(EXTENSIONS_DIR+"."+mod_name+"."+mod_name, fromlist=[mod_name])
    for obj in vars(loaded_mod).values():
        if isinstance(obj, Blueprint):
            app.register_blueprint(obj)

回答by Kit

When I got this error my code looked like this:

当我收到此错误时,我的代码如下所示:

from blueprints import api
...
app.register_blueprint(api)

I fixed this by doing this:

我通过这样做解决了这个问题:

app.register_blueprint(api.blueprint)

回答by rummidge

I have also experienced the same effect in a project. The origin of the problem was an incorrect import of the blueprint file.

我在一个项目中也经历过同样的效果。问题的根源是蓝图文件的错误导入。

Make sure the import statement imports the real blueprint and not the module on which it is defined.

确保 import 语句导入真正的蓝图,而不是定义它的模块。

In other words, you may be doing

换句话说,你可能正在做

from .blueprint import blueprint

while you meant

而你的意思是

from .blueprint.blueprint import blueprint

As a side recommendation, name the module on which the blueprint is defined with a different name than the blueprint itself, in order to clarify the import. An example:

作为附带建议,将定义蓝图的模块命名为与蓝图本身不同的名称,以阐明导入。一个例子:

from .blueprint.views import blueprint

回答by Owais Kazi

I was importing blueprint directly from flask import Flask. I resolved this error by installing flask again using pip3 install flask. Sometimes, if flask version is greater than or equal to 0.12.3 you might face this issue.

我是直接导入蓝图from flask import Flask。我通过使用pip3 install flask. 有时,如果flask 版本大于或等于0.12.3,您可能会遇到此问题。