2

在 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

请注意,在活动监视器中,这被报告为 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
4

3 回答 3

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

虽然是为, and_imaging.so构建的(因为这是编译 Apple 的 Python 的方式),但仅因为我给 gcc 的标志而编译:i386ppcx86_64libjpegx86_64

(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 的架构是否兼容。不过,您没有收到更好的错误消息,这很烦人!

于 2011-03-13T04:03:12.060 回答
1

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

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

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

于 2011-06-29T12:54:12.490 回答
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
于 2013-10-26T05:05:18.403 回答