Packaging Python Scripts into CLI Commands

Chris Doucette
Nov 28, 2018 · 3 min read


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

$ python3

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 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
$ touch caesar/
$ touch caesar/

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

|-- caesar
| |--
| `--


Once you have the directory setup we will create the script in 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 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

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 file we specified the following for our entry point

'console_scripts': [
'caesar = caesar.caesar:main'

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

$ sudo python3 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.


Creating a 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 on November 28, 2018.


Easily consumable information security stories ::

Chris Doucette

Written by

Security Engineer | | Follow me on Twitter: @thegrumpyape



Easily consumable information security stories ::

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