single |
# redis-py
The Python interface to the Redis key-value store.
[CI]
[docs]
[MIT licensed]
[pypi]
[codecov]
[Total alerts]
[Installation] | [Contributing] | [Getting Started] | [Connecting To
Redis]
---------------------------------------------
## Installation
redis-py requires a running Redis server. See [Redis's
quickstart] for installation
instructions.
redis-py can be installed using pip similar to other
Python packages. Do not use sudo with pip.
It is usually good to work in a
[virtualenv] or
[venv] to avoid conflicts
with other package managers and Python projects. For a quick
introduction see [Python Virtual Environments in Five
Minutes].
To install redis-py, simply:
``` bash
$ pip install redis
```
or from source:
``` bash
$ python setup.py install
```
View the current documentation [here].
## Contributing
Want to contribute a feature, bug fix, or report an issue? Check out
our [guide to
contributing].
## Getting Started
redis-py supports Python 3.6+.
``` pycon
>>> import redis
>>> r = redis.Redis(host='localhost', port=6379, db=0)
>>> r.set('foo', 'bar')
True
>>> r.get('foo')
b'bar'
```
By default, all responses are returned as bytes in Python
3.
If **all** string responses from a client should be decoded, the user
can specify *decode_responses=True* in
```Redis.__init__```. In this case, any Redis command that
returns a string type will be decoded with the encoding
specified.
The default encoding is utf-8, but this can be customized by specifiying
the
encoding argument for the redis.Redis class.
The encoding will be used to automatically encode any
strings passed to commands, such as key names and values.
--------------------
### MSET, MSETNX and ZADD
These commands all accept a mapping of key/value pairs. In redis-py 2.X
this mapping could be specified as **args* or as `**kwargs`. Both of
these styles caused issues when Redis introduced optional flags to ZADD.
Relying on `*args` caused issues with the optional argument order,
especially in Python 2.7. Relying on `**kwargs` caused potential
collision issues of user keys with the argument names in the method
signature.
To resolve this, redis-py 3.0 has changed these three commands to all
accept a single positional argument named mapping that is expected to be
a dict. For MSET and MSETNX, the dict is a mapping of key-names -\>
values. For ZADD, the dict is a mapping of element-names -\> score.
MSET, MSETNX and ZADD now look like:
``` pycon
def mset(self, mapping):
|