How to Package a Command Line Python Script

Python is a powerful programming language widely used for creating various applications, including command-line interface (CLI) scripts that automate tasks. To share these scripts with others, you need to package and distribute them properly.

This article will guide you through the complete process of packaging a Python command-line script, making it easy to distribute and install.

Step 1: Create a Virtual Environment

First, create an isolated environment for your project to manage dependencies effectively ?

python -m venv myproject_env

Activate the virtual environment ?

# On Windows
myproject_env\Scripts\activate

# On macOS/Linux  
source myproject_env/bin/activate

Step 2: Create the Project Structure

Organize your project with the proper directory structure ?

myproject/
??? mycommand/
?   ??? __init__.py
?   ??? cli.py
??? setup.py
??? requirements.txt
??? README.md

Step 3: Write Your CLI Script

Create the main CLI script in mycommand/cli.py ?

import argparse

def main():
    parser = argparse.ArgumentParser(description='My CLI Tool')
    parser.add_argument('--name', default='World', help='Name to greet')
    args = parser.parse_args()
    
    print(f'Hello, {args.name}!')

if __name__ == '__main__':
    main()

Create an empty __init__.py file in the mycommand directory to make it a Python package.

Step 4: Create the Setup Script

The setup.py file defines package metadata and installation instructions ?

from setuptools import setup, find_packages

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

setup(
    name='mycommand',
    version='1.0.0',
    author='Your Name',
    author_email='your.email@example.com',
    description='A simple 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',
    entry_points={
        'console_scripts': [
            'mycommand=mycommand.cli:main',
        ],
    },
)

Step 5: Create Requirements File

List your dependencies in requirements.txt ?

click==8.0.4
requests==2.28.1

Install dependencies ?

pip install -r requirements.txt

Step 6: Build the Package

Install build tools and create the package ?

pip install build
python -m build

This creates distribution files in the dist/ directory.

Step 7: Install and Test Locally

Install your package locally for testing ?

pip install dist/mycommand-1.0.0-py3-none-any.whl

Test the installed command ?

mycommand --name Python

Expected output ?

Hello, Python!

Step 8: Distribute the Package

Upload to PyPI for public distribution ?

pip install twine
twine upload dist/*

For private distribution, share the .whl file directly with users.

Package Structure Summary

File Purpose
setup.py Package configuration and metadata
cli.py Main command-line script
requirements.txt Project dependencies
__init__.py Makes directory a Python package

Conclusion

Packaging Python CLI scripts involves creating proper project structure, defining metadata in setup.py, and using build tools to create distributable packages. This process enables easy sharing and installation of your command-line tools across different systems.

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

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements