Command Line Scripts - Python Packaging


Python is a very popular and powerful programming language which is used to create a large number of applications with different purpose. A command line interface script or CLI script is a script that performs a specific task which is scripted, when it is executed through command line and it is used to automate so many tasks. A CLI script is very useful for developers.

We can also create these CLI scripts in python. After creating a CLI script we can also package it and share it with other programmers to use. Therefore, we must know how to package a CLI script and distribute it. In this article we will go through all the necessary steps for packaging and distributing.

This article will guide you through all the steps needed to package a python script and distribute them, it will make it very easy for you to package a script on your own.

Steps to Create Command Line Scripts

We will follow the below steps to create a command line script in python −

Step 1: Importing the Required Libraries

We need to import the argpase library for this task. This library helps to parse command line arguments.

import argparse

Step 2: Creating a Command Line Script

We first need to create a command line script or CLI script in python. This will contain the main function that our script will perform on execution. This can also accept arguments from the command line itself using the argparse library. Following is an example of a simple CLI script −

import argparse
def greet(name):
   print(f"Hello, {name}!")
if __name__ == "__main__":
   parser = argparse.ArgumentParser()
   parser.add_argument("name", help="Enter name: ")
   args = parser.parse_args()
   greet(args.name)

This above script has a greet function which accept a name as an argument and print the greeting that is written inside it with that name/argument in the command line. We can run the following command in the terminal or command line to run this script −

python greet.py Tutorialspoint

This will execute the script and give the following output −

Hello, Tutorialspoint!

Step 3: Adding Dependencies and Packaging Requirements

Most command line scripts will have dependencies on other libraries or packages. These dependencies need to be included in the package so that they can be installed along with the script. You can add dependencies to your package by creating a requirements.txt file in the same directory as your script.

If the project you are working on requires a few libraries then requirements.txt will contain the name of all those libraries, like if it needs requests library and argparse library the requirments.txt will be −

requests
argparse

This file specifies that the requests library is required by the package.

Step 4: Creating a Package Using Setuptools

After creating the command line script and defining the dependencies needed for the script, we can start creating the package for it using the setuptools library. This library provides tools and features for building and distributing packages.

For using the setuptools library we need to create a setup.py file in the directory where our script is already present. Below is an example of the setup.py file −

from setuptools import setup, find_packages
setup(
   name="greet",
   version="0.1.0",
   packages=find_packages(),
   install_requires=["requests"],
   entry_points={
      "console_scripts": [
         "greet=greet:main",
      ],
   },
)

This file defines the package name, version, and dependencies using the setup function. It also specifies the entry point for the script using the entry_points dictionary. This tells setuptools to create a console script called greet that runs the main function in the greet module.

Step 5: Distributing the Package

After we have created the package of our CLI script using setuptools library, our package is ready to be distributed and be used by other users or programmers. We can distribute our package by using the PyPI or Python Package Index. PyPI is a repository of python packages that enables users to install packages and also share them easily.

We will use twine tool to distribute the package to PyPI. We need to create 2 filed one named sdist which stands for source distribution and bdist__wheel for wheel distribution. We can do this by the below command −

python setup.py sdist bdist_wheel

This will create two distribution files in the dist directory: a source distribution (*.tar.gz) and a wheel distribution (*.whl). Next, we can use the twine tool to upload our package to PyPI. First, install twine using pip −

pip install twine

Then, navigate to the dist directory and use the following command to upload your package −

twine upload dist/*

After this our package is uploaded to PyPI and this makes it available for other to use it after installing by using pip

Conclusion

In this article we learnt how we can create a package for command line script and also how we can distribute it. We understood uses for CLI scripts and its purposes. We also learnt about two packages argparse and requests. We went through all the steps needed to package a CLI and distribute it on PyPI. By following all the steps mentioned in the articles carefully you can also easily create a CLI script and package and distribute it.

Updated on: 20-Apr-2023

235 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements