How to Package and Deploy CLI Applications With Python


Based on the interface we have two types of applications CLI (Command Line Interface) applications and GUI (Graphical User Interface) applications.

A command line application or, console application is the one which can be accessed from a shell or, command line through a text interface, these accepts inputs from the user in the text format.

Unlike GUI Applications which provides a Graphical interface containing, buttons text boxes and icons, to the users to access the underlying application.

Python is a popular programming language for developing CLI applications, though the process of packaging and distributing such applications is a bit difficult. We can achieve this easily using the inbuilt and external tools.

Let us look at all the steps involved in packaging and deploying CLI applications with python.

Step 1: Create a Python Package

First of all, we have to create a python package to create a CLI application. A package is a folder which contains a init.py file this indicates that this folder is a python package. This will be useful when this CLI application is installed by someone.

We can create a folder with any name for our package and we will add 2 files inside it, the first file is init.py and the second file is my_cli_app.py.

In this example, we are trying to create a package called my_package and adding a file called my_cli_app.py, which contains the code for our CLI application.

my_package/
 __init__.py
 my_cli_app.py

Step 2: Define a CLI Entry Point

In the second step we will define a Command line Interface entry point or CLI entry point. An entry point is the python function that will be executed first when we run our application from the CLI.

The click library is a very popular library of python which is usually used to create CLI applications. We have to import the click library of python to create an entry point.

In this example, we have defined a CLI entry point using the above mentioned click library of python. The @click.command() decorator informs the click that this function is a CLI command. The @click.option() decorator defines a command-line option, which allows the user to pass an argument to the command.

import click
@click.command()
@click.option('--name', default='World', help='The person to greet.')
def hello(name):
   click.echo(f'Hello, {name}!')
if __name__ == '__main__':
   hello()

Step 3: Add Dependencies to Setup.py

The third step is to add our dependencies to the setup.py file. These dependencies are needed by our CLI application. The setup.py file is used by python to build and install our CLI application. For adding the dependencies we will use the install_requires parameter from the setuptools.setup() function.

Here, we have added click as a dependency using the install_requires parameter. We have also defined an entry point for our CLI application using the entry_points parameter. The console_scripts entry point tells Python that this is a command-line script, and the my_cli_app command is defined as the entry point.

from setuptools import setup, find_packages
setup(
   name='my_package',
   version='0.1',
   packages=find_packages(),
   install_requires=[
      'click',
   ],
   entry_points='''
      [console_scripts]
      my_cli_app=my_package.my_cli_app:hello
      ''',
)

Step 4: Build and Distribute Our Package

The final or fourth step in packaging and deploying your Python-based CLI application is to build and distribute our package. With the use of the "setuptools" library, we can build and distribute our package easily.

To build our package, run the following command −

$ python setup.py sdist bdist_wheel

This above command will create a source distribution (sdist) and a binary distribution (bdist_wheel) of our package in the dist directory or folder. The source distribution (sdist) contains the source code and resources, and the binary distribution contains the compiled code and can be installed on different platforms.

To distribute our package, we need to upload it on the Python Package Index (PyPI) using the twine library. For that we must have an account on PyPI. So, we need to create an account on PyPI and then install the twine library using pip −

$ pip install twine

Next, we can upload our package to PyPI using the following command −

$ twine upload dist/*

This command will upload all the distributions in the dist/ directory to PyPI.

Step 5: Install and Use our Package

To install our CLI application package, users can use pip to download and install or package from PyPI −

$ pip install my_package

Once the package is installed, users can use the CLI command defined in the entry_points parameter. In the following snippet, we have passed an argument as name to the --name option, which will be used by the hello function to print the greeting message with the name given as argument. This makes the application a bit user friendly.

$ my_cli_app --name Alice
Hello, Alice!

Conclusion

In this article, we learned how to package and deploy a command line application using python. We learned what a CLI application is, how it is useful, and all the steps needed to create and deploy a CLI application. We can follow each step written in this article, then we can create Python package, define CLI entry point, add dependencies to setup.py file, create and distribute package and install and use our package easily. By following the steps above, we can be sure that our application is easily packaged and deployed and can be easily used by other programmers.

Updated on: 20-Apr-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements