Build A Stand-Alone Executable Elasticsearch Application Using Python

Introduction

This article will explain how to build a stand-alone Elasticsearch query application using Python.

The original Python code used for the app can be found in this ObjectRocket article explaining how to query Elasticsearch documents in a terminal window.

Prerequisites

curl -GET "localhost:9200" -v

Screenshot of a terminal window making a cURL request to the Elasticsearch cluster

Building an executable Python script with PyInstaller

Installing PyInstaller and building a Python application on Windows

It’s recommended that you use or switch to Python 3 instead of Python 2.7 since Python 2 is now deprecated and scheduled to lose support by 2020.

Type cmd in the Windows Search bar, or type run and then type cmd in the modal window to open a new instance of Command Prompt to get started.

Install the PyInstaller package for Python 3 on Windows

Depending on your installations of Python you’ll need to use the pip3 or pip command to install PyInstaller for Python 3. Type python --version into a Command Prompt terminal. You should see a response that tells says Python 3.x.

If the response says you have version 2.7 installed, or if it raises an error, then try the following command instead:

python3 --version

If Python 3 used the python alias (without the 3), then use the pip command to install PyInstaller:

python -m pip install --user pyinstaller

NOTE: Use the --user option if Windows raises a permissions error.

Otherwise use pip3 to installed the package:

pip3 install --user pyinstaller

Here’s the command to upgrade your current version of PyInstaller:

python -m pip install --upgrade --user pyinstaller

Screenshot of Windows command prompt installing the PyInstaller library with the PIP3 Python package manager

PyInstaller command-not-found error in Windows

Type python -m PyInstaller, pyinstaller, or PyInstaller into your Command Prompt window and press return. If none of these commands work, then it probably means that the pyinstaller.exe executable file is not set in your system’s environmental variables path.

Windows doesn’t know the path to the PyInstaller executable file

Use the echo %PATH in your command line to have it return a list of all the paths for your environmental variables.

Type environment into the Windows Search bar, or just open the System Properties window in your Control Panel. Make sure to click on the Advanced tab in System Properties, and then click the Environment Variables button:

Screenshot of the System Properties window in Windows and the Environmental Variables button in System Properties

Add the path for the ‘pyinstaller.exe’ to your Windows environment variables

Open a instance of File Explorer and navigate to C:\Program Files\Python37\Scripts or C:\Users\USER_NAME\AppData\Roaming\Python\Python37\Scripts and look for the executable. Once you find the file make sure to get its full path.

Click the New.. button in your Environmental Variables to add the system path for the pyinstaller.exe file. Make sure to type PyInstaller in the Variable name input field, and the path to the .exe file in the Variable value field.

Screenshot of the user variables in the Environmental Variables settings for Windows

Verify that the PyInstaller command for Python 3 is now working

Close all your instances of Command Prompt, and re-open it. Type PyInstaller or python PyInstaller into the window’s prompt, and it should now return a response from the executable.

Build a Windows executable using the Pyinstaller package for Python

You should now be able to build an executable from a Python script using PyInstaller using the following command:

python -m PyInstaller script_name.py

NOTE: Make sure to first change (cd) into the directory containing the Python script before running the PyInstaller command.

python -m pip install –upgrade –user pip python -m pip install –upgrade –user pyinstaller

pip -v install –user elasticsearch

PyInstaller C:UsersObjectRocketAppDataRoamingPythonPython37Scriptspyinstaller.exe

Python

echo %PATH%

C:UsersObjectRocketAppDataRoamingPythonPython37Scripts

python -m PyInstaller

pyinstaller –version

Screenshot of a Windows Command Prompt application created using PyInstaller for Python

Installing the PyInstaller Package on Ubuntu distros of Linux

pip3 install pyinstaller

sudo apt-get install libc-bin sudo apt-get install binutils

pyinstaller

pyinstaller: error: the following arguments are required: scriptname

pyinstaller cli.py python3 -O -m PyInstaller elastic_query.py

cd dist/elastic_query/

./elastic_query test

sudo rm -rf build/; sudo rm -rf dist/; sudo rm -rf pycache/; sudo rm elastic_query.spec

Change into the PyInstaller’s ‘dist’ directory

Screenshot of the dist directory created by the Python PyInstaller library

Execute the ‘elastic_query’ file created by PyInstaller with a query

pass a query to its parameters

Elasticsearch cluster response returned from the PyInstaller application’s API request

./elastic_query some_index "str field" "ObjectRocket"

Screenshot of a terminal printing the results of an Elasticsearch query request made using a PyInstaller application

Conclusion

We hope you enjoyed this tutorial on how to build a stand-alone Elasticsearch query application with Python. You can grab any or all of the code we used below.

Just the Code

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

# use sys for system arguments passed to script
import sys

# import the Elasticsearch client library
from elasticsearch import Elasticsearch

# create a client instance of Elasticsearch
client = Elasticsearch("http://localhost:9200")

# The script name is the first arg [0], but the second arg [1]
# can be used for the Elasticsearch index's name
try:
    INDEX_NAME = sys.argv[1]
except IndexError as err:
    print ("IndexError - Script needs the Elasticsearch index name:", err)
    sys.exit()

def make_query(filter):
    index_exists = client.indices.exists(index=INDEX_NAME)

    # check if the index exists
    if index_exists == True:
        print ("INDEX_NAME:", INDEX_NAME, "exists.")
        print ("FILTER:", filter, "\n")

        try:

            # pass filter query to the client's search() method
            response = client.search(index=INDEX_NAME, body=filter)

            # print the query response`
            print ('response["hits"]:', len(response["hits"]))
            print ('response TYPE:', type(response))

            # iterate the response hits
            print ("\nQueried %d Hits:" % response['hits']['total']['value'])
            for hit in response['hits']['hits']:
                print(hit["_source"])

        except Exception as err:
            print ("search(index) ERROR", err)
            response = {"error": err}
    # return an empty dict if index doesn't exist
    else:
        response = {}

    return response


def main():

    # declare variable for system arguments list
    sys_args = sys.argv

    # remove Python script name from args list
    sys_args.pop(0)

    # quit the script if there are not exactly 3 arguments
    if len(sys_args) != 3:
        print ("Three arguments needed. You provided:", sys_args)
        print ("First argument is index, and next two are the field and query:", sys_args)
        sys.exit()

    else:
        # get the field name and query value from sys args
        field_name = sys_args[1]
        value = sys_args[2]

        # pass the field name and value args to filter dict
        filter = {
            'query': {
                'match': {
                    field_name: value
                }
            }
        }

    # pass the filter dict to the make_query() function
    resp = make_query(filter)

# have interpreter call the main() func
if __name__ == "__main__":
    main()

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.