Three Command-line Tools for Productive Python Development

Apr 06, 2014

Below are three tools that I started using recently that I have since found indispensable when developing with Python.

I. Bootstrap your project with cookiecutter

I am a big fan of project templates. They enable you to reuse best practices and patterns whenever you start a new project.

Audrey Roy's cookiecutter tool makes project templates easy to use and create. Just run the cookiecutter command, answer some questions about your project, and cookiecutter will create an entire scaffold for your project.

cookiecutter logo

Installing and using cookiecutter

To get the cookiecutter CLI, install with pip.

$ pip install cookiecutter

Choose a template and copy its git URL. There are a number of available Python templates.

Run the cookiecutter command with the git URL.

$ cookiecutter

Answer the prompts that appear, and you're done!

II. Automatically activate your virtual environment with autoenv

Sometimes you forget to activate your project's virtual environment. Never again with Kenneth Reitz's autoenv.

autoenv automatically executes code when you cd into a directory with a .env file. You can use it to automatically activate a project's virtual environment.

Installing and using autoenv

If you are on a Mac with homebrew, run:

$ brew install autoenv
$ echo 'source /usr/local/opt/autoenv/' >> ~/.bash_profile

You can also use pip or git to install on other platforms.

Drop a .env file in your project's directory with the command to activate its environment.

# .env
source activate path/to/env/bin/activate

# Or, for virtualenvwrapper:
# workon myproject 

# Or, for anaconda:
# source activate myproject

III. Auto-import modules and functions into your shell sessions with konch

Importing the same packages, modules, and classes every time you run the Python shell is tedious. That's why I created konch.

Inspired by Flask-Script and the shell_plus command from django-extensions, konch allows you to create project-specific namespaces for your Python shell. You can even configure your shell's welcome banner and input prompt.

konch logo

Installing and using konch

Install with pip.

$ pip install konch

In your project directory, create a new .konchrc file with konch init.

$ konch init

Edit the context dictionary in your .konchrc to include anything you want accessible in your shell sessions.

import konch
import mypackage
from mypackage import cheese

    'context': {
        'mypackage': mypackage,
        'cheese': cheese,
        'bleu': cheese.bleu,
        'cheddar': cheese.cheddar

Run konch to start your shell.

$ konch

A Note for Git Users

If you don't want your .env and .konchrc files to show up in source control, you can add them to a global .gitignore file.

$ git config --global core.excludesfile ~/.gitignore_global
$ echo ".env\n.konchrc" >> ~/.gitignore_global

Related Links

tagged in python

Please send comments by email. I welcome your feedback, advice, and criticism.