在OS X Lion上安装PIL

5

我试图在Mac OSX 10.7.4上安装PIL,但经过几个小时的尝试仍未成功。一直遇到相同的问题,详情请见下面的pastebin链接。请帮忙解决!

设置

tbc:~ mystic$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python

Pastebin

我从几个来源中搜索和尝试了:

Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import Image
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named Image
>>> import PIL
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named PIL
>>>

根据 jdi 的建议进行了编辑,但仍然遇到了与上面的 pastebin 链接中显示的相同问题。

tbc:jpeg-8c mystic$ xcode-select -version
xcode-select version 2003.
tbc:jpeg-8c mystic$ which xcode-select
/usr/bin/xcode-select
tbc:jpeg-8c mystic$ xcode-select -print-path
/Applications/Xcode.app/Contents/Developer
tbc:jpeg-8c mystic$

同时,我按照"the paul"的建议尝试使用Homebrew。

tbc:cellar mystic$ brew link jpeg
Linking /usr/local/Cellar/jpeg/8d... 3 symlinks created
tbc:cellar mystic$ brew install pil
Error: pil-1.1.7 already installed
tbc:cellar mystic$ brew uninstall pil
Uninstalling /usr/local/Cellar/pil/1.1.7...
tbc:cellar mystic$ brew install pil
==> Downloading http://effbot.org/downloads/Imaging-1.1.7.tar.gz
Already downloaded: /Library/Caches/Homebrew/pil-1.1.7.tar.gz
==> python setup.py build_ext
==> python setup.py install --prefix=/usr/local/Cellar/pil/1.1.7
==> Caveats
This formula installs PIL against whatever Python is first in your path.
This Python needs to have either setuptools or distribute installed or
the build will fail.
Warning: Non-executables were installed to "bin".
Installing non-executables to "bin" is bad practice.
The offending files are:
/usr/local/Cellar/pil/1.1.7/bin/pilfont.py
==> Summary
/usr/local/Cellar/pil/1.1.7: 174 files, 2.0M, built in 15 seconds
tbc:cellar mystic$ python 
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pil
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named pil
>>> from PIL import Image
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named PIL

这里是.pip的日志记录。
它在抱怨以下问题:
/Developer/SDKs/MacOSX10.6.sdk/usr/include/stdarg.h:4:25: error: stdarg.h: No such file or directory

但是 stdarg.h 文件与同一位置。我不明白为什么它试图在MacOSX10.6.sdk中定位,因为当前版本是 MacOSX10.7.sdk。真让人发疯!!!

看起来好像成功了。尝试打开一个新的Python控制台,然后输入“import Image”。如果没有出现错误,你就可以开始了。 - Nick Craig-Wood
我已经更新了问题并在pastebin链接中提供了详细信息。 - thchand
看起来/Developer/SDKs/MacOSX10.6.sdk出了问题。你的硬盘上有它吗? - chl
对你来说,这个是否存在?/Developer/SDKs/MacOSX10.6.sdk。在最新的XCODE中的Lion中,他们移动了SDK的位置。 - jdi
是的,我在Developer文件夹中有MacOSX10.6.sdk和MacOSX10.7.sdk两个。 - thchand
3个回答

9
如果您正在使用最新的XCode,并且在Lion上,则可能遇到的问题是他们移动了开发人员SDK的位置。期望它们位于/Developer/中的包将不再按需找到它们。
关于细节,请参考这篇文章:
http://www.agile-workers.com/web/2012/03/qt-qmake-osx_sdk-xcode/ 但总的来说,您可能需要运行:
sudo /usr/bin/xcode-select -switch /Applications/Xcode.app/Contents/Developer
以指向新位置。
手动修复可能只需将SDK从/Applications/Xcode.app/Contents/Developer/...链接到/Developer/...,但我建议先尝试xcode-select工具。
更新:根据您的最新更新,以下是一些建议。

首先,根据您发布的内容,我无法确定PIL是否由于SDK而无法构建。如果它已经成功构建,并且您正在使用标准的Apple Python,则很有可能Homebrew Python路径不在您自己的Python路径中。

$ python
>>> import sys
>>> sys.path.append("/usr/local/lib/python2.7/site-packages")
>>> from PIL import Image

如果仍然出现错误,那么我认为问题仍然与缺少SDK位置有关。你可以使用以下命令将SDK的新位置链接到旧位置:

mkdir -p -v /Developer/SDKs
ln -s /Applications/Xcode.app/Contents/Developer/MacOSX10.6.sdk  /Developer/SDKs/MacOSX10.6.sdk

或者,您可以在构建PIL时明确设置sdk路径:
brew remove pil
export SDKROOT=/Applications/Xcode.app/Contents/Developer/MacOSX10.6.sdk
brew install pil

他的MacOSX10.7.sdk /Developer 下,所以可能他还没有更新xcode。 - the paul
我已经根据您的建议编辑了问题,但仍然存在相同的问题。 - thchand
我想我从某个博客中遵循了错误的建议,并从 /usr/bin 中删除了 gcc-4.2。出现错误“无法执行gcc-4.2:没有这样的文件或目录”。再次尝试通过 >>> brew install https://raw.github.com/gist/1587643/gcc42.rb 进行安装,但没有成功。 - thchand
@thchand:看看我在这个非常相似的安装选项问题上的答案:http://stackoverflow.com/questions/10658909/unable-to-install-with-easy-install-or-pip-on-mac/10659066 - jdi
@jdi - 你太棒了,按照你的建议解决了问题。我花了三天时间才找到这个问题。 - thchand
显示剩余5条评论

1

错误:在安装PIL之前,您必须先执行“brew link jpeg”。 - thchand
是的,错误:无法建立符号链接文件:/usr/local/Cellar/jpeg/8d/bin/wrjpgcom - thchand
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/11443/discussion-between-the-paul-and-thchand - the paul

0
如果您已经安装了Homebrew,那么这将完成工作:
brew install pillow

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接