Developers

Getting Started with Python and Redis

This is  a small tutorial to start using Redis with Python. We will look at the steps that tell you how to install Redis in your local ubuntu machine. As we are compiling from source this should also represent how to install in any linux distribution. Then, we can look at some basic commands in Redis and get a feel of the Redis commands. Finally, we will install the Redis-py module and see how we can interface with the Redis module from Python.

Before going through this tutorial, please go through our Redis infographic to get an overview of the Redis module and how to approach it. We also have a webinar on developing a Redis module.

I am using an ubuntu machine. Let us start with the installation of Redis.

sudo apt-get update

Install build essentials which have C and C++ compilers and other GNU C libraries which will help us in the installation later.

sudo apt-get install build-essential

Tcl is also needed to run Redis.

sudo apt-get install tcl8.5

Download the Redis source and untar it.

wget http://download.redis.io/releases/redis-stable.tar.gz
tar xzf redis-stable.tar.gz
cd redis-stable

We will need to now compile from source.

make 
make test
make install

Once this is done, there will be a set of scripts that will be created in the utils folder which can be used to install the service.

?  utils sudo ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server.


Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
 Adding system startup for /etc/init.d/redis_6379 ...
   /etc/rc0.d/K20redis_6379 -> ../init.d/redis_6379
   /etc/rc1.d/K20redis_6379 -> ../init.d/redis_6379
   /etc/rc6.d/K20redis_6379 -> ../init.d/redis_6379
   /etc/rc2.d/S20redis_6379 -> ../init.d/redis_6379
   /etc/rc3.d/S20redis_6379 -> ../init.d/redis_6379
   /etc/rc4.d/S20redis_6379 -> ../init.d/redis_6379
   /etc/rc5.d/S20redis_6379 -> ../init.d/redis_6379
Success!
Starting Redis server...
Installation successful!

To access Redis, we will need the redis-cli.

?  utils redis-cli
127.0.0.1:6379> exit

We can set and get key values using the “SET” and “GET” keywords.

127.0.0.1:6379> SET users:GeorgeWashington "lang: python, born:1990"
OK
127.0.0.1:6379> GET users:GeorgeWashington
"lang: python, born:1990"
127.0.0.1:6379> exit

Let’s now install the Python module and try to see if we can access the Redis server from Python.

Let’s test the Redis server with the Python.

?  redis_tutorial virtualenv venv -p python3.5
Running virtualenv with interpreter /usr/bin/python3.5
Using base prefix '/usr'
New python executable in venv/bin/python3.5
Also creating executable in venv/bin/python
Installing setuptools, pip...done.
?  redis_tutorial source venv/bin/activate
(venv)?  redis_tutorial
(venv)?  redis_tutorial
(venv)?  redis_tutorial pip install redis
Downloading/unpacking redis
  Downloading redis-2.10.5-py2.py3-none-any.whl (60kB): 60kB downloaded
Installing collected packages: redis
Successfully installed redis
Cleaning up...
(venv)?  redis_tutorial python
Python 3.5.2 (default, Jul 17 2016, 00:00:00)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import redis
>>> r = redis.StrictRedis()
>>> r.get("mykey")
>>> r.get("mykey")
>>> r.get("users:GeorgeWashington")
b'lang: python, born:1990'

This was a basic tutorial about using Python with Redis. Next, we will lay a frontend for making a twitter client using Flask as the middleware and Redis in the backend.

References:

agiliq, getting started with redis and python
digitalocean, how to install and use redis

Joydeep

Joydeep Bhattacharjee is a Category Head (Python) at HackerEarth. He likes to dabble in all things tech and is passionate about open source.

Share
Published by
Joydeep

Recent Posts

How To Conduct A Recruitment SWOT Analysis?

A SWOT analysis is a business strategy to assess the Strengths, Weaknesses, Opportunities and Threats…

3 weeks ago

How to Build a Recruitment Pipeline for Seasonal Hiring

Seasonal hiring can be a daunting task, whether it is peak accounting season for finance…

3 weeks ago

Best Practices for Writing Inclusive Job Descriptions

The hiring landscape has seen a paradigm shift in terms of diversity in people, talent,…

3 weeks ago

Benefits Of AI-Powered Job Descriptions

The introduction of AI in recruitment has revolutionized how hiring workflows are designed. It paved…

3 weeks ago

Benefits of Recruitment Process Outsourcing (RPO)

Today’s era has seen a steep increase in the use of technology in hiring and…

3 weeks ago

AI-Enhanced Job Matching: Finding the Perfect Fit

Today’s job landscape has become increasingly competitive for both job seekers and recruiters. One of…

3 weeks ago