ImportError: No module named flask.ext.login

2019-03-17 20:07发布

I have a problem with flask_login module.

i have installed flask_login module successfully. Also from the command prompt i can run this script easily with no error:

Python 2.7 (r27:82525, Jul  4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from flask.ext.login import LoginManager

But when I am running this script:

from flask import Flask
from flask.ext.login import LoginManager
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World! Welcome"

if __name__ == "__main__":
    app.run()

i am getting the error :

ImportError: No module named flask.ext.login

What is the mistake i am doing. I am very new to this flask. Thanks in advance.

8条回答
叼着烟拽天下
2楼-- · 2019-03-17 20:41

This is an environment error -- whatever IDE you're using doesn't have the PYTHONPATH set properly. You can test this out by creating a small test file: test.py, with the following contents:

from os import environ
print environ.get('PYTHONPATH')

If the PYTHONPATH doesn't contain the directory that holds your Flask-Login package, then that's the issue.

查看更多
Ridiculous、
3楼-- · 2019-03-17 20:43

See For me the simple solution is to just remove .ext from flask.ext.login and instead just type flask_login. This works for python 3 and make sure that you have correctly installed your desired package.

查看更多
乱世女痞
4楼-- · 2019-03-17 20:49

The solution is simple. Different versions of the flask run with different codes so just change the code : flask.ext.login to the code: flask_login

It should run smoothly.

Note that this also applies to bootstrap. Actually inorder to find the solution I had the error while importing bootstrap.

查看更多
Animai°情兽
5楼-- · 2019-03-17 20:51

If you have already installed flask-login by

$pip install flask_login

Try replacing the

from flask.ext.login import LoginManager

with

from flask_login import LoginManager
查看更多
姐就是有狂的资本
6楼-- · 2019-03-17 20:51

Strange But it worked when I tried to run the script from Command prompt

C:\Users\dib\Desktop>python foo.py
 * Running on http://127.0.0.1:5000/
127.0.0.1 - - [11/Feb/2014 18:03:52] "GET / HTTP/1.1" 200 -

But when I am trying to run from the python default IDE pressing f5 it is not working.

查看更多
ら.Afraid
7楼-- · 2019-03-17 20:52

Initially I installed flask_login on virtual environment. I found out that its trying to find on the machine. So I installed it globally as;

mycomp# pip install -U flask_login

That solved the problem.

查看更多
登录 后发表回答