简体   繁体   English

如何解决 ImportError: cannot import name 'LoginManager' from 'flask_login' in Flask?

[英]How to solve ImportError: cannot import name 'LoginManager' from 'flask_login' in Flask?

I am trying a tutorial from https://www.digitalocean.com/community/tutorials/how-to-add-authentication-to-your-app-with-flask-login .我正在尝试https://www.digitalocean.com/community/tutorials/how-to-add-authentication-to-your-app-with-flask-login的教程。

...
from flask_login import LoginManager

def create_app():
    ...
    db.init_app(app)

    login_manager = LoginManager()
    login_manager.login_view = 'auth.login'
    login_manager.init_app(app)

    from .models import User

    @login_manager.user_loader
    def load_user(user_id):
        # since the user_id is just the primary key of our user table, use it in the query for the user
        return User.query.get(int(user_id))   

However, when I am trying to do from flask_login import LoginManager I received the error ImportError: cannot import name 'LoginManager' from 'flask_login'但是,当我尝试from flask_login import LoginManager执行时,我收到错误ImportError: cannot import name 'LoginManager' from 'flask_login'

Edit: I have installed flask_login through pip3 both inside and outside of my virtual environment.编辑:我已经通过 pip3 在我的虚拟环境内部和外部安装了 flask_login。 However, I am unable to use anything from flask_login.但是,我无法使用 flask_login 中的任何内容。 I have tried 'from flask_login import UserMixin' as well and a similar error is produced.我也尝试过'from flask_login import UserMixin',并产生了类似的错误。 I have also checked if flask_login is being installed by doing 'pip3 list' and Flask-Login is found in this list too.我还通过执行“pip3 list”检查了是否正在安装flask_login,并且在此列表中也找到了Flask-Login。

May I ask how can I solve this problem?请问我该如何解决这个问题? Thank you in advance!先感谢您!

Solved: I made a stupid mistake of not realizing the error is pointing to files in another directory.已解决:我犯了一个愚蠢的错误,没有意识到错误是指向另一个目录中的文件。

It seems that either you haven't installed flask-login if that is the case then install flask-login by pip install flask-login .如果是这种情况,您似乎还没有安装flask-login ,然后通过pip install flask-login flask-login

If you have installed, then do confirm that you have activated your virtual environment.如果您已安装,请确认您已激活虚拟环境。

I had this exact error, got solved by:我有这个确切的错误,通过以下方式解决:

1: 1:

from flask_login.login_manager import LoginManager
  1. Then I reactivated the virtual env:然后我重新激活了虚拟环境:
deactivate && source venv/bin/activate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM