在OSX El Capitan上升级setuptools

81

我正在尝试升级setuptools。实际上,我正在尝试升级ansible,但它在尝试升级setuptools时失败了。即使我自己尝试也失败了。甚至试图卸载它也失败了。

$ sudo -H pip install --upgrade setuptools
Collecting setuptools
  Using cached setuptools-18.4-py2.py3-none-any.whl
Installing collected packages: setuptools
  Found existing installation: setuptools 1.1.6
    Uninstalling setuptools-1.1.6:
Exception:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/basecommand.py", line 211, in main
    status = self.run(options, args)
  File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/commands/install.py", line 311, in run
    root=options.root_path,
  File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/req/req_set.py", line 640, in install
    requirement.uninstall(auto_confirm=True)
  File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/req/req_install.py", line 716, in uninstall
    paths_to_remove.remove(auto_confirm)
  File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/req/req_uninstall.py", line 125, in remove
    renames(path, new_path)
  File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/utils/__init__.py", line 315, in renames
    shutil.move(old, new)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 299, in move
    copytree(src, real_dst, symlinks=True)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 208, in copytree
    raise Error, errors
Error: [('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.py', '/tmp/pip-OyTXsR-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.py', "[Errno 1] Operation not permitted: '/tmp/pip-OyTXsR-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.py'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.pyc', '/tmp/pip-OyTXsR-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.pyc', "[Errno 1] Operation not permitted: '/tmp/pip-OyTXsR-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.pyc'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/markers.py', '/tmp/pip-OyTXsR-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/markers.py', "[Errno 1] Operation not permitted: '/tmp/pip-OyTXsR-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/markers.py'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/markers.pyc', '/tmp/pip-OyTXsR-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/markers.pyc', "[Errno 1] Operation not permitted: '/tmp/pip-OyTXsR-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/markers.pyc'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib', '/tmp/pip-OyTXsR-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib', "[Errno 1] Operation not permitted: '/tmp/pip-OyTXsR-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib'")]

我完全不知道哪里出了问题。查看/System/Library/Frameworks/Python.framework/Versions/2.7/及以下所有文件,每个文件都是由root:wheel所有。

我该如何解决?


2
我没有解决方案,但我有一个解释:这可能是由于新的系统完整性保护。即使使用sudo,pip也试图修改不允许修改的内容。请参见此问题。我还没有花时间处理这个问题。--user选项可能是我要研究的。如果您找到了实际的“更新”所有内容的解决方案,请分享! - Arthur
我遇到了与setuptools相关的ipdb错误。升级setuptools并不能解决问题。使用--user=python可以解决问题,但仍无法修复ipdb错误。 - R11G
4个回答

189

这是由于OS X El Capitan中引入的系统完整性保护所导致的。

在命令中添加--user python允许此操作正常工作。

换句话说

pip install --upgrade setuptools --user python

2
pip(与easy_install不同)不需要sudo权限,因为您让它使用python用户,我确保它可以正常工作。 - Kof
1
同样适用于Docker容器内的CentOS 7。由于某种原因而失败(尽管在2016年5月之前,完全相同的设置可以正常工作...) - geerlingguy
2
我无法让它正常工作,也不理解--user选项应该如何工作。对我有用的方法是在升级setuptools(与安装ipython相关)时暂时禁用系统完整性保护,然后在之后重新启用它。要做到这一点:进入恢复模式(cmd+R),启动终端并输入csrutil disable。重启后,在升级后重新启动到恢复模式并输入csrutil enable。重启后,ipython对我来说仍然可以正常工作。 - Grav
我最终按照这里给出的指示(http://apple.stackexchange.com/questions/209572/how-to-use-pip-after-the-os-x-el-capitan-upgrade)成功了。 - diidu
它告诉Python使用“python”用户,将所需的文件分配给该用户。否则,它可能会尝试使用当前登录用户进行操作。 - Kof
显示剩余3条评论

2

概述

问题在于Python库冲突,再加上Mac OS的System Integrity Protection (SIP)保护系统Python库。

我认为最好的解决方案是卸载并重新安装自己的Python安装包,并将其与由Mac OS提供的Python库分开,后者由SIP保护。

我更喜欢这种方法而不是禁用SIP,因为我预计SIP将成为任何未来Mac OS版本的一部分,而SIP并不是原因,它只是暴露了Python库冲突的问题。

详细信息

我在尝试安装ansible时也遇到了这个问题。

我的问题始于按照Mac OS的ansible安装说明进行安装,即通过pip安装并使用easy_install安装pip,如Latest Releases Via Pip所述。

问题在于使用这种方式安装pip时,easy_install是Mac OS提供的/usr/bin/easy_install,但它会写入/Library/Python/2.7/site-packages/easy-install.pth文件,并引用Mac OS提供的Python库。

随后使用pip install ansible安装ansible,然后报告setuptools要求使用Mac OS提供的Python库/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python。Ansible需要setuptools但没有指定版本。另一个依赖包paramiko需要setuptools >= 11.3,但似乎在使用pip安装ansible时不会检查此项。当您尝试运行ansible时会出现错误,提示setup tools为1.1.6,这是Mac OS提供的setuptools版本,而且现在受到SIP的保护,因此无法升级。

我通过卸载我的本地Python(如https://docs.python.org/2.7/using/mac.html#getting-and-installing-macpython中所示)并重新下载和安装来解决此问题,而不是禁用SIP。

如果您担心rm,可以改用mv
要卸载,我执行了以下操作:

sudo rm -rf /Library/Python
sudo rm -rf /Applications/Python\ 2.7/
sudo rm /usr/local/bin/ansible  # executable
sudo rm /usr/local/bin/python*  # symlinks to /Library/Python/2.7
sudo rm /usr/local/bin/easy_install*
# and so on for references to /Library/Python/2.7 in /usr/local/bin

我随后从https://www.python.org/downloads/下载了适用于Mac OS X的2.7.13安装程序包,并进行了安装。
这会在/Library/Frameworks/Python.framework/Versions/2.7安装本地python和pip,并在/usr/local/bin中创建符号链接,同时将/Library/Frameworks/Python.framework/Versions/2.7添加到我的$PATH。这样可以使它与Mac OS提供的库(/System/Library/Frameworks/Python.framework/usr/bin)保持分离,以便我获得更好的效果。
which pip
/Library/Frameworks/Python.framework/Versions/2.7/bin/pip

我随后使用pip install ansible安装了ansible。
然后当我运行which ansible时,我得到以下结果:

/Library/Frameworks/Python.framework/Versions/2.7/bin/ansible

pip list

setuptools (28.8.0)
six (1.10.0)

现在我的Ansible可以正常工作了,因为新安装的程序没有引用Mac OS Python库。

请注意,由于安装程序设置了$PATH,which python现在是/Library/Frameworks/Python.framework/Versions/2.7/bin/python,而/usr/local/bin/python是这个的符号链接。
如果你想要使用系统Python,你需要使用/usr/bin/python或者改变$PATH。


1
答案是,你无法在OSX上为系统自带的Python更新setuptools。原因是用户无法删除或修改/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python中的文件。即使你有root权限也无法修改这些文件。当然,你可以使用其他方法来安装更新版本的setuptools,但这并不会覆盖默认的系统包。这意味着如果你选择使用--user python标志或者选择将其安装到~/Library而没有root权限,它们将不会覆盖系统文件版本。
你或root无法修改系统默认设置的原因是El Capitan+中SIP权限限制。你可以禁用SIP,但这通常不推荐。
相反,唯一合理的解决方案是使用Python虚拟环境。

0
我正在使用MacbookAir(M1 2020),这对我有效:
pip install --upgrade setuptools

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