我正在尝试升级安装工具。好吧,实际上我正在尝试升级 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
问题是 Python 库冲突,与 Mac OS 上的系统完整性保护 (SIP) 相结合,可保护系统 Python 库。
我认为最好的解决方案是卸载并重新安装您自己的 Python 安装,并将其与 Mac OS 提供的受 SIP 保护的 Python 库分开。
我赞成禁用 SIP,因为我希望 SIP 成为任何未来 Mac OS 版本的一部分,而 SIP 不是这里的原因,它只是暴露了 Python 库冲突的问题。
我在尝试安装 ansible 时也遇到了这个问题。
当我按照 Mac OS 的 ansible 安装说明进行操作时,我的问题就开始了,即通过 pip 安装并使用
easy_install
安装 pip,如 通过 Pip 最新版本中所述
问题是,以这种方式安装 pip 时,
easy_install
是 Mac 操作系统提供的 easy_install
,位于 /usr/bin/easy_install
,但它会写入位于 easy-install.pth
的 /Library/Python/2.7/site-packages/easy-install.pth
文件,并且该文件引用 Mac 操作系统提供的 Python 库。
随后使用
pip install ansible
安装 ansible,然后报告 Mac OS 提供的 Python 库满足了 setuptools 要求,网址为 /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
我没有禁用 SIP,而是通过卸载本地 Python 来修复此问题,如 https://docs.python.org/2.7/using/mac.html#getting-and-installing-macpython 所示 然后再次下载并安装。
如果你担心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
然后我安装了ansible
pip install 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
是指向此的符号链接。答案是,您无法在 OSX 上为操作系统附带的工厂 python 更新 setuptools。原因是
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
中的文件无法被用户删除或修改。不仅是普通用户,即使您拥有 root 权限,您也无法修改这些文件。
当然,您可以使用各种其他方法来安装较新版本的 setuptools,但这不会覆盖默认的系统软件包。这意味着如果您按照建议的路线使用
--user python
标志,或者选择在没有 root 的情况下安装它们到 ~/Library
,这些将不会覆盖系统文件版本。
您或 root 无法修改系统默认值的原因是 El Capitan+ 中的 SIP 权限限制。您可以禁用 SIP,但通常不建议这样做。
唯一合理的解决方案是使用 python virtualenv。
我正在使用 MacbookAir (M1 2020),这对我有用:
pip install --upgrade setuptools