Skip to main content

Connect with Python

This example connects to Redis®* service from Python, making use of the redis-py library.


These are the placeholders you will need to replace in the code sample:

REDIS_URIURL for the Redis connection, from the service overview page


Install the redis-py library:

pip install redis


Create a new file named, add the following content and replace the placeholder with the Redis URI:

import redis

def main():
redis_uri = 'REDIS_URI'
redis_client = redis.from_url(redis_uri)

redis_client.set('key', 'hello world')
key = redis_client.get('key').decode('utf-8')

print('The value of key is:', key)

if __name__ == '__main__':

This code creates a key named key with the value hello world and no expiration time. Then, it gets the key back from Redis and prints its value.

Run the code:


Note that on some systems you will need to use python3 to get Python3 rather than the previous Python2

If the script runs successfully, the outputs should be:

The value of key is: hello world