Unlock skill-first hiring with HackerEarth today

Learn more
piller_image

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

Related reads

The Impact of Talent Assessments on Reducing Employee Turnover
The Impact of Talent Assessments on Reducing Employee Turnover

The Impact of Talent Assessments on Reducing Employee Turnover

Organizations of all industries struggle with employee turnover. The high turnover rates cause increased hiring costs, lost productivity, and broken team dynamics. That’s…

Virtual Recruitment Events: A Complete Guide
Virtual Recruitment Events: A Complete Guide

Virtual Recruitment Events: A Complete Guide

Virtual hiring events are becoming vital for modern recruitment, and the hiring world is changing rapidly. As businesses embrace remote-first cultures and global…

The Role of Recruitment KPIs in Optimizing Your Talent Strategy
The Role of Recruitment KPIs in Optimizing Your Talent Strategy

The Role of Recruitment KPIs in Optimizing Your Talent Strategy

The competition for talent today is intense, and this makes it very important for organizations to get the right people on board. However,…

Interview as a Service – Optimizing Tech Hiring for Efficient Recruitment
Interview as a Service – Optimizing Tech Hiring for Efficient Recruitment

Interview as a Service – Optimizing Tech Hiring for Efficient Recruitment

Hiring trends are continuously evolving over the ages to keep pace with the latest technological advances. Hiring processes are being optimized almost every…

HR Scorecards: Using Metrics to Improve Hiring and Workforce Management
HR Scorecards: Using Metrics to Improve Hiring and Workforce Management

HR Scorecards: Using Metrics to Improve Hiring and Workforce Management

Hiring practices have changed significantly over the past 30 years. Technological advancements and changing workforce demographics have driven hirers to strike the right…

Why Recruiting Analytics is Critical for Hiring Success in 2024
Why Recruiting Analytics is Critical for Hiring Success in 2024

Why Recruiting Analytics is Critical for Hiring Success in 2024

In the current world, where the hiring process is ever-evolving, it has become crucial to make the right hiring decisions based on certain…

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