Snow Leopard,Homebrew python 2.7,virtualenv,libjpeg和PIL =导入错误

时间:2011-03-13 03:36:34

标签: python macos osx-snow-leopard python-imaging-library libjpeg

这个问题在StackOverflow和网络上有很多排列。我尝试了很多东西,没有什么对我有用。

这是我的设置。我正在运行OS X 10.6。我用Homebrew来安装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

注意,在Activity Monitor中,这被报告为64位进程。

首先我尝试用Homebrew安装libjpeg。后来我卸载它并通过下载源将其安装到/ usr / local,如下所示:

export CC="/usr/bin/gcc -arch x86_64"
./configure --enable-shared --enable-static
make
sudo make install

然后,在我的Django virtualenv中,我重新安装了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

然而,它不起作用:

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

3 个答案:

答案 0 :(得分:5)

byp是你的虚拟人吗?我只是做了同样的事情,虽然使用系统Python 2.6并没有问题。如果需要,您可以将libjpeg直接安装到virtualenv中,然后您就不必破解PIL构建脚本。这是我做的:

% 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

虽然为_imaging.soi386ppc构建了x86_64(因为这是Apple编译Python的方式),但为{libjpeg编译了x86_64 1}}只是因为我给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)

由于_imaging.so动态链接到libjpeg,如果dylib不适用于预期的体系结构,则会在加载时失败。我能够通过运行Python作为32位来激发类似于你所看到的东西:

(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

所以我要检查Python,PIL和libjpeg的架构是否兼容。虽然你没有得到更好的错误信息,但这很烦人!

答案 1 :(得分:1)

我试图解决这个问题几个小时,唯一的办法就是使用这个包:

http://ethan.tira-thompson.org/Mac_OS_X_Ports.html

安装libjpeg,然后PIL将从pip安装并正常工作,我猜其他来源也是如此。

答案 2 :(得分:0)

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