OSX: 安装python包时出错

0 投票
1 回答
2999 浏览
提问于 2025-04-17 14:54

目前,我无法安装任何Python包,因为出现了gcc-4.0的错误。我会复制两个错误示例(一个是用pip,另一个是手动用setup.py构建/安装的方式):

sgarza62$ pip install PIL
Downloading/unpacking PIL
  Running setup.py egg_info for package PIL
    WARNING: '' not a valid package name; please use only.-separated package names in setup.py

Installing collected packages: PIL
  Running setup.py install for PIL
    WARNING: '' not a valid package name; please use only.-separated package names in setup.py
    --- using frameworks at /System/Library/Frameworks
    building '_imaging' extension
    gcc-4.0 -fno-strict-aliasing -fno-common -dynamic -arch ppc -arch i386 -g -O2 -DNDEBUG -g -O3 -DHAVE_LIBZ -I/System/Library/Frameworks/Tcl.framework/Headers -I/System/Library/Frameworks/Tk.framework/Headers -IlibImaging -I/Library/Frameworks/Python.framework/Versions/2.7/include -I/usr/include -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _imaging.c -o build/temp.macosx-10.3-fat-2.7/_imaging.o
    unable to execute gcc-4.0: No such file or directory
    error: command 'gcc-4.0' failed with exit status 1
    Complete output from command /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python -c "import setuptools;__file__='/var/folders/9q/bvqtzkbx1hg1934b36zgk0y40000gp/T/pip-build/PIL/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/9q/bvqtzkbx1hg1934b36zgk0y40000gp/T/pip-nV9YEx-record/install-record.txt --single-version-externally-managed:
    WARNING: '' not a valid package name; please use only.-separated package names in setup.py

running install

running build

running build_py

running build_ext

--- using frameworks at /System/Library/Frameworks

building '_imaging' extension

gcc-4.0 -fno-strict-aliasing -fno-common -dynamic -arch ppc -arch i386 -g -O2 -DNDEBUG -g -O3 -DHAVE_LIBZ -I/System/Library/Frameworks/Tcl.framework/Headers -I/System/Library/Frameworks/Tk.framework/Headers -IlibImaging -I/Library/Frameworks/Python.framework/Versions/2.7/include -I/usr/include -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _imaging.c -o build/temp.macosx-10.3-fat-2.7/_imaging.o

unable to execute gcc-4.0: No such file or directory

error: command 'gcc-4.0' failed with exit status 1

sgarza62$ cd Imaging-1.1.7
sgarza62$ sudo python setup.py build
running build
running build_py
running build_ext
--- using frameworks at /System/Library/Frameworks
building '_imaging' extension
gcc-4.0 -fno-strict-aliasing -fno-common -dynamic -arch ppc -arch i386 -g -O2 -DNDEBUG -g -O3 -DHAVE_LIBZ -I/System/Library/Frameworks/Tcl.framework/Headers -I/System/Library/Frameworks/Tk.framework/Headers -IlibImaging -I/Library/Frameworks/Python.framework/Versions/2.7/include -I/usr/include -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _imaging.c -o build/temp.macosx-10.3-fat-2.7/_imaging.o
unable to execute gcc-4.0: No such file or directory
error: command 'gcc-4.0' failed with exit status 1

这里有一些我从终端复制的信息,希望其中一些能帮助找出问题所在:

sgarza62$ gcc
i686-apple-darwin11-llvm-gcc-4.2: no input files

sgarza62$ python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:32:06)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

sgarza62$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python

sgarza62$ echo $PATH
/Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

sgarza62$ /usr/bin/python -c 'import sys;print(sys.version)'
2.7.2 (default, Jun 16 2012, 12:38:40) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)]
sgarza62$ /usr/bin/python2.6 -c 'import sys; print(sys.version)'
2.6.7 (r267:88850, Jun 16 2012, 12:38:45) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)]
sgarza62$ /usr/bin/python2.5 -c 'import sys;print(sys.version)'
2.5.6 (r256:88840, Jun 16 2012, 12:38:24) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)]
sgarza62$ /usr/bin/python2.7 -c 'import sys;print(sys.version)'
2.7.2 (default, Jun 16 2012, 12:38:40) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)]

这里有一些在StackOverflow上关于相同或类似问题的问答(但没有提供我问题的完整解决方案)。也许这些之前的回答能帮助我解决这个问题:

在Mac OS 10.6上用不同的gcc版本重新安装python

如何在Mac OS X 10.8 / Xcode 4.4上使用/安装gcc

Python在OS X上使用错误版本的GCC构建


这些是我已经做过的事情,都是Stack用户针对类似问题建议的:

  • 下载Xcode

  • 在Xcode的偏好设置中安装命令行工具

  • sudo ln -s /usr/bin/gcc /usr/bin/gcc-4.2


我在问题中尽量提供了尽可能多的事实,以清晰有序的方式呈现。如果你们需要更多信息,请随时问我!我会很乐意在问题中添加更多细节。

另外,我知道这个问题看起来很具体,但从搜索的结果来看,很多人对这个问题感到困惑。

如果可以的话,我非常希望能得到一个简单明了的解决方案(明确告诉我在终端中该怎么做来解决这个问题)。我在终端中还不太熟练,对于笼统的回答会有些困惑。

谢谢大家的时间,我非常感激!

1 个回答

4

这里的问题是,你在一个装有Xcode 4的OS X系统上使用了只能运行在32位系统上的python.org 2.7.3版本。这个32位的安装包是为了兼容多个较旧的OS X版本设计的,最早可以追溯到10.3.9。不过,随着Xcode 4去掉了对PPC的支持,以及其他一些变化,在10.7及以后的系统上,安装那些有C扩展模块的第三方Python包就不再那么顺利了。不过,还是有办法解决这个问题的。以PIL为例,只需要通过环境变量调整几个Python Distutils的默认设置就可以了:

# override the default gcc-4.0 compiler and use clang instead
$ export CC=clang
# override the universal architectures to only use Intel-32
$ export ARCHFLAGS='-arch i386'
$ pip install PIL

python.org网站为当前的Python版本提供了两个OS X安装包的版本。另一个安装包是64位/32位版本,它是为较新的一些系统配置的;这个版本不支持PPC,因此在使用Xcode 4时就不会遇到这个问题。

顺便提一下,Python 2.7的下一个版本(2.7.4)会对Xcode 4提供更好的支持,使用这两个安装包时就不会有这个问题了。

撰写回答