Mac + Virtualenv + _imaging C Module Not Installed Error

I use virtualenv now for all my local development on my Mac. I highly recommend it. But it can bring up issues. Mostly with PIL and psycopg2 connections in my experience. My most common issue with PIL is an error that says “The _imaging C module is not installed.” Below is the simple fix that works for me.

First, let me say that I installed PIL via easy_install globally. In other words I installed it while no virtualenv was active. For myself that global install of PIL is in /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PIL. There are lots of posts on how to get a good working install of PIL on MacOS so I won’t duplicate those here.

Now for the fix. First we need to find out what versions of PIL are on the virtualenv python path. Start the virtualenv. (Once started you’ll have the virtualenv name in parenthesis before your prompt in terminal)

python
import sys
print sys.path

Now go through the listing of the directories that are listed for that command. In my case an install of another module had installed PIL in my virtualenv and that install was broken. So I had 2 instances of PIL listed, one in the global directory listed above and one at ~/env/page3/lib/python2.6/site-packages/PIL-1.1.6-py2.6-macosx-10.3-i386.egg/. So how did I know which one to keep? In my case I knew I wanted the global one but you can check for the good install by going into each director and making sure there is a _imaging.so file there. In the above example the PIL install in my virtalenv did not have this file and hence the error.

Now its time to remove the offending install. Just go to the main directory that the install is located, such as site-packages in my case and if it was installed via easy_install then remove it from the easy_install.pth file.

nano easy_install.pth

Save that and exit and then remove the egg.

rm -rf PIL-1.1.6-py2.6-macosx-10.3-i386.egg/

You should be ready to roll!


Comments