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:
conda create --name mycliapp python=3.9
This will create a new environment named mycliapp with Python version 3.9.
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
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.
To use Git and GitHub for version control, we need to follow these steps:
git init
This will create a new Git repository.
领英推荐
git add mycliapp.py
git commit -m "Initial commit"
This will create a new commit with the message "Initial commit".
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.
git push -u origin main
This will push our changes to the main branch of our remote repository.
Distribute the app as PyPI package
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:
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.
python setup.py sdist bdist_wheel
This will create a source distribution and a binary distribution in the dist directory.
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.
PhD Candidate at University of Liège
1 年Thanks for sharing Anass ??. Create, Teach, Inspire...