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

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)

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 extensions directory is where developers drop in their expanded functionality.

/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 AttributeError in a way that allows my app.register_blueprint call to succeed?

Martijn Pieters

You are trying to register the module and not the contained Blueprint object.

You'll need to introspect the module to find Blueprint instances instead:

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

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

From Java

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

From Java

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

From Java

tensorflow:AttributeError: 'module' object has no attribute 'mul'

From Dev

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

From Dev

urllib module error! AttributeError: 'module' object has no attribute 'request'

From Dev

AttributeError: '_AppCtxGlobals' object has no attribute 'user' in Flask

From Dev

python error "AttributeError: 'module' object has no attribute 'sha1'"

From Dev

Mtaplotlib AttributeError: 'module' object has no attribute 'pyplot'

From Dev

AttributeError: 'module' object has no attribute

From Dev

Flask AttributeError: 'NoneType' object has no attribute 'split'

From Dev

AttributeError: 'module' object has no attribute

From Dev

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

From Dev

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

From Dev

AttributeError: 'module' object has no attribute

From Dev

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

From Dev

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

From Dev

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

From Dev

Flask - AttributeError: 'module' object has no attribute 'items'

From Dev

travic ci error AttributeError: 'module' object has no attribute 'hashpw'

From Dev

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

From Dev

Registering route on blueprint raises AttributeError: 'function' object has no attribute 'route'

From Dev

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

From Dev

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

From Dev

Flask error: AttributeError: 'NoneType' object has no attribute 'startswith'

From Dev

Flask - AttributeError: '_AppCtxGlobals' object has no attribute 'db'

From Dev

Tensorflow import error AttributeError: 'module' object has no attribute 'Exporter'

From Dev

AttributeError("module 'name' has no attribute 'def name'",)

From Dev

Flask AttributeError: 'Blueprint' object has no attribute 'response_class'

Related Related

  1. 1

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

  2. 2

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

  3. 3

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

  4. 4

    tensorflow:AttributeError: 'module' object has no attribute 'mul'

  5. 5

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

  6. 6

    urllib module error! AttributeError: 'module' object has no attribute 'request'

  7. 7

    AttributeError: '_AppCtxGlobals' object has no attribute 'user' in Flask

  8. 8

    python error "AttributeError: 'module' object has no attribute 'sha1'"

  9. 9

    Mtaplotlib AttributeError: 'module' object has no attribute 'pyplot'

  10. 10

    AttributeError: 'module' object has no attribute

  11. 11

    Flask AttributeError: 'NoneType' object has no attribute 'split'

  12. 12

    AttributeError: 'module' object has no attribute

  13. 13

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

  14. 14

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

  15. 15

    AttributeError: 'module' object has no attribute

  16. 16

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

  17. 17

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

  18. 18

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

  19. 19

    Flask - AttributeError: 'module' object has no attribute 'items'

  20. 20

    travic ci error AttributeError: 'module' object has no attribute 'hashpw'

  21. 21

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

  22. 22

    Registering route on blueprint raises AttributeError: 'function' object has no attribute 'route'

  23. 23

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

  24. 24

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

  25. 25

    Flask error: AttributeError: 'NoneType' object has no attribute 'startswith'

  26. 26

    Flask - AttributeError: '_AppCtxGlobals' object has no attribute 'db'

  27. 27

    Tensorflow import error AttributeError: 'module' object has no attribute 'Exporter'

  28. 28

    AttributeError("module 'name' has no attribute 'def name'",)

  29. 29

    Flask AttributeError: 'Blueprint' object has no attribute 'response_class'

HotTag

Archive