How to Package a Command Line Python Script

Python is a powerful programming language that has various applications in the world of software engineering and is very widely used. We can create various types of applications using Python. We can also make a CLI script in Python which can be used to automate many tasks. We can also then package the CLI scipt.

For these scripts to be used by other we need to package and distribute these applications. So we must know how we can package a command line python script. This article will guide you through all the steps needed to package a python script and it will make it very easy for you to package a script on your own.

We need to follow the below steps in order to package a command line script or CLI script in Python.

Step 1: Install Required Libraries

We need to install all the required libraries of python before starting with the script. The requirements.txt file contains all the needed libraries for the command line script you want to create. These libraries are not fixed and change depending upon the requirements of the script. To install run the below command

pip install -r requirements.txt

This command will install all the required libraries and their dependencies, which are listed in the requirements.txt file.

Step 2: Create a Virtual Environment

In the second step we need to create a virtual environment that will contain all the libraries needed for the project also it contains the dependencies for this project. We create a virtual environment to make sure that the project works fine while packaging and while developing. We will use the below command to create a virtual environment −

python3 -m venv env

This command will create a new virtual environment in the .env directory.

Step 3: Activate the Virtual Environment

After the virtual environment is created we have to activate it. We will use this command below to activate it −

source env/bin/activate

The above command will activate the virtual environment we crated earlier, and any libraries or dependencies that you install will also be installed inside the virtual environment.

Step 4: Create the Setup Script

The next step is to create a setup script. A setup script is a Python script that describes the package that you want to create. It includes information such as the package name, version, author, and other metadata. Here is an example setup script −

from setuptools import setup, find_packages
   author='Your Name',
   description='My Command Line Tool',
      'console_scripts': [

We have used the setup tools library of python to create the package. In the above code we have defined the name of our package along with the version author and various other metadata. We also have defined the entry point for the script that is the point from where the execution of package will start, which is the file.

Step 5: Create the CLI Script

The 5th step is creating a Command Line Interface script or CLI script. This script will have the main function that aur script needs to be perform. This script will be executed when it is called from command line. Below is a simple example

def main():
   print('Hello, World!')

In the above script we have simply made a main function which will print hello world when executed. This is our main function that needs to be performed when we call the CLI application.

Step 6: Build the Package

Once you have created the setup script and the CLI script, you can build the package using the following command −

python sdist bdist_wheel

This command will create a source distribution and a binary distribution of your package. The source distribution will contain the source code for your package, and the binary distribution will contain a compiled version of your package.

Step 7: Distribute the Package

After building the package now we have to distribute it so that other developers or programmers can use it. We can easily distribute is using the package manager of python which is PyPI. We can also share the package manually to users by simply sending the built package file to the end user over google drive or any media sharing platform.

Step 8: Install and Test the Package

We should install the package on our system to test. We can install this package using pip which is package installer for python. We will use the below command to install the package −

pip install mycommand-0.1-py3-none-any.whl

This will install the package on our system successfully. We can now also test this package by running this command below −


This command will execute the main function in the file and print "Hello, World!" to the console.


In this article we understood how we can package a command line script written in python. We went through all the steps needed to package the application. After following all the steps in this article you can easily package your own command line script which will include dependencies and meta data. The package that you will create can easily be distributed to users as well by sharing them or using PyPI.

Updated on: 20-Apr-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started