— Go straight to page navigation

11. October 2010

Building PIL against MacPorts on Snow Leopard

I spent hours getting this to work so I thought it best to share it, esp. since so many others seem to be having the _jpeg_resync_to_restart error. The problem affected me in the following way: PIL depends on libjpeg, which was present on my system but failing to load and caused import errors the _imaging package.

>>> from PIL import _imaging
Traceback (most recent call last):
File "", line 1, in
ImportError: dlopen(/Library/Python/2.6/site-packages/PIL/, ↩
  2): Symbol not found: _jpeg_resync_to_restart
Referenced from: /Library/Python/2.6/site-packages/PIL/
Expected in: flat namespace in /Library/Python/2.6/site-packages/PIL/

After flailing about for a while, I had discovered that I could get the thing to compile and work fine on Python 2.6, but not Python 2.5 (which I need). The significant difference between these with regard to libjpeg is that 2.5 runs in 32bit while 2.6 runs in 64bit. This was confirmed by doing this:

>> import platform
>>> platform.architecture()
('32bit', '')

I spent what seemed like en eternity trying out possible combinations of gcc, -arch flags, Imaging and libjpeg builds. What finally worked for me was to build against the MacPorts installation of libjpeg.

The process

This assumes you have MacPorts installed. If not, then I guess you are some sort of developer god who wouldn't need my help with trivial matters such as this one.

  1. Make sure there are universal binaries of libjpeg. I made sure I had both versions available (just in case):

    sudo port install jpeg +universal
    sudo port install jpeg6b +universal
  2. Get Imaging (1.1.7 is the latest when this is written):

    curl -C - -O
    tar xzf Imaging-1.1.7.tar.gz
  3. Direct the Imaging build process to the libraries:I did this by editing Imaging-1.1.7/ to make sure JPEG_ROOT was set like this:

    JPEG_ROOT = "/opt/local"
  4. Make sure you are building for all architectures:

    export CC="gcc -arch i386 -arch x86_64 -arch ppc"
  5. Build and install Imaging. I needed this to work for for 2.5 so I did it this way:

    cd Imaging-1.1.7
    python2.5 build
    sudo python2.5 install --optimize=1

    You may want to do it in an active virtualenv or simply for your normal Python install:

    cd Imaging-1.1.7
    python build
    sudo python install --optimize=1

    You may have to do this step for any (virtual or otherwise) environment you work in to ensure that PIL gets installed in site-packages for every environment you need it in.

And that's all I know. If this doesn't work for you then please don't ask me for help because I am simply not qualified to provide it.

Published: 11. October 2010. Tagged: , , , .