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

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


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)


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)


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