Tag Archives: Django

Fix the dreaded “Not an image file” MySQL Django error: use VirtualEnv

If you develop Django using MySQL on a Mac, you’ve undoubtedly run into the “Not an image file” error for models with image fields. The problem has to do with JPEG support for Mac OS and how it interacts with Python Imaging Library. Diabolically, once you’ve installed PIL with bad JPEG support it seems impossible to fix.

My solution is to use a virtualenv. virtualenv is a python tool to create an isolated Python interpreter on your system, which allows you to control the installed packages without mussing up your main Python installation. It’s good practice to use a separate virtualenv for each project, and if you use Eclipse you usually want one for each workspace

There are, of course, other solutions to this problem (using PostgreSQL is probably one of them). The instructions here are based on this post on DjangoDays.com.


You’ll need to have xCode installed. Also, if you want to use Eclipse, vlku.com has an excellent screencast that covers everything.

Create a “workspace”

The best place to keep the virtualenv for your Django project is right next to your Django code. Create a directory to hold both. This can be just a plain directory, but if you’re using Eclipse make a new workspace first and complete the following steps in that workspace. If you have existing code, drop it in the root of this folder. For this tutorial let’s call the folder “myworkspace” and your project “myproject”.

Set up a Virtual Environment with –no-site-packages

Use easy_install to get virtualenv.

easy_install virtualenv

Change into the ‘myworkspace’ directory and create a new virtualenv. The key here is to use the –no-site-packages argument, which creates a Python that ignores the system packages already installed. We don’t want anything to come over from the system Python in case you’ve already installed a useless PIL.

python virtualenv.py python --no-site-packages

You now have a fresh, Django- and PIL-less Python interpreter at myworkspace/python/bin/python. The directory structure should now be as follows:

- myworkspace
+ - myproject
+ - python

Install libjpeg (JPEG Support) system wide

Here’s the key to fixing the problem, and where the DjangoDays.com post really saved my butt. Forget the virtualenv for a second, we’re installing JPEG support system wide.

  1. Download JPEG support. This little sucker keeps moving around the internet. I used version 6, which is still available here. The project is maintained my the Independent JPEG Group, who are showing version 8 as of this posting.
  2. Extract the tarball, cd into the resulting folder (in my case, it was called jpeg-6b, and install it with these changes.
    cd jpeg-6b
    cp /usr/share/libtool/config.sub .
    cp /usr/share/libtool/config.guess .
    ./configure –enable-shared
    sudo mkdir -p /usr/local/include
    sudo mkdir -p /usr/local/bin
    sudo mkdir -p /usr/local/lib
    sudo mkdir -p /usr/local/man/man1
    sudo make install

Thanks DjangoDays.com.

Install PIL

Now we’ll install PIL and point it to the correctly installed JPEG Support. This is again lifted from the DjangoDays.com post.

  1. Download the PIL tarball here.
  2. Extract it, cd into the resulting folder, and edit setup.py to change these lines:
    JPEG_ROOT = none
    ZLIB_ROOT = none

    to this:

    JPEG_ROOT = “/usr/local/include”
    ZLIB_ROOT = “/usr/local/include”
  3. Install PIL with these commands:
    python setup.py build_ext -i
    python selftest.py
    sudo python setup.py install

I got 9 errors when I ran selftest.py, but kept going and haven’t seen any indication that the errors are giving me any trouble. Again, thanks DjangoDays.com.

Install Django, MySQL-python, etc.

Here’s a post a did a last year detailing Django installation on Mac OS X. That is, again, not the only way of doing it. The key here is to use the Python interpreter you just created to install Django and all it’s prereqs. You also have a virtualenv specific easy_install. Use it to install any other dependencies.


Run your project

I’m skipping a lot of steps (and debugging) here, but assuming you’ve got your project set up correctly you should now be able to cd into the ‘myproject’ directory and run the server with the following command:

../python/bin/python manage.py runserver

As a shortcut, you can set an environment variable in the ‘myworkspace’ directory to map the command ‘python’ to /myworkspace/python/bin/python. Better yet, if you’re using Eclipse, use that Python as you’re interpreter, and configure the debugger to run the command for you.

Accessing the new model instance with Django’s “post_save” signal

I’d been wracking my brain for a few hours before I finally went back to the Django signals documentation and looked up how to get to the newly created instance with Django’s ‘post_save’ signal. I’m not sure if johnboxall’s django-paypal signal example is out of date or simply erroneous, but it’s not the right way to do it. I’d expected the sender itself to be the new instance. Instead, this:

def notify_on_payment(sender, **kwargs):
  print "We got it"
  ipn_obj = sender
  print ipn_obj

post_save.connect(notify_on_payment, sender=PayPalIPN)

got me this:

We got it
<class 'paypal.standard.ipn.models.PayPalIPN'>

…plus a big fat error page in the browser.

Note: I couldn’t get the payment_was_successful signal to work at all, so I rolled my own.

The <class> bit is NOT the newly created instance, but the class itself. Django did not like me trying to access the ‘id’ of a Model class.

Long story short, the actual instance is passed as a keyword argument. Here’s how to get to it:

def notify_on_payment(sender, **kwargs):
  print "We got it"
  ipn_obj = kwargs['instance']
  print ipn_obj

Here’s the output:

We got it
<IPN: Recurring > #This is the get_display for this particular Model

Much better :). I can now access fields on that instance using dot notation.

I’d be interested in knowing whether the example on johnboxall’s page is outdated or wrong. It would seem reasonable to me for the sender to be the newly created instance to send the signal. I am, however, usually wrong about these things. Any ideas?

Getting django-syncr running on WebFaction

I’m working on a Django installation at WebFaction that will be needing the special magnificence that is django-syncr. It’s got a few other dependencies, so I thought I’d record the steps I needed to take to get it up and running.


You don’t need this unless you’re using Python 2.4 or earlier, but if you do: http://effbot.org/zone/element-index.htm


Get it here: http://code.google.com/p/django-tagging/

OR…. just easy_install it :). I’m on Python 2.5, so SSH into your server and:

easy_install-2.5 django-tagging

God bless setuptools.


If you want to get your Twitter feed into your Django database, you’re gonna need python-twitter. And for python-twitter, you’re gonna need simplejson. Let’s get that first. If you’re still SSH’d in:

easy_install-2.5 simplejson

Now for python-twitter:

easy_install-2.5 python-twitter

Do you want the Flickr support?

Flickr won’t release the actual photo or allow it to be embedded using the API (as far as I can tell). I don’t really see any need for all the Flickr metadata to live in my database without the image, so I’m skipping it.

Installing django-syncr itself

django-syncr isn’t in PyPI yet, so we’ll have to download and install it the old-fashioned setuptools way:

cd ~/webapps/djangoapp/
mkdir src
cd src
svn checkout http://django-syncr.googlecode.com/svn/trunk/ syncr
cd ..
easy_install-2.5 src/syncr

I pulled up out of the ‘src’ directory so easy_install would no exactly what ‘syncr’ I was talking about.

Including django-syncr in your Installed Apps

You’ll need to edit your settings.py file to tell django you’d like to use the new apps you just easy_installed. Get to your settings.py file and open it with your favorite editor:

cd ~/webapps/djangoapp/myproject/
emacs settings.py

Added the following lines (between the ellipses) to your ‘INSTALLED_APPS’:


In my particular case, I’ve added it after a bunch of Satchmo stuff but before my own app.

Syndb and restart

That’s it for the tricky stuff. If you’ve done everything right you should be able to sync the database no problem (now is a great time to back up your data, by the way):

python2.5 manage.py syncdb

Then restart your server…


and check your admin site for the new tables.

Installing Satchmo on Snow Leopard

Django is my framework of choice for non-CMS web applications, and that makes Satchmo my choice for eCommerce. It’s a great library, even if it does have a million dependencies. On the bright side, I’ve used many of it’s dependencies in other projects, so look at installing these as a chance to learn more about Python.

