Packaging Python Scripts into CLI Commands

Chris Doucette
Nov 28, 2018 · 3 min read

Introduction

Today I want to show you how you can turn a python script from this:

$ python3 coolscript.py

To this:

$ coolscript

How It’s Done

How you are able to change a python script into a full fledged CLI command is by leveraging setup.py. I will demonstrate this by creating a program that will perform a caesar cipher on any text you provide it.

Project Structure

First we will need to create our projects directory:

$ mkdir caesar 
$ cd caesar
$ mkdir caesar
$ touch setup.py
$ touch caesar/__init__.py
$ touch caesar/caesar.py

And what we should be left with is a directory structure that looks like this:

caesar/ 
|-- caesar
| |-- __init__.py
| `-- caesar.py
`-- setup.py

Script

Once you have the directory setup we will create the script in caesar.py. The objective of this tutorial is not to teach you how to create the script however I will add it below if you are interested.

Also we will need to make one change in our __init__.py file to allow our script to be accessed inside the directory.

Setup File

Now that we have completed our script it is time to fill out our setup.py

Lets break down this file line by line. First we are importing setuptools which is the standard library used by Python for building scripts. Next we call the setup function of setuptools and pass in the required parameters. The parameter we want to focus on the most is entry_points

  • name: The name of the script

Entry Points

In our above setup.py file we specified the following for our entry point

entry_points={
'console_scripts': [
'caesar = caesar.caesar:main'
]
},

Here we are configuring the command caesar to be related to our main function in our caesar.py script file. Now when we execute our setup.py file it should build and install our script and allow us to access it using the caesar command.

$ sudo python3 setup.py install

Now that our script has been built lets try to use it on the CLI

$ caesar "Test Message" 
Whvw Phvvdjh
$ caesar "Whvw Phvvdjh" --decrypt
Test Message

And it worked! We can now use our script as a CLI command.

Conclusion

Creating a setup.py file is an important step for any python project since it allows you to provide information to users about the file and allows you to create extra functionality to the script such as adding it as a CLI command.

Thanks for reading!

— Chris


Originally published at ediblesec.com on November 28, 2018.

EdibleSec

Easily consumable information security stories :: https://ediblesec.com

Chris Doucette

Written by

Security Engineer | ediblesec.com | Follow me on Twitter: @thegrumpyape

EdibleSec

EdibleSec

Easily consumable information security stories :: https://ediblesec.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade