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

Hackerearth Subscribe

Get advanced recruiting insights delivered every month

Hire top tech talent with our recruitment platform

Access Free Demo

Related reads

How Candidates Use Technology to Cheat in Online Technical Assessments
How Candidates Use Technology to Cheat in Online Technical Assessments

How Candidates Use Technology to Cheat in Online Technical Assessments

Article Summary How online assessments have transformed hiring Current state of cheating in online technical assessments Popular techniques candidates use to cheat Steps…

Talent Acquisition Strategies For Rehiring Former Employees
Talent Acquisition Strategies For Rehiring Former Employees

Talent Acquisition Strategies For Rehiring Former Employees

Former employees who return to work with the same organisation are essential assets. In talent acquisition, such employees are also termed as ‘Boomerang…

Automation in Talent Acquisition: A Comprehensive Guide
Automation in Talent Acquisition: A Comprehensive Guide

Automation in Talent Acquisition: A Comprehensive Guide

Automation has become a major element in the modern-day hiring process. The automated hiring process gained momentum since the advent of remote work…

Predictive Analytics for Talent Management
Predictive Analytics for Talent Management

Predictive Analytics for Talent Management

The job landscape in today’s age is highly competitive for both job seekers and hiring managers. Finding the right talent under such conditions…

How To Create A Positive Virtual Onboarding Experience?
How To Create A Positive Virtual Onboarding Experience?

How To Create A Positive Virtual Onboarding Experience?

The advent of the pandemic changed the hiring industry in many ways. One of the biggest outcomes of this global phenomenon was that…

How To Conduct A Recruitment SWOT Analysis?
How To Conduct A Recruitment SWOT Analysis?

How To Conduct A Recruitment SWOT Analysis?

A SWOT analysis is a business strategy to assess the Strengths, Weaknesses, Opportunities and Threats of a system. The exercise helps teams evaluate…

Hackerearth Subscribe

Get advanced recruiting insights delivered every month

View More

Top Products

Hackathons

Engage global developers through innovation

Hackerearth Hackathons Learn more

Assessments

AI-driven advanced coding assessments

Hackerearth Assessments Learn more

FaceCode

Real-time code editor for effective coding interviews

Hackerearth FaceCode Learn more

L & D

Tailored learning paths for continuous assessments

Hackerearth Learning and Development Learn more