This tutorial is based on the excellent instructions at the Satchmo Project site.

Installing Dependencies

If you’re going to use Satchmo, you’ve of course got to have Django. See my Django installation tutorial.

Remember how I said that I like to keep Libraries in my user folder? Here’s my first official renege. I don’t want to keep up with all these dependencies, so I’m going to allow easy_install to install these libraries in my Python 2.6 SITE-PACKAGES whenever possible.

Installing easy_install

Here are the official instructions, written for the slightly more advanced user. Here’s what I did:

  1. Download the SetupTools Python egg for our version of Python (2.6, the default on Snow Leopard)
  2. Run the egg as a Shell script with the following Terminal command:
    sudo sh /Users/kevin/Downloads/setuptools-0.6c11-py2.6.egg
  3. Type in your password when your asked for it

That’s it. easy_install is installed as a part of setuptools, and you can run it directly from Terminal by typing ‘easy_install’

Installing the other dependencies

These are generally straightforward with easy_install. Use the command below the name of each library to install. Notes included where I ran into trouble.


easy_install pycrypto


There’s a link to the 2.5 egg on the Satchmo install site, but that doesn’t help us much. Here’s how to get ReportLab for Python 2.6 (you’ll need GCC, which is a part of XCode):

  1. Download the latest ReportLab tarball from the ReportLab site.
  2. Move the tarball into your user folder and un-tar it
  3. cd
    mv Downloads/ReportLab_2_3.tar.gz ReportLab_2_3.tar.gz
    tar xzvf ReportLab_2_3.tar.gz
  4. Move into the new directory and type the following command to install it:
  5. cd ReportLab_2_3
    python setup.py install

After running this I got the following error:

########## SUMMARY INFO #########
#Attempting install of _rl_accel, sgmlop & pyHnj
#extensions from '/Users/kevin/ReportLab_2_3/src/rl_addons/rl_accel'
#Attempting install of _renderPM
#extensions from '/Users/kevin/ReportLab_2_3/src/rl_addons/renderPM'
# installing without freetype no ttf, sorry!

Not sure what’s going on here, but I’ll ignore this for now.


easy_install http://www.satchmoproject.com/snapshots/trml2pdf-1.2.tar.gz


easy_install django-registration


easy_install PyYAML

Python Imaging Library (PIL)

sudo easy_install --find-links=http://download.zope.org/distribution PILwoTk


  1. Download the django-threaded-multihost tarball from GoSatchmo
  2. Move the tarball into your user folder and un-tar it
  3. cd
    mv Downloads/django-threaded-multihost-1.3-2.tar.gz django-threaded-multihost-1.3-2.tar.gz
    tar xzvf django-threaded-multihost-1.3-2.tar.gz
  4. Move into the new directory and type the following command to install it:
  5. cd django-threaded-multihost-1.3-2
    python setup.py install


  1. This one’s a little funky. We’re going to need a place for this to live. Make a new directory ‘/Users/kevin/src/django/plugins/’
    mkdir ~/src/django/plugins
  2. Move to the new directory and checkout django-app-plugins from Subversion
  3. cd ~/src/django/plugins
    svn checkout http://django-app-plugins.googlecode.com/svn/trunk/ django-app-plugins-read-only
  4. Create a symbolic link to the SITE-PACKAGES directory from django-app-plugins/app_plugins
    cd /Library/Python/2.6/site-packages/
    ln -s /Users/kevin/src/django/plugins/django-app-plugins-read-only/app_plugins .


This package is kept in a Mercurial repository. You’ll have to download and install Mercurial. Here’s the Mercurial Installer for Snow Leopard.

  1. Move to your newly created ‘plugins’ directory and checkout SORL-Thumbnail with Mercurial
    cd ~/src/django/plugins
    hg clone https://sorl-thumbnail.googlecode.com/hg/ sorl-thumbnail
  2. Create a symbolic link to your SITE-PACKAGES directory for SORL-Thumbnail
    cd /Library/Python/2.6/site-packages/
    ln -s /Users/kevin/src/django/plugins/sorl-thumbnail/sorl .


  1. Move to your newly created ‘plugins’ directory and checkout signals-ahoy with Mercurial
    cd ~/src/django/plugins
    hg clone http://bitbucket.org/bkroeze/django-signals-ahoy/
  2. Create a symbolic link to your SITE-PACKAGES directory for django-signals-ahoy/signals-ahoy
    cd /Library/Python/2.6/site-packages/
    ln -s /Users/kevin/src/django/plugins/django-signals-ahoy/signals_ahoy .


sudo easy_install sphinx


sudo easy_install docutils

Installing Satchmo

Still with me? I want to install Satchmo the same way I installed Django. I’ll be installing it in it’s own directory in ‘src’, though it is technically a plugin. I might be using different versions of Satchmo in the future. I also want to be able to browse the source without too much trouble.

  1. Checkout Satchmo into your ‘src’ directory
    cd ~/src
    mdkir satchmo
    cd satchmo
    hg clone http://bitbucket.org/chris1610/satchmo/ satchmo-trunk
  2. Add ‘satchmo-trunk/satchmo/apps’ to your Python path. I did this by adding it to my .bash_profile. Either edit or create yours, in the root your user directory, to include this line:
    export PYTHONPATH="$PYTHONPATH:/Users/kevin/src/satchmo/satchmo-trunk/satchmo/apps"

At this point, Satchmo is installed. You should be able to import Satchmo from the Python 2.6 interpreter:

>>> import satchmo_store

… and you can now run pre-exisiting Satchmo projects on your iMac.

Installing Django (1.1) and MySQL on Snow Leopard

A few days ago I posted my misadventures in setting up a Plone 3 buildout on Snow Leopard. That (fingers crossed) was a lot more complicated than getting Django going. However, I’ll also be installing MySQL (my database of choice) as a backend database. I’ll be foregoing Apache and mod_wsgi, as this is just a development instance.

Most of this is based on the excellent instructions available at the Django site

Working With What We’ve Got

The Macintosh Operating System, being the forward thinking platform that it is, already has Python 2.5 and 2.6 installed. Python 2.6 is the default (it’s the interpreter that starts when you type ‘python’ into the Terminal), and we’ll be sticking with that. It also includes Subversion, which we’ll use to download and update the Django trunk.

Where To Put Django

As my coding style has developed, I’ve gravitated more toward keeping Python libraries that need close management in my ‘home’ folder (/Users/kevin/ on my machine) and using symbolic links to get it on my Python path. It appears to me that everything installed under ‘/Library/’ is laid out quite nicely without any of my assistance, thank you very much.

Besides not wanting to play around with the system folders, there are a couple advantages to installing like this. First, I’ll want to update my Django installation as new code comes out, and it’s simpler to find stuff in your home folder. Second, keeping packages like Django in your home folder makes browsing the source code (a must in Open Source) much simpler.

You’ll see me use easy_install to get stuff for Pythons 2.5 and 2.6, but as a general rule if I have to manually install it it’s going in my home folder. For an alternative approach (i.e. installing Django in the local site-packages directory) see this post.

Getting MySQL

To paraphrase Billy Bob Thornton from The Bad News Bears, developing in Django without a database is kind of like kissing your sister. Let’s install MySQL.

