Last modified: Jun 01, 2025 By Alexander Williams
How to Install Flask-Login in Python
Flask-Login is a popular extension for Flask. It simplifies user authentication. This guide will help you install it easily.
Table Of Contents
Prerequisites
Before installing Flask-Login, ensure you have Python installed. You also need Flask. If not, install Flask first.
pip install flask
Check your Python version. Flask-Login works with Python 3.6+. Verify with:
python --version
Install Flask-Login
Use pip to install Flask-Login. Run this command in your terminal:
pip install flask-login
This will download and install Flask-Login. It also installs dependencies automatically.
Verify Installation
Check if Flask-Login installed correctly. Open Python shell and import it:
import flask_login
print(flask_login.__version__)
If no errors appear, installation was successful. You should see the version number.
Basic Flask-Login Setup
Now, let's set up a basic Flask app with Flask-Login. First, create a new Python file.
from flask import Flask
from flask_login import LoginManager
app = Flask(__name__)
app.secret_key = 'your_secret_key'
login_manager = LoginManager()
login_manager.init_app(app)
if __name__ == '__main__':
app.run()
This code initializes Flask-Login. The secret_key
is essential for session security.
User Model Setup
Flask-Login requires a user model. Create a simple user class with required methods.
from flask_login import UserMixin
class User(UserMixin):
def __init__(self, id):
self.id = id
The UserMixin
provides default implementations for required methods.
User Loader Function
Flask-Login needs a user loader. This function loads a user by their ID.
@login_manager.user_loader
def load_user(user_id):
return User(user_id)
This callback is used to reload the user object from the user ID.
Login and Logout Routes
Now, add login and logout routes to your Flask app.
from flask import redirect, url_for
from flask_login import login_user, logout_user
@app.route('/login')
def login():
user = User(1)
login_user(user)
return redirect(url_for('protected'))
@app.route('/logout')
def logout():
logout_user()
return 'Logged out'
The login_user
and logout_user
functions handle authentication.
Protected Route
Create a protected route that requires authentication.
from flask_login import login_required
@app.route('/protected')
@login_required
def protected():
return 'Protected page'
The login_required
decorator ensures only logged-in users can access this route.
Testing the Application
Run your Flask application and test the routes.
flask run
Visit /login first, then try accessing /protected. You should see the protected content.
Common Issues
If you get errors, check these common issues:
1. Missing secret_key in Flask app
2. Forgot to initialize LoginManager
3. User model doesn't implement required methods
Conclusion
Flask-Login makes user authentication simple in Flask. Now you can secure your web applications easily.
For more Python packages, check our guides on Install Babel for Python Internationalization or How to Install pytz in Python Easily.