Last modified: Mar 25, 2025 By Alexander Williams
Install Redis-py in Python Step by Step
Redis-py is a Python client for Redis. It allows Python apps to interact with Redis databases. This guide will help you install it easily.
Table Of Contents
Prerequisites
Before installing Redis-py, ensure you have Python installed. You also need Redis server running locally or remotely.
Check Python version using python --version
. For Redis, install it from the official site if needed.
Step 1: Install Redis-py
Use pip to install Redis-py. Run the following command in your terminal.
pip install redis
This will download and install the latest Redis-py version. Wait for the process to complete.
Step 2: Verify Installation
After installation, verify it works. Open a Python shell and import Redis.
import redis
print(redis.__version__)
If no errors appear, Redis-py is installed correctly. You should see the version number.
Step 3: Connect to Redis Server
Create a connection to your Redis server. Use the Redis
class from the redis module.
r = redis.Redis(host='localhost', port=6379, db=0)
print(r.ping())
This code connects to a local Redis server. The ping
method checks if the server is responsive.
Step 4: Basic Redis Operations
Now, try basic Redis commands. Store and retrieve a key-value pair.
r.set('foo', 'bar')
print(r.get('foo'))
The output should be b'bar'
. The b
prefix indicates a bytes object in Python.
Handling Errors
If you face ModuleNotFoundError, Redis-py is not installed. Check our guide on how to solve ModuleNotFoundError.
Connection errors mean Redis server is not running. Start it before connecting.
Advanced Configuration
For remote servers, use the host IP and port. Always secure connections with passwords.
r = redis.Redis(host='your_ip', port=6379, password='your_pass')
Replace placeholders with your actual server details. Never expose passwords in code.
Conclusion
Installing Redis-py is simple with pip. Verify the installation and connect to Redis to start using it.
Now you can integrate Redis with Python for caching, messaging, and more. Explore Redis commands for advanced usage.