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

NewGuy:

私の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)

これは私のプロジェクトのディレクトリレイアウトです。extensions開発者が拡張された機能でドロップする場所ディレクトリです。

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

問題は、私がこのエラーを受け取り、それが私に何を言っているのかわからないということです。

>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'

単純な拡張機能は次のようになります

from flask import Blueprint

extension1 = Blueprint('extension1', __name__)

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

通話を成功させるAttributeError方法でを解決するにはどうすればよいapp.register_blueprintですか?

Martijn Pieters:

含まれているオブジェクトではなく、モジュールを登録しようとしていBlueprintます。

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)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

AttributeError: 'module' object has no attribute

分類Dev

AttributeError: 'module' object (scipy) has no attribute *** Why does this error occur?

分類Dev

AttributeError: 'module' object has no attribute 'computation'

分類Dev

AttributeError: 'module' object has no attribute 'reader'

分類Dev

AttributeError: 'module' object(scipy)has no attribute 'misc'

分類Dev

AttributeError: 'module' object(scipy)has no attribute 'misc'

分類Dev

AttributeError: 'module' object has no attribute 'urlretrieve'

分類Dev

Python AttributeError: 'module' object has no attribute 'suite'

分類Dev

AttributeError: 'module' object has no attribute 'hist'

分類Dev

Spark SQL - Select yields AttributeError: 'module' object has no attribute 'api'

分類Dev

numpy.getbuffer causes AttributeError: 'module' object has no attribute 'getbuffer'

分類Dev

Container Engine: AttributeError: 'module' object has no attribute 'Credentials'

分類Dev

AttributeError: 'Model' object has no attribute '_name' during input layer concatenation

分類Dev

AttributeError: 'function' object has no attribute

分類Dev

AttributeError: 'bool' object has no attribute

分類Dev

'Flask' object has no attribute 'post' error for login unit test

分類Dev

'module' object has no attribute 'SortedDict' Django Tastypie error

分類Dev

AttributeError: 'datetime' module has no attribute 'strptime'

分類Dev

Dynamically changing a function __name__ throws AttributeError: 'method' object has no attribute '__name__'

分類Dev

Flask: model_form: AttributeError: type object 'Form' has no attribute 'query'

分類Dev

theanoのインポート:AttributeError: 'module' object has no attribute 'find_graphviz'

分類Dev

pythonエラー「AttributeError: 'module' object has no attribute 'sha1'」

分類Dev

AttributeError: module 'win32ctypes.pywin32.win32api' has no attribute 'error'

分類Dev

SQLAlchemy "AttributeError: 'str' object has no attribute 'c'"

分類Dev

TensorFlow:AttributeError: 'Tensor' object has no attribute 'shape'

分類Dev

Scrapy:AttributeError: 'list' object has no attribute 'iteritems'

分類Dev

AttributeError: 'list' object has no attribute 'split'-Python

分類Dev

AttributeError( "'str' object has no attribute 'read'")

分類Dev

AttributeError: 'DeferredAttribute' object has no attribute '_meta'

Related 関連記事

  1. 1

    AttributeError: 'module' object has no attribute

  2. 2

    AttributeError: 'module' object (scipy) has no attribute *** Why does this error occur?

  3. 3

    AttributeError: 'module' object has no attribute 'computation'

  4. 4

    AttributeError: 'module' object has no attribute 'reader'

  5. 5

    AttributeError: 'module' object(scipy)has no attribute 'misc'

  6. 6

    AttributeError: 'module' object(scipy)has no attribute 'misc'

  7. 7

    AttributeError: 'module' object has no attribute 'urlretrieve'

  8. 8

    Python AttributeError: 'module' object has no attribute 'suite'

  9. 9

    AttributeError: 'module' object has no attribute 'hist'

  10. 10

    Spark SQL - Select yields AttributeError: 'module' object has no attribute 'api'

  11. 11

    numpy.getbuffer causes AttributeError: 'module' object has no attribute 'getbuffer'

  12. 12

    Container Engine: AttributeError: 'module' object has no attribute 'Credentials'

  13. 13

    AttributeError: 'Model' object has no attribute '_name' during input layer concatenation

  14. 14

    AttributeError: 'function' object has no attribute

  15. 15

    AttributeError: 'bool' object has no attribute

  16. 16

    'Flask' object has no attribute 'post' error for login unit test

  17. 17

    'module' object has no attribute 'SortedDict' Django Tastypie error

  18. 18

    AttributeError: 'datetime' module has no attribute 'strptime'

  19. 19

    Dynamically changing a function __name__ throws AttributeError: 'method' object has no attribute '__name__'

  20. 20

    Flask: model_form: AttributeError: type object 'Form' has no attribute 'query'

  21. 21

    theanoのインポート:AttributeError: 'module' object has no attribute 'find_graphviz'

  22. 22

    pythonエラー「AttributeError: 'module' object has no attribute 'sha1'」

  23. 23

    AttributeError: module 'win32ctypes.pywin32.win32api' has no attribute 'error'

  24. 24

    SQLAlchemy "AttributeError: 'str' object has no attribute 'c'"

  25. 25

    TensorFlow:AttributeError: 'Tensor' object has no attribute 'shape'

  26. 26

    Scrapy:AttributeError: 'list' object has no attribute 'iteritems'

  27. 27

    AttributeError: 'list' object has no attribute 'split'-Python

  28. 28

    AttributeError( "'str' object has no attribute 'read'")

  29. 29

    AttributeError: 'DeferredAttribute' object has no attribute '_meta'

ホットタグ

アーカイブ