Issue while importing LoginManager from flask.ext.

2019-06-04 01:42发布

I am trying get hands-on with flask-login extension. I am using virtualenv for flask.

I could able to import LoginManager from flask.ext.login in the python interpreter window, but not in the script. Below the import calls in the script.

from flask.ext.login import LoginManager

I am getting below trace.

Traceback (most recent call last):
File "practice/flask_login.py", line 1, in <module>
from flask.ext.login import LoginManager
File "/Users/sunil/co_operative/flask/lib/python2.7/site-packages/flask/exthook.py", line 81, in load_module
reraise(exc_type, exc_value, tb.tb_next)  
File "/Users/sunil/co_operative/practice/flask_login.py", line 1, in <module>    
from flask.ext.login import LoginManager
ImportError: cannot import name LoginManager

2条回答
走好不送
2楼-- · 2019-06-04 01:53

Cause of problem :

Your file name is causing problem.

Explanation:

As mentioned in official flask document:

http://flask.pocoo.org/docs/0.10/extensiondev/, when user try to import flask extension using expression import flask.ext.something, flask will look for module flask_something.

So, import flask.ext.login look for module flask_login which is conflicting by your file name. And flask is trying to find Definition of LoginManager in your file.

Solution

File rename will solve problem.

查看更多
SAY GOODBYE
3楼-- · 2019-06-04 01:56

You have called your script flask_login.py. When you import from flask.ext.SOMETHING, behind the scenes it imports from flask_SOMETHING. You are hiding the real flask_login.py. Rename your script and you should be good to go.

查看更多
登录 后发表回答