Building and Distributing a Python CLI Application
Command-Line Interface Application

Building and Distributing a Python CLI Application

Command Line Interface (CLI) applications are powerful tools that allow users to interact with software through a terminal, without the need for a graphical user interface. In this article, we will discuss the steps to create a CLI application using Python, configure a conda environment, install required packages, use Git and GitHub for version control and distribute the application as a PyPI package.

Configure conda environment

Conda is a package and environment management system for Python that allows developers to create and manage isolated environments for different projects. In order to configure a conda environment for our CLI application, we need to follow these steps:

  • Install conda: Download and install conda from the official website.
  • Create a new environment: Open a terminal and run the following command to create a new conda environment.

conda create --name mycliapp python=3.9        

This will create a new environment named mycliapp with Python version 3.9.

  • Activate the environment: Activate the environment by running the following command.

conda activate mycliapp        

Now, any package we install will be installed in this environment.


Install required packages for CLI application

Our CLI application will need several packages to function properly. We can use the pip package manager to install these packages. Run the following command to install the required packages.

pip install click requests        

Here, we are installing the click and requests packages. click is a popular Python package for creating CLI applications, while requests is a package for making HTTP requests.


Create a CLI application

With our environment configured and required packages installed, we can now create our CLI application. We will create a simple application that fetches a random quote from an API and displays it in the terminal.

Create a new file mycliapp.py and add the following code:

import click
import requests

@click.command()
def get_quote():
    response = requests.get('https://api.quotable.io/random')
    if response.status_code == 200:
        data = response.json()
        click.echo(data['content'])
    else:
        click.echo('Failed to fetch quote')

if __name__ == '__main__':
    get_quote()        

Here, we are using the click package to define our command get_quote. The requests package is used to make a GET request to the https://api.quotable.io/random endpoint, which returns a random quote. We then print the quote to the terminal using click.echo.


Use Git and GitHub for versioning

No alt text provided for this image
Git and GitHub

Using Git and GitHub for version control is a best practice for software development. It allows us to track changes to our code over time, collaborate with other developers, and easily roll back changes if needed.

First, make sure to install Git and have a GitHub account.

To use Git and GitHub for version control, we need to follow these steps:

  • Initialize Git: Open a terminal and navigate to the directory where our mycliapp.py file is located. Run the following command to initialize Git.

git init        

This will create a new Git repository.

  • Add files: Run the following command to add our files to the Git repository.

git add mycliapp.py        

  • Commit changes: Run the following command to commit our changes.

git commit -m "Initial commit"        

This will create a new commit with the message "Initial commit".

  • Create a new repository on GitHub: Go to the GitHub website and create a new repository with the same name as our local repository.
  • Add remote: Run the following command to add the remote repository on GitHub.

git remote add origin https://github.com/username/mycliapp.git        

Here, username is your GitHub username and mycliapp is the name of the repository you just created.

  • Push changes: Run the following command to push our local changes to the remote repository.

git push -u origin main        

This will push our changes to the main branch of our remote repository.


Distribute the app as PyPI package

No alt text provided for this image
Python package Index

PyPI (Python Package Index) is a repository of software packages for the Python programming language. It allows developers to distribute their Python packages to a wider audience.

To distribute our CLI application as a PyPI package, we need to follow these steps:

  • Create a setup.py file: Create a new file setup.py in the same directory as our mycliapp.py file, and add the following code:

from setuptools import setup

setup(
  name='mycliapp',
  version='0.1.0',
  py_modules=['mycliapp'],
  install_requires=[
    'click',
    'requests'
  ],
  entry_points='''
    [console_scripts]
    mycliapp=mycliapp:get_quote
  '''
)        

Here, we are using the setuptools package to define our package metadata and dependencies. We are also using the entry_points parameter to create a console script that can be run from the terminal.

  • Create a distribution package: Run the following command to create a distribution package.

python setup.py sdist bdist_wheel        

This will create a source distribution and a binary distribution in the dist directory.

  • Upload to PyPI: Upload the distribution package to PyPI by running the following commands.

pip install twine
twine upload dist/*        

This will upload our package to PyPI and make it available to the public. Make sure you have a PyPI account, as the credentials are needed to upload and distibute the package.

Your package can now be easily installed on any Python environment by simply running:

pip install mycliapp        


Conclusion

In this article, we went through the basic process for creating a CLI application, versioning it with Git and GitHub and distributing it as a PyPI package. By following these steps, we can ensure that our application is easily distributable and maintainable making it easier to reach a wider audience and provide a better user experience. In the next tutorial, I'll share with you how to distribute your application as a Docker image and the benefits behind containerization.

Abderrazzaq Kharroubi

PhD Candidate at University of Liège

1 年

Thanks for sharing Anass ??. Create, Teach, Inspire...

要查看或添加评论,请登录

社区洞察

其他会员也浏览了