To use Redis with Python, you need a Python Redis client. The following sections demonstrate the use of redis-py, a Redis Python Client. Additional Python clients for Redis can be found under the Python section of the Redis Clients page.

Install redis-py

See redis-py’s README file for installation instructions.

Use pip to install redis-py:

pip install redis

You can also download the latest redis-py release from the GitHub repository. To install it, extract the source and run the following commands:

$ cd redis-py
$ python install

Connect to Redis

The following code creates a connection to Redis using redis-py:

import redis

r = redis.Redis(

To adapt this example to your code, replace the following values with your database’s values:

  • In line 4, set host to your database’s hostname or IP address
  • In line 5, set port to your database’s port
  • In line 6, set password to your database’s password

Example code for Redis commands

Once connected to Redis, you can read and write data with Redis command functions.

The following code snippet assigns the value bar to the Redis key foo, reads it back, and prints it:

# open a connection to Redis
r.set('foo', 'bar')
value = r.get('foo')

Example output:

$ python

Connection pooling

The redis-py client pools connections by default. No special actions are required to use it.


The redis-py client natively supports SSL.

Use the SSLConnection class or instantiate your connection pool using a rediss:// URL and the from_url method, like so:

r = redis.Redis.from_url( url='rediss://:[email protected]:port/0',