这个问题在 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
最佳答案
byp
是你的 virtualenv 吗?我只是做了同样的事情,尽管使用的是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.so
是为 i386
、ppc
和 x86_64
构建的(因为 Apple 的 Python 就是这样构建的)已编译),libjpeg
是为 x86_64
编译的,只是因为我给了 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 在预期架构中不可用,它会在加载时失败。我能够通过以 32 位运行 Python 来引发与您所看到的类似的结果:
(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的架构是否兼容。不过,您没有收到更好的错误消息,这很烦人!
关于python - Snow Leopard、Homebrew python 2.7、virtualenv、libjpeg 和 PIL = 导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5287231/