Can't import LoginManager() in Flask Can't import LoginManager() in Flask flask flask

Can't import LoginManager() in Flask


You probably have a circular import. This is fine, but you need to take into account that you'll be working with modules that haven't yet completed all top-level instructions.

If you have code like:

from flask.ext.login import LoginManagerfrom app.admin import admin_blueprintlm = LoginManager()lm.init_app(app)lm.login_view = 'login'

then app.admin will be imported before the lm = LoginManager() line has executed; any code in app.admin that then tries to address app.lm will fail.

Move blueprint imports down your module and lm will have been created already:

from flask.ext.login import LoginManagerlm = LoginManager()lm.init_app(app)lm.login_view = 'login'from app.admin import admin_blueprint

See the Circular Imports note in the 'Larger Applications' documentation section of the Flask manual.