The other day I helped a friend setup Django on his computer to develop his first Django project ever. During this process, I realized how many shortcuts and tools I use when I develop Django. The main purpose of this post is to document these.

Edit: This guide might be Mac specific. Should work on Linux as well, though.

Go get Oh My ZSH

Oh My ZSH is an awesome framework built in top of ZSH. It will increase the fun you have, while typing commands in the terminal and on top of that provide you with some great tools for designing and extending ZSH.

I open sourced (some) of my ZSH aliases and shortcuts. I think they are worth checking out. I made a few aliases on top of them, e.g.

alias dr='dj runserver'

You can add as many as you want yourself. Another thing I enjoy about Oh My ZSH is the theming. my own theme, but there are plenty available in their [repository on github]https://github.com/robbyrussell/oh-my-zsh.

Edit: I completely forgot to mention that Oh My ZSH helps you with many other things, including auto completion of git (as shown in the screenshot). You should definitely check it out, there is a big chance there are plugins for the software you use already.

Use virtualenv

This is very simple. If you install packages globally, you are doing it wrong. If you disagree, I encourage you to google it. Installing virtualenv is very simple:

sudo pip install virtualenv

If you don't have pip already get it with:

sudo easy_install pip

Using virtualenv is very easy. You run:

virtualenv .

in your project folder and it will setup a virtual environment. To enter the virtual environment run:

source bin/activate

I don't know about you, but I hate to see bin/ lib/ and other folders in my project directory, so I made a wrapper around virtualenv in my ZSH scripts.

Using my ZSH scripts you can just run "venv" and it will create you a virtualenv in .venv/ - well hidden from everything else. To "work" in your virtual environment, write "work".  Simple, right?

For those of you who know virtualenvwrapper, my script is not exactly the same. Virtualenvwrapper stores the virtualenvs in your home directory, whereas my script stores them in the project itself. This way, if you delete the project, you delete the virtualenv automatically as well. Furthermore, you don't have to write:

workon <project>

but just:

work

if you are in the correct directory.

Managing dependencies

Generally, most packages you need to install will be available on the Python Packaging Index (PyPI). Once you have activated your virtual environment, you can install packages using

pip install <package>

The list of packages required for your specific project is by convention stored in requirements.txt. Furthermore, it is suggested that you "pin" all your requirements. That means locking their version, like this "Django==1.5".

On the pip installers website, you can read more about how to specify the requirements in requirements.txt, including how to install directly from e.g. a git repository.

It is a good idea to verify that you added all requirements to requiremens.txt by using the command:

pip freeze

It outputs all requirements in the correct format with locked versions. While you theoretically could pipe that directly into requiremens.txt, I highy recommend against it. You could potentially install undesired applications on the server, only used for debugging.

Use a good project layout

I always use a project layout like this:

project/static/ project/init.py project/settings.py project/urls.py project/wsgi.py manage.py requirements.txt

If you are using my ZSH scripts, you will already know how to bootstrap this:

venv && work && dj syncdb --all && dj migrate --fake && dr
  • maybe a fit for a new alias? ;)

Use a good IDE, I prefer PyCharm

There might be other good IDE's out there for Python so instead of only recommending the one I use, I list the features I enjoy and then you can have a look at the options yourself.

  • Syntax highligthing
  • PEP8 checking
  • Jump to definition (CMD+click any reference and it takes you to the definition)
  • Good, useful shortcuts
  • Supports virtualenv
  • Supports git and highlights changes in your code
  • Supports django templates

Maybe I forgot a few things - but most important: I feel comfortable using it.

If you're on OS X - Check out GitX or something similar

You are already using Git, right? I like to double check everything before I commit. When I changed everything I want for a specific commit, I open GitX and manually "git add" every file. I go through every line to make sure I got rid of all those print statements and that my refactors were successful. GitX is old and unmaintained - I found a fork on GitHub which adds branches on the left side of the GUI - but I wasn't able to find it again. The important thing here is that you find a tool you enjoy using and use it each time you commit something more than a few lines of code. Your co-workers will love this.

Edit: Thanks to @alesdotio I recovered the link to the GitX fork I use

Makefiles are cool

... but don't overdo it! On larger apps, I sometimes add a Makefile that can bump the version, git tag the release and push a new release to PyPi (or private PyPi-like installation). A Makefile is a very old format, where you can define a series of tasks and execute them with "make ". This extremely simple tool can help you reproduce simple commands on many machines without the user having to know them. Good for distributing projects to non-python programmers, such as frontend engineers. Feel free to change/edit/fork or do whatever you want with my example file. Makefiles can also be used for bootstrapping a project, such as setting up virtualenv, database etc for development. Some people like add e.g. the command to run the webserver in their makefile. I am strongly against that - I think Makefiles are for setting up your project (e.g. compiling for some languages or fetching requirements).

Use Postgres as your database

If you are already using Postgres in production, you should take a look at the excellent Postgres.app. It has never been this easy to run Postgres on your mac before - and for migrations you avoid a lot of pain by using Postgres locally as well. If you are not already using Postgres, I recommend you to check out Craig Kerstiens' blog.

Use South for easy database migrations

Most people reading this will already be using South. If you are not one of them, South is a database migration tool for Django. They have an excellent tutorial on how to get started in their documentation.

Did I miss something?

I probably did. Feel free to get in touch if you have questions or think I missed something. I will try to update this, as I start using different tools. If you liked my post, please don't forgot to participate in the discussion on hacker news or follow me on twitter.