Snow Leopard, Homebrew python 2.7, virtualenv, libjpeg and PIL = import error
There are a ton of permutations of this question on StackOverflow and the web in general. I've tried a lot of things and nothing works for me.
Here's my setup. I'm running OS X 10.6. I've use开发者_C百科d Homebrew to install Python 2.7.1:
$ python
Python 2.7.1 (r271:86832, Mar 12 2011, 16:21:44)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Note, in Activity Monitor this is reported as a 64-bit process.
First I tried installing libjpeg with Homebrew. Later I uninstalled it and have been installing it to /usr/local by downloading the source, like so:
export CC="/usr/bin/gcc -arch x86_64"
./configure --enable-shared --enable-static
make
sudo make install
Then, in my Django virtualenv, I reinstall PIL:
export CC="/usr/bin/gcc -arch x86_64"
pip uninstall PIL
pip install --no-install PIL
vi build/PIL/setup.py # change JPEG_ROOT to libinclude("/usr/local")
pip install PIL
And still, it doesn't work:
>>> import _imaging
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dlopen(/Users/me/byp/lib/python2.7/site-packages/PIL/_imaging.so, 2): Symbol not found: _jpeg_resync_to_restart
Referenced from: /Users/me/byp/lib/python2.7/site-packages/PIL/_imaging.so
Expected in: dynamic lookup
byp
is your virtualenv? I just did the same thing, though with the system Python 2.6 and had no problems. You can install libjpeg
directly into the virtualenv if you want, then you won't have to hack the PIL build script. Here's what I did:
% mkvirtualenv foo
(foo)% cdvirtualenv
(foo)% lftpget http://www.ijg.org/files/jpegsrc.v8c.tar.gz
(foo)% tar zxf jpegsrc.v8c.tar.gz
(foo)% cd jpeg-8c
(foo)% CC="gcc -arch x86_64" ./configure --prefix=$VIRTUAL_ENV
(foo)% make install
(foo)% cd ..
(foo)% pip install PIL
(foo)% python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import _imaging
>>> _imaging
<module '_imaging' from '/Users/nicholas/LMI/foo/lib/python2.6/site-packages/PIL/_imaging.so'>
>>> ^D
While _imaging.so
got built for i386
, ppc
and x86_64
(because that is how Apple's Python was compiled), libjpeg
was compiled for x86_64
only because of the flags I gave gcc:
(foo)% file lib/libjpeg.8.dylib
lib/libjpeg.8.dylib: Mach-O 64-bit dynamically linked shared library x86_64
(foo)% file lib/python2.6/site-packages/PIL/_imaging.so
lib/python2.6/site-packages/PIL/_imaging.so: Mach-O universal binary with 3 architectures
lib/python2.6/site-packages/PIL/_imaging.so (for architecture i386): Mach-O bundle i386
lib/python2.6/site-packages/PIL/_imaging.so (for architecture ppc7400): Mach-O bundle ppc
lib/python2.6/site-packages/PIL/_imaging.so (for architecture x86_64): Mach-O 64-bit bundle x86_64
(foo)% otool -L lib/python2.6/site-packages/PIL/_imaging.so
lib/python2.6/site-packages/PIL/_imaging.so:
/Users/nicholas/LMI/foo/lib/libjpeg.8.dylib (compatibility version 12.0.0, current version 12.0.0)
/opt/local/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.5)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.1)
Because _imaging.so
links to libjpeg
dynamically, it fails at load time if a dylib is not available with the expected architecture. I was able to provoke something similar to what you saw by running Python as 32-bit:
(foo)% arch -i386 python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import _imaging
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dlopen(/Users/nicholas/LMI/foo/lib/python2.6/site-packages/PIL/_imaging.so, 2): Symbol not found: _jpeg_resync_to_restart
Referenced from: /Users/nicholas/LMI/foo/lib/python2.6/site-packages/PIL/_imaging.so
Expected in: flat namespace
in /Users/nicholas/LMI/foo/lib/python2.6/site-packages/PIL/_imaging.so
So I'd check that the architectures of Python, PIL and libjpeg are compatible. It's rather annoying you don't get a better error message though!
I was trying to solve this issue for hours, the only way, was using this package:
http://ethan.tira-thompson.org/Mac_OS_X_Ports.html
to install libjpeg, then PIL will install and work properly from pip and i guess other sources as well.
mkvirtualenv project-env
workon project-env
brew install samueljohn/python/pillow
pip install --upgrade --no-install Pillow
sed -i '.bak' 's?JPEG_ROOT = None?JPEG_ROOT="\/usr\/local\/Cellar\/jpeg\/8c\/"?' $VIRTUAL_ENV/build/Pillow/setup.py
sed -i '.bak' 's/TIFF_ROOT = None/TIFF_ROOT="\/usr\/local\/Cellar\/libtiff\/4.0.3\/"/' $VIRTUAL_ENV/build/Pillow/setup.py
sed -i '.bak' 's/FREETYPE_ROOT = None/FREETYPE_ROOT="\/usr\/local\/Cellar\/freetype\/2.5.0.1\/"/' $VIRTUAL_ENV/build/Pillow/setup.py
sed -i '.bak' 's/LCMS_ROOT = None/LCMS_ROOT="\/usr\/local\/Cellar\/little-cms\/1.19\/"/' $VIRTUAL_ENV/build/Pillow/setup.py
cd $VIRTUAL_ENV/build/Pillow/ && python setup.py install
精彩评论