Command Line Scripts - Python Packaging

Python command line interface (CLI) scripts are programs that perform specific tasks when executed from the command line. These scripts help automate repetitive tasks and can be packaged for easy distribution to other developers. This article covers the complete process of creating, packaging, and distributing Python CLI scripts.

Creating the CLI Script

First, let's create a simple CLI script that accepts command line arguments using the argparse library ?

import argparse

def greet(name):
    print(f"Hello, {name}!")

def main():
    parser = argparse.ArgumentParser(description="A simple greeting CLI tool")
    parser.add_argument("name", help="Name of the person to greet")
    args = parser.parse_args()
    greet(args.name)

if __name__ == "__main__":
    main()

Save this as greet.py. You can run it from the command line ?

python greet.py Tutorialspoint

Output ?

Hello, Tutorialspoint!

Project Structure Setup

Create a proper directory structure for your package ?

greet-package/
??? greet/
?   ??? __init__.py
?   ??? cli.py
??? setup.py
??? requirements.txt
??? README.md

Move your script into greet/cli.py and create an empty __init__.py file to make it a Python package.

Requirements File

Create requirements.txt to specify dependencies ?

argparse
requests

Creating the Setup Configuration

Create setup.py to define package metadata and entry points ?

from setuptools import setup, find_packages

with open("README.md", "r", encoding="utf-8") as fh:
    long_description = fh.read()

setup(
    name="greet-cli",
    version="0.1.0",
    author="Your Name",
    author_email="your.email@example.com",
    description="A simple greeting CLI tool",
    long_description=long_description,
    long_description_content_type="text/markdown",
    packages=find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires=">=3.6",
    install_requires=[
        "argparse",
    ],
    entry_points={
        "console_scripts": [
            "greet=greet.cli:main",
        ],
    },
)

The entry_points section creates a console command called greet that executes the main() function from greet.cli module.

Building the Package

Install build tools and create distribution files ?

pip install build twine
python -m build

This creates two types of distributions in the dist/ directory ?

  • Source distribution (*.tar.gz) Contains source code
  • Wheel distribution (*.whl) Pre-built binary package

Testing the Package Locally

Before distributing, test your package locally ?

pip install -e .
greet TestUser

The -e flag installs the package in "editable" mode for development.

Distributing to PyPI

Upload your package to the Python Package Index for public distribution ?

# For testing (optional)
twine upload --repository testpypi dist/*

# For production
twine upload dist/*

You'll need to create accounts on PyPI and provide credentials during upload.

Installation and Usage

Once published, users can install your package ?

pip install greet-cli
greet "Python Developer"

Best Practices

Practice Description
Version Control Use semantic versioning (e.g., 1.2.3)
Documentation Include comprehensive README.md
Testing Test locally before distribution
Dependencies Pin specific versions in requirements.txt

Conclusion

Creating and distributing Python CLI scripts involves writing the script, setting up proper package structure, configuring setup.py with entry points, and using build tools for distribution. Following these steps ensures your CLI tools can be easily installed and used by other developers through PyPI.

Updated on: 2026-03-27T01:27:31+05:30

441 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements