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

6

34

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?

Gallaway answered 24/10, 2014 at 14:38 Comment(0)
P
41

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)
Placebo answered 24/10, 2014 at 14:41 Comment(4)
I'm getting a very similar error, except I'm just trying to import a very basic blueprint without any of that fancyness.Bucket
@DavidCrook: it basically means you are trying to register something that is not an actual Blueprint instance. Make sure you pass in the right object.Placebo
I am new to Blueprints and I am getting the same error. Could this error arise because I am using the exactly the same name for both blueprint and folder where blueprint is located? Also where do I put above-mentioned piece of code to pass the blueprint object? I have run.py file that imports import app as app, and then in lower level folder init.py where I import all blueprints in the following way: from app.i_statement import income_statement, and register all blueprints import lines of code as such: app.register_blueprint(i_statement).Vitamin
@user3151858: income_statement itself is not a blueprint object. I don't know what kind of object it is so I can't really help more there.Placebo
M
10

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)
Magdala answered 2/7, 2019 at 19:9 Comment(1)
Thanks for this. I was registering the module but not the blueprint object inside the module.Cyclothymia
G
5

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.

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
Grishilda answered 31/8, 2019 at 22:18 Comment(0)
V
2

I got an error saying AttributeError: 'Blueprint' object has no attribute 'register_blueprint'

For this I simply uninstalled Flask using pip uninstall flask and then installed flask[async] pip install flask[async]

Vassal answered 21/10, 2021 at 7:20 Comment(0)
M
0

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.

Method answered 25/11, 2019 at 9:40 Comment(0)
B
0

Changing the flask version will help. I was using Flask==1.1.2 and changing to Flask==2.0.2 resolves the error.
pip install Flask==2.0.2

Bronwyn answered 24/4, 2022 at 5:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.