Blog
Bla bla bla

This is where we ramble about coding, design and things we love. Apologies to all the Dutch readers, it's only in English.

02
oct

Django development on Snow Leopard

Django development on Snow Leopard I got my Snow Leopard in the mail lately and I decided to install it after a clean format. This gave me the opportunity to start using the new recommended Django development setup with PIP, Virtualenv and Virtualenvwrapper. While installing it all I kept a log to share with you guys (and girls). Follow along if you want to use this new found glory in Django development. We will install the following software; Mercurial, Git, PIP, Virtualenv, Virtualenvwrapper and Imaging Library

When everything is installed I will describe in short how we develop our Django site's at Bread & Pepper with all these tools.

XCode

Some of the above software must be compiled by hand 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 just a point and click operation.

Mercurial

Next we will need Mercurial for downloading the most recent versions of some of the Python packages. We will install Mercurial by hand so we don't have to use MacPorts[1]. After being a frequent user of MacPorts in the old days, I prefer compiling everything by hand these days.

Let's launch the Terminal from your Application folder and start with editing your .profile so it utilises your /url/local/ directory. I'm using Vim, so I will type in my terminal vim .profile 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 source directory. My source directory 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. If you get /usr/local/bin/hg than all went fine.

Git

Now let's install Git, also needed for some packages. 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 all went as it should by checking 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 project will make use of the Python Imaging library. To be able to use this with JPEG and Freetype2 you must have some extra libraries installed. Let's start with JPEG by going to your ~/Sources directory and typing the following commands[2]:

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 libjpg.

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. You will have your own worflow, 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

This will tell PIP to install all the packages listed in the django-basic-requirements.txt file and install it in your magicpony environment. 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 enter our directory and create a new Django project:

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 which points to all the specific packages needed for that 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 top of the notch 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.

Footnotes

1: Thanks to Dan Benjamin from Hivelogic for supplying the instructions on how to install Mercurial.

2: Thanks go out to Rich Atkinson's from Jetfar.com for the instructions.

13 comments

Fantastic tutorial, not sure exactly what was different but I could not get PIL working and after following this it works like a charm. Very much appreciate you taking the time to publish your companies Django dev setup for Snow Leopard as it can be a little tricky.

- Shane McCallum

19:39 on November 18, 2009

Great post, I picked up a few things here (virtualenv) that I hadn't really delved into before.

Just a few issues I saw was that this line under install PIL:

tar xzvf jpgscr.v6b.tar.gz

... should be:

tar zxvf jpegsrc.v6b.tar.gz

And under creating a virtualenv, this line:

virtualenv magicpony --no-site-packages

... should be:

virtualenv <a href="http://magicpony.com" rel="nofollow">magicpony.com</a> --no-site-packages

Cheers!

- Dana Woodman

17:32 on November 29, 2009

Oops... looks like it formatted my code strangely, what I said on the last bit is it should have the .com appended to the end of the magicpony bit.

- Dana Woodman

17:34 on November 29, 2009

Any reason why your post looks identical to this one posted a day earlier on Wunki? http://wunki.org/2009/10/01/django-development-snow-leopa...

- Shane McCallum

01:29 on December 2, 2009

@Dana Woodman: Thanks for the tips Dana. Will correct it.

@Shane McCallum: That's because wunki is my own personal site. I also placed it there.

- Petar Radosevic

00:02 on December 3, 2009

Great tutorial. Quick question: assuming I followed this tutorial and started coding magicpony.com, what would be the local URL I would use to view the results? Something like http://localhost/virtualenvs/magicpony.com?

- John Turner

05:47 on December 24, 2009

@John Turner

The virtual environment has nothing to do with the URL's that Django uses. The URL's are usually created inside your Django project in urls.py. I would advice you to take the Django tutorial to get a better understanding of the URL's created by Django.

Good luck!

- Petar Radosevic

17:28 on December 28, 2009

Thanks for this great informative step by step approach to getting virtual environments setup on OSX. Followed the steps and all works like a charm.

Probably worth mentioning that when you have your setup complete and have created a new django site you can add the files to a new Mercurial project by cd'ing into your projects directory and the at the terminal type hg init.

Then do hg status which will show you what files are not under SCM (Source Control Management). To add these files do hg add, then followed by a hg commit -m "your checkin message here".

- James Whittaker

23:21 on January 23, 2010

Installed per these directions successfully. I'm using PostgreSQL, so I added psycopg2 to my django-basic-requirements doc. Also need to use Internationalization feature, and need to add gettext. The download for gettext does not contain a setup file, causing an error message if I add it to my django-basic-requirements doc andrun the virtualenv command.

Tried installing gettext outside the django-basic-requirements doc (using install instructions) and was not able to sync db. (error msg as follows:

(ao.com)Judith-Boonstras-MacBook-Pro-15:andor judy$ python manage.py syncdb Traceback (most recent call last): File "manage.py", line 2, in <module> from django.core.management import execute_manager File "/Users/judy/Sites/virtualenvs/ao.com/src/django/django/core/management/init.py", line 3, in <module> from optparse import OptionParser, NO_DEFAULT File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/optparse.py", line 404, in <module> builtincvt = { "int" : (_parse_int, ("integer")), File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/gettext.py", line 566, in gettext return dgettext(current_domain, message) File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/gettext.py", line 530, in dgettext codeset=_localecodesets.get(domain)) File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/gettext.py", line 465, in translation mofiles = find(domain, localedir, languages, all=1) File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/gettext.py", line 437, in find for nelang in expandlang(lang): File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/gettext.py", line 131, in expandlang from locale import normalize ImportError: cannot import name normalize

Can anyone provide me some help with this?

- Judith Boonstra

21:46 on January 27, 2010

Hi Judith,

I think that you have created a 'locale' directory in your project directory with inside it an 'init.py'. This way Python sees the 'locale' directory in your Django project as a module and tries to import 'locale' from it. Of course, there is no 'locale' in that directory.

If you remove the 'init.py' from your 'ao.com/locale' directory it should work. Hope that this was the solution for you.

Good luck.

- Petar Radosevic

22:14 on January 27, 2010

Thank you Peter for the tip. Everything worked as expected after I removed 'init.py'.

- Judith Boonstra

23:22 on January 28, 2010

Merci ! Great tutorial.

- Valéry Frisch

14:43 on February 1, 2010

You're on Python 2.6.1 ? That saves a lot of headache. I was more greedy and wanted 2.6.4

- Tudor

20:51 on March 4, 2010

For markup you can use Markdown.