正在 OSX El Capitan 上升级设置工具

Upgrading setuptools on OSX El Capitan

我正在尝试升级安装工具。好吧,实际上我正在尝试升级 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

我该如何解决?

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

在命令中添加 --user python 允许它工作。

换句话说

pip install --upgrade setuptools --user python

答案是您无法在 OSX 上为 OS 附带的工厂 python 更新设置工具。原因是 /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python 中的文件不能被用户删除或修改。不仅是普通用户,即使您拥有 root 权限,您也将无法修改这些文件。

当然,您可以使用各种其他方法来安装更新版本的 setuptools,但这不会覆盖默认系统包。这意味着如果您按照建议的路线使用 --user python 标志,或者您选择在没有 root 的情况下将它们安装到 ~/Library,这些将不会覆盖系统文件版本。

您或 root 无法修改系统默认值的原因是由于 El Capitan+ 中的 SIP 权限限制。您可以禁用 SIP,但通常不建议这样做。

相反,唯一合理的解决方案是使用 python virtualenv。

概述

问题是 Python 库与 Mac OS 上保护系统 Python 库的系统完整性保护 (SIP) 相结合。

我认为最好的解决方案是卸载并重新安装您自己的 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 提供的 easy_install 但它写入 easy-install.pth 文件位于 /Library/Python/2.7/site-packages/easy-install.pth,该文件引用 Mac OS 提供的 Python 库。

随后使用 pip install ansible 安装 ansible 然后报告 Mac OS 在 /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python[=70 提供的 Python 库满足了 setuptools 要求=] Ansible 需要 setuptools 但不指定版本。另一个依赖包 paramiko 需要 setuptools >= 11.3,但是通过 pip 安装 ansible 时似乎没有检查。当你尝试 运行 ansible 虽然它抱怨设置工具是 1.1.6 这是 Mac OS 提供的 setuptools 版本,现在受 SIP 保护,所以它不能'暂未升级。

我没有禁用 SIP,而是按照 https://docs.python.org/2.7/using/mac.html#getting-and-installing-macpython 中的提示通过卸载本地 Python 来解决此问题 然后重新下载安装。

如果您担心 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 安装程序包 并安装它。

这安装了一个本地 python 和 /Library/Frameworks/Python.framework/Versions/2.7 的 pip 和 /usr/local/bin 中的符号链接,并在我的 $PATH 中添加了 /Library/Frameworks/Python.framework/Versions/2.7。这使一切都与 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