redis-py
Official Python client for Redis. Install is 'redis', import is 'redis'. Current version: 7.4.0 (Mar 2026). aioredis was merged into redis-py 4.2+ — use 'redis.asyncio' for async, not separate aioredis package. By default all responses are bytes — set decode_responses=True for strings. StrictRedis renamed to Redis in v3 but alias still works.
Warnings
- gotcha decode_responses defaults to False. All values returned as bytes (b'value') not strings. Silently breaks string comparisons, JSON parsing, and any code expecting str.
- breaking aioredis separate package is abandoned (last release 2021). 'import aioredis' still installs but is dead. Use 'import redis.asyncio as aioredis' from redis >= 4.2.
- gotcha SSL connections require 'rediss://' (double s) URL scheme, not 'redis://'. Using wrong scheme silently connects without TLS or raises ConnectionError.
- gotcha Upstash Redis requires TLS — always use rediss:// with Upstash URLs. Using redis:// with Upstash raises ConnectionError.
- gotcha StrictRedis renamed to Redis in v3. StrictRedis still works as an alias but generates confusion in docs. Use Redis directly.
- gotcha Connection pool is shared by default. Do not call r.close() in web request handlers — it closes the pool. Use aclose() in async or let the pool manage connections.
- gotcha ZADD argument order changed in v3. Old: zadd(name, member, score). New: zadd(name, {member: score}). Old order raises TypeError silently or produces wrong results.
Install
-
pip install redis -
pip install 'redis[hiredis]'
Imports
- Redis (sync)
import redis r = redis.Redis( host='localhost', port=6379, db=0, decode_responses=True # returns str not bytes ) r.set('key', 'value') print(r.get('key')) # 'value' not b'value' - redis.asyncio (async)
import redis.asyncio as aioredis import asyncio async def main(): r = aioredis.Redis( host='localhost', port=6379, decode_responses=True ) await r.set('key', 'value') print(await r.get('key')) await r.aclose() asyncio.run(main()) - from_url
import redis # Standard Redis URL r = redis.from_url('redis://localhost:6379/0', decode_responses=True) # Redis with password r = redis.from_url('redis://:password@localhost:6379/0') # SSL/TLS (Upstash, Redis Cloud) r = redis.from_url('rediss://user:pass@host:6380/0')
Quickstart
# pip install redis
import redis
r = redis.Redis(
host='localhost',
port=6379,
db=0,
decode_responses=True # str not bytes
)
# Basic operations
r.set('name', 'Alice')
print(r.get('name')) # 'Alice'
# Expiry
r.setex('session', 3600, 'token123') # TTL 1 hour
# Hash
r.hset('user:1', mapping={'name': 'Alice', 'age': '30'})
print(r.hgetall('user:1')) # {'name': 'Alice', 'age': '30'}
# List
r.lpush('queue', 'task1', 'task2')
print(r.lrange('queue', 0, -1))
r.close()