Notes

This page contains my slides and some additional notes for my FOSSCON talk.

Further Reading

As I was preparing this talk I realized that I wouldn't have nearly enough time to cover everything that would be useful to someone writing command line applications. Here is some more information about topics I chose to skip.

Virtualenv

Virtualenv allows you to isolate the Python environment and dependencies for your application from the rest of the system. You should develop your application in a virtualenv and package it so that others can install it in a virtualenv as well. Following the packaging instructions below will help you do that.

I use and recommend a tool called virtualenvwrapper to manage the virtualenvs on development systems.

Cookiecutter

If you develop an application that follows best practices and the recommendations in my talk you will end up with a fair amount of boilerplate and repetition. Cookiecutter allows you to create the directory structure and files needed for a new application based on a template repository. I have created a repo for Python command line apps using Click. In order to start working on a new project, install cookiecutter and then run:

cookiecutter https://github.com/tylerdave/cookiecutter-python-package.git

It will prompt you for information about your project and create the needed files in a new directory. You can then install the package in editable mode and start developing.

cd projectdirectory pip install -e .

Logging

The logging module in the Python standard library provides a lot of functionality that you'd look for in logging like log levels and the ability to log to different destinations like file, stderr, etc.

I'd also recommend using Structlog if you intend to do any sort of analysis or reporting on your logs (and you should!)

Python Packaging

You should package your application so it's easy for others to install and manage and you should share it on PyPI if possible / practical. To do this you should follow the directions in the Python Packaging User Guide (PyPUG). If you want more background or details on packaging you can also view my recent talk: PyOhio 2015: Python Packaging from Init to Deploy.

Example Code

The examples used in the talk are available on GitHub in the FOSSCON 2015 Example repo. You can install it and experiment by cloning the repo and then running this within the repo dir:

pip install -e .

Credits

The content of my talk was based upon my own experiences with writing command line applications with Python but the following sources helped me formulate it and remind me of some points I didn't want to miss:

Armin Ronacher's introduction to Click: Building Command Line Applications with Click

Mark Smith's EuroPython 2014 talk: Writing Awesome Command-Line Programs in Python

Eliot Eshelman's blog post: Shell Script or Python Script/Program?