Install the Low-level Python Client for Elasticsearch

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

  • Python is a high-level general-purpose programming language that you’ll frequently need to use in Elasticsearch. Fortunately, Elastic maintains its own official git repository for the low-level Python client. The pip3 commands discussed below will be the same, regardless of the operating system you’re running. This tutorial will provide step-by-step instructions on how to install a low-level Python client in Elasticsearch.

Prerequisites

  • Python must already be installed on your system. This article assumes you’re using Python 3, as Python 2 will be depreciated by 2020. Check your version of Python with the following command:
    1
    python3 --version

Install the pip3 package manager

  • Install pip3 if you haven’t done so yet. You can also upgrade it with the following command:
1
pip3 install --upgrade pip
  • Consult the documention for more information on installing PIP.

Python 3 on Windows

Python 3 on Linux

  • Install Python with the apt package manager if you installed Python 3 on a Debian-based version of Linux and can’t find pip3. The following command shows how to do this:
1
sudo apt install python3-pip
  • Use the yum package manager to install Python if you’re running on a Red Hat distribution Linux and can’t find pip3. This command shows how :
1
sudo yum install python3 python3-wheel

Install Python Elasticsearch

Python 3 and MacOS

  • Python 2 is the default version of Python on macOS. Use HomeBrew to install Python 3 on a macOS system as follows:
1
brew install python3

Install the Elasticsearch low-level client for Python

  • Install the Python client Elasticsearch once Python 3 and PIP for Python 3 are installed and running.

Install the Client

  • This command uses pip3to install the Elasticsearch client :
1
pip3 install elasticsearch

Client Compatibility

According to the Elasticsearch github repository, the major client version number of the Python client should match the major client version number of Elasticsearch. For example, use version 5.x of the Python client if your version of Elasticsearch is 5.x. The following commands show how to check the versions of Elasticsearch and Python:

1
2
3
4
5
# get Elasticsearch version
curl -XGET "http://localhost:9200/"

# get Python client version
pip3 freeze | grep elasticsearch

Install Python Elasticsearch

Import the Elasticsearch library

  • Python has a built-in console interpreter you can access if it’s installed on your system. Type python3 and press Return to enter this interpreter.

  • Once you’re in the interpreter, enter the following command to ensure that the Elasticsearch client is properly installed:

1
import elasticsearch
  • The client is installed unless Python raises an ImportError after you enter the above command.

  • Create a new instance of the Elasticsearch client to ensure the Python library is working properly by using the following command:

1
es = elasticsearch.Elasticsearch()

Import Elasticsearch into Python Interpreter

  • Press CTRL + Z to exit the Python interpreter.

Conclusion

This tutorial showed how to install a low level Python client for Elasticsearch. It also included instructions for installing pip3, which the Python client requires. The specific steps for this procedure depend on the operating system, which include Windows, macOS and various types of Linux.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.