Django development on Snow Leopard
I got my Snow Leopard in the mail lately and I decided to do a format before installing it. I had also read a lot about the new way of developing Django applications inside it’s own virtual environment. And installing Python packages with PIP inside of those environments. The clean install of Snow Leopard gave me another reason to start using all this new technology. In this article I will give you a quick guide into creating a great Django development environment making use of the following software:
- Mercurial
- Git
- PIP
- Virtualenv
- Virtualenvwrapper
- Imaging Library (with JPEG and Freetype support)
When everything is installed I will describe in short how we develop our Django site’s at Bread & Pepper with all these shiny new tools.
XCode
Some of the above software must be compiled and therefore we need to install XCode. You can find XCode on your Snow Leopard DVD or at the Apple developers website. Installing XCode is really straight forward and I expect that everyone that is interested in this kind of articles knows how to install it.
Mercurial
We will first install Mercurial for downloading the most recent versions of some of the Python packages that are on Bitbucket. We won’t use MacPorts but just compile it by hand1.
Let’s launch the Terminal from your ‘’Applications’’ folder and start with editing your .profile so you have /url/local/bin on your $PATH. I’m using Vim, so I will edit it by typing vim .profile inside the terminal and add the following code:
export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
export PYTHONPATH=/usr/local/lib/python2.6/site-packages
Save and close the file (:wq or ZZ in Vim) and apply the new changes by typing source ~/.profile in your terminal.
We can now download and install Mercurial. First download the source in your sources directory. Mine is /Sources (mkdir ~/Sources if you want the same).
cd ~/Sources
curl -O http://mercurial.selenic.com/release/mercurial-1.3.1.tar.gz
And finally let’s build and install.
tar xzvf mercurial-1.3.1.tar.gz
cd mercurial-1.3.1
make all
sudo make install
cd ..
We can check if Mercurial got installed successfully by typing which hg. You should get /usr/local/bin/hg.
Git
Now let’s install Git, also needed for some packages that are on Github. Go to the ~/Sources folder again and do the following:
curl -O http://kernel.org/pub/software/scm/git/git-1.6.4.4.tar.bz2
tar xzvf git-1.6.4.2.tar.bz2
cd git-1.6.4.4
./configure --prefix=/usr/local
make
sudo make install
cd ..
Verify it installed correctly with which git. You should see /usr/local/bin/git.
PIP
PIP is a Python package manager that works well with Virtualenv because you can say in which ‘’Virtual Environment’’ you want to install the package. You can also supply PIP with an requirements file which lists all the packages used in the project. More abouth this later, let’s install it first:
sudo easy_install pip==dev
Virtualenv and Virtualenvwrapper
Virtualenv supplies you with a isolated Python environment for all your Django projects. The production version didn’t work with Snow Leopard yet, so you must install the latest version from the repository. You can do that with:
sudo easy_install http://bitbucket.org/ianb/virtualenv/get/tip.zip
Virtualenvwrapper will make it easier to switch between environments. Let’s install that also:
sudo easy_install virtualenvwrapper
We will need to add two lines to our ~/.profile to make it work.
export WORKON_HOME=$HOME/Sites/virtualenvs
source /usr/local/bin/virtualenvwrapper_bashrc
Make sure that WORKON_HOME is the directory where you will have your Virtual environments. After you have run source ~/.profile you can use the package by typing workon.
Imaging (PIL) with JPEG and Freetype2 support
Almost every Django project will make use of the Python Imaging library. To be able to use this with libjpeg and Freetype2 you must have some extra libraries installed. Let’s start with libjpeg by going to your ~/Sources directory and typing the following commands2:
curl -O -L http://downloads.sourceforge.net/project/libjpeg/libjpeg/6b/jpegsrc.v6b.tar.gz
tar xzvf jpgscr.v6b.tar.gz
cd jpeg-6b
cp /usr/share/libtool/config/config.sub .
cp /usr/share/libtool/config/config.guess .
./configure --enable-shared --enable-static
make
Before installing you may need to create some extra directories.
sudo mkdir -p /usr/local/include
sudo mkdir -p /usr/local/lib
sudo mkdir -p /usr/local/man/man1
You can now install libjpeg.
sudo make install
Next we need to install the Freetype2 library. This is a lot simpler.
cd ~/Sources
curl -O -L http://downloads.sourceforge.net/project/freetype/freetype2/2.3.9/freetype-2.3.9.tar.gz
tar xvfz freetype-2.3.9.tar.gz
cd freetype-2.3.9
./configure && make && sudo make install clean
Combining all the tools
You now have all those great tools installed, but what can you do with them? In this piece I’m going to describe how we work at “Bread & Pepper”:http://breadandpepper.com. You will have your own workflow, but maybe you can get some tips from ours. The first thing I needed was a general Django requirements file for PIP. I have all my virtual environments in my ~/Sites/virtualenvs directory this is also the place for my base requirements file for PIP. Create a new file with vim ~/Sites/virtualenvs/django-basic-requirements.txt and fill it with the following:
# Docutils for admin documentation.
docutils
# Python Imaging Library
http://effbot.org/downloads/Imaging-1.1.6.tar.gz
# Latest Django version
-e svn+http://code.djangoproject.com/svn/django/trunk#egg=Django
Save it :wq. This file supplies us with the packages we use for every Django project. So every time we start a new project, we only have to supply this file to PIP and the rest is magic.
We now need to create a virtualenvs directory and our first Virtual Environment.
cd ~/Sites
mkdir virtualenvs
cd virtualenvs
All our Virtual enviroments will reside in this directory. Let’s start building a site called magicpony.
virtualenv magicpony --no-site-packages
The above command creates a new directory called magicpony.com that is your new isolated Python environment. The --no-site-packages argument makes sure the environment is completely isolated by not inheriting the packages in your system wide $PYTHONPATH.
Now, let’s install our basic Django requirements by supplying the file we created earlier (make sure you’re still in the ~/Sites/virtualenvs directory) to PIP.
pip install -E magicpony.com -r django-basic-requirements.txt
The -E magicpony argument tells PIP to use the magicpony virtual environment. The -r django-basic-requirements.txt argument tells it to look inside a requirements file to know wich packages to install. You can now activate the environment by typing:
workon magicpony.com
If all goes well, you will see (magicpony) placed in front of your prompt. If workon doesn’t work, this could be because you don’t have the right settings for Virtualenvwrapper inside your ~/.profile.
Let’s activate our environment, enter the right directory and create a new Django project:
workon magicpony.com
cd ~/Sites/virtualenvs/magicpony.com
django-admin.py startproject magicpony
Now when we enter the newly created directory, we can go start our Django server with ./manage.py runserver.
Another thing we do is placing a requirements.txt file inside our project directory (also under SCM3) which points to all the specific packages needed for this particular Django project. This way every new developer only has to do the following:
pip install -E magicpony -r virtualenvs/magicpony.com/magicpony/requirements.txt
And they can start working on the new project.
Conclusion
Well, it was a long ride, but now you have a great Django development setup. It certainly made our lives easier by not constantly having to make sure that the right packages were installed. We also use the requirements files for our servers so deployment (with Fabric) goes easy and secure. If you feel that I left something out, or made same errors, please do contact me.
Thanks to Dan Benjamin from Hivelogic for supplying the instructions on how to install Mercurial. ↩
Thanks go out to Rich Atkinson’s from Jetfar.com for the instructions. ↩
We only keep the Django application directory under SCM. This also contains the
requirements.txtfor that project. This way when a new developer needs to work on the project he only has to create a Virtual Environment, pull the Django project from Github or Bitbucket inside this directory and install all the required packages from the requirements file with PIP. ↩