{"categories":["docs","develop","stack","oss","rs","rc","oss","kubernetes","clients"],"description":"Connect your Python application to a Redis database","duplicateOf":"head:data-ai-metadata","location":"body","title":"redis-py guide (Python)","tableOfContents":{"sections":[{"id":"install","title":"Install"},{"id":"connect-and-test","title":"Connect and test"},{"id":"more-information","title":"More information"}]},"codeExamples":[]}
Connect your Python application to a Redis database
redis-py is the Python client for Redis.
The sections below explain how to install redis-py and connect your application
to a Redis database.
redis-py requires a running Redis server. See here for Redis Open Source installation instructions.
You can also access Redis with an object-mapping client interface. See
RedisOM for Python
for more information.
Install
To install redis-py, enter:
pip install redis
For faster performance, install Redis with hiredis support. This provides a compiled response parser, and for most cases requires zero code changes. By default, if hiredis >= 1.0 is available, redis-py attempts to use it for response parsing.
Note:
The Python distutils packaging scheme is no longer part of Python 3.12 and greater. If you're having difficulties getting redis-py installed in a Python 3.12 environment, consider updating to a recent release of redis-py.
pip install redis[hiredis]
Connect and test
Connect to localhost on port 6379, set a value in Redis, and retrieve it. All responses are returned as bytes in Python. To receive decoded strings, set decode_responses=True. For more connection options, see these examples.