There are lots of ways to install MySQL, including compiling and installing from source. As this is a Django development machine, I feel comfortable simply using an installer package. To my surprise, MySQL as of today doesn’t have a specific 10.6 installer package on their downloads page. No matter, we’ll just grab the 64-bit version for Mac OS X 5.

  1. Go to http://dev.mysql.com/downloads/mysql/5.5.html#downloads (for MySQL 5.5, the most current recommended version)
  2. Find the “Mac OS X 10.5 (x86_64)” under “Mac OS (package format)” and click “Pick a mirror”
  3. MySQL/Sun Microsystems will want to know everything about you. Go ahead and register.
  4. Pick a site, download the package, and double click to open the package
  5. Click “mysql-5.5.0-m2-osx10.5-x86_64.pkg” to begin the install
  6. OPTIONAL: click “mySQLStartupItem.pkg” to have MySQL start when you start your system
  7. Head’s Up!

    I don’t want MySQL running all the time, so I’m not going to install the MySQL Startup Item. That means that I’ll have to START MYSQL FROM TERMINAL EVERY TIME I WANT TO USE IT. I’ll show you how to make this simple a bit further down.

  8. OPTIONAL: click “MySQL.prefPane” to install a Preference Pane in the System Preference for MySQL (I didn’t do this either)
  9. Using your favorite text editor, create a new document called “.bash_profile” in your home directory (yes, the dot is supposed to be there). We’re going to add the MySQL directory to the PATH, allowing you to run MySQL commands by typing one string into the terminal (not the entire path). Add the following line to “.bash_profile” and save it.
  10. export PATH="$PATH:/usr/local/mysql/bin"
  11. You’ll need a root password for MySQL. Type the following command into Terminal, replacing “superduper” with a password you’ll remember
  12. mysqladmin -u root password superduper

To start MySQL, type the following into your Terminal

sudo mysqld_safe

You’ll be prompted for your system password. Enter it and MySQL will start running. You’ll need to leave a Terminal tab open to leaveit running. To stop MySQL, type Control+Z from the Terminal tab it’s running from. Now for Django itself.

Installing MySQLdb

MySQLdb is the Python library that let’s us manipulate a MySQLdb with Python code.

  1. Download the MySQLdb tarball from SourceForge.net
  2. Move the tarball to your user directory and un-tar it
    mv Downloads/MySQL-python-1.2.3c1.tar.gz MySQL-python-1.2.3c1.tar.gz
    tar xzvf MySQL-python-1.2.3c1.tar.gz
  3. Change into the directory your just created and install the library
    cd MySQL-python-1.2.3c1
    python setup.py install

Installing Django 1.1

Most of the Django sites I’ve installed at at version 1.1, but not all. I’ll also probably want to run the development version at some point as well. I’m going to set up the directories around Django with that in mind.

  1. Open Terminal, and create the following directory structure (some of which will already exist): /Users/kevin/src/django/core/. Working in our home directory…
    mkdir src
    cd src
    mkdir django
    cd django
    mkdir core
    cd core
  2. Download the latest stable version of Django 1.1 (it’s a tar.gz file, AKA a ‘tarball’)
  3. Unpack the Django tarball into the directory you created in step 1. Working in the ‘core’ directory…
    tar xzvf /Users/kevin/Downloads/Django-1.1.1.tar.gz
  4. Change into the directory you created in step 3
    cd Django-1.1.1
  5. Here is where I start deviating a little from the norm. I want to keep this code in my home directory, so I’m going to create a symbolic link to my Python SITE-PACKAGES directory that points to this version of Django. First, find your Site-Packages directory by typing this into Terminal
    python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"

    In my case, this returned:


    So, to make the symbolic link (from within the Django-1.1.1 directory), I type:

    ln -s $PWD/django /Library/Python/2.6/site-packages/django

    (change the path to match your own SITE-PACKAGES directory)

  6. I’m going to want django-admin.py in my path (and you will too). Create a symbolic link to /usr/local/bin:
    sudo ln -s /Users/kevin/src/django/core/Django-1.1.1/django/bin/django-admin.py /usr/local/bin
  7. Check that you’ve installed Django correctly by starting the Python Interpreter (type ‘python’ into the command line) and try importing Django
    >>> import django

    No errors = awesomeness

This method installing Django 1.1.1 as my default Django (the Django I get when I type ‘django’ into a Python Interpreter). If you wanted to simultaneously run a different version of Django, repeat this process with the tarball for that version (or checkout the trunk from Subversion). When you create the symbolic link, name it something other than ‘django’ (i.e. ‘django-1.1.1’ or ‘django-trunk’).