Full Stack Python logo Full Stack Python

全部主题 | Blog | 时讯 | @fullstackpython | Facebook | 源码 # How to Use Redis with Python 3 and redis-py on Ubuntu 16.04 Posted by Matt Makai on 五月 16, 2016. Last updated 八月 10, 2016.

Redis is an in-memory key-value pair NoSQL data store often used for web application sessions, transient data and as a broker for task queues. redis-py is a common Python code library for interacting with Redis. Let's learn how to get Redis up and running on Ubuntu and then start using it in a simple Python application.

Tools We Need

This tutorial is tested with Python 3.5 but either Python 2 or 3 should work for everything written here. Just make sure one version is installed on your system by going to the terminal and typing python --version. Other than Python itself, here is the software we are going to use throughout the rest of this post:

If you aren't sure how how to install pip and virtualenv, review the first few steps of the how to set up Python 3, Flask and Green Unicorn on Ubuntu 16.04 LTS guide.

Install Redis

There are a few ways to install Redis, such as downloading and compiling from source. However, on Ubuntu we can install a system package through apt. The advantage of this method is that the apt process will take care of installing redis-server as a system service. Open the terminal and run the following command:

  1. sudo apt-get install redis-server

Enter your sudo password and when you are prompted whether you want to install the new package enter 'yes'.

How to Use Redis with Python 3 and redis-py on Ubuntu 16.04 - 图2

After a few moments the downloading and processing should be complete and you will be back at the prompt.

How to Use Redis with Python 3 and redis-py on Ubuntu 16.04 - 图3

Redis is now installed and the Redis server is running in the background as a system service. Even though we installed the redis-server package, the installation also comes with the Redis command line client. The client is useful for connecting directly to the Redis server without any Python code. Give redis-cli a try by typing this into the command prompt:

  1. redis-cli

The Redis client connects to the localhost server and gives a new prompt to show it's ready for commands:

How to Use Redis with Python 3 and redis-py on Ubuntu 16.04 - 图4

Give the prompt a try by using Redis commands such as keys * or set a 1. The full list of Redis commands is provided in the project documentation.

Virtualenv and Install redis-py

We need to figure out our python3 location, then create a virtualenv, activate the virtualenv and then install redis-py with pip. Determine your python3 executable location with the which command.

  1. which python3

You'll see some output like the following screenshot.

How to Use Redis with Python 3 and redis-py on Ubuntu 16.04 - 图5

Create a new virtualenv either in your home directory or wherever you store your project virtualenvs. Specify the full path to your python3 installation.

  1. # specify the system python3 installation
  2. virtualenv --python=/usr/bin/python3 venvs/redistest

Activate the virtualenv.

  1. source ~/venvs/redistest/bin/activate

Next we can install the redis-py Python package from PyPI using the pip command.

  1. pip install redis

How to Use Redis with Python 3 and redis-py on Ubuntu 16.04 - 图6

Alright, now it is installed in our virtualenv. Let's write some simple Python code to try out give redis-py!

Working with Redis from Python

Fire up the Python REPL with the python or python3 command. You can also write the following code in a Python file such as "testredis.py" then execute it with python testredis.py.

  1. import redis
  2. # create a connection to the localhost Redis server instance, by
  3. # default it runs on port 6379
  4. redis_db = redis.StrictRedis(host="localhost", port=6379, db=0)
  5. # see what keys are in Redis
  6. redis_db.keys()
  7. # output for keys() should be an empty list "[]"
  8. redis_db.set('full stack', 'python')
  9. # output should be "True"
  10. redis_db.keys()
  11. # now we have one key so the output will be "[b'full stack']"
  12. redis_db.get('full stack')
  13. # output is "b'python'", the key and value still exist in Redis
  14. redis_db.incr('twilio')
  15. # output is "1", we just incremented even though the key did not
  16. # previously exist
  17. redis_db.get('twilio')
  18. # output is "b'1'" again, since we just obtained the value from
  19. # the existing key
  20. redis_db.delete('twilio')
  21. # output is "1" because the command was successful
  22. redis_db.get('twilio')
  23. # nothing is returned because the key and value no longer exist

That is a quick introduction to some commonly-used Redis commands invoked by their Python bindings through the redis-py library. Take a look at the redis-py official documentation to learn more about the extensive command list you can use to create, read, modify and delete keys and values in Redis.

Questions? Tweet @fullstackpython or post a message on the Full Stack Python Facebook page. Something wrong with this post? Fork this page's source on GitHub.


#### 在这里注册以便每月能收到一份邮件资料,内容包含本站的主要更新、教程和 Python 书籍的打折码等。

Learn more about these concepts

Redis and Ubuntu logos. Copyright their respective owners. Operating Systems Ubuntu NoSQL …or view all topics.


This site is based on Matt Makai's project Full Stack Python, thanks for his excellent work!

此网站由 @haiiiiiyun开源爱好者们 共同维护。 若发现错误或想贡献,请访问: Github fullstackpython.cn 项目