一. 问题
二. 分析
2.1 系统环境
2.2 Cellar Python
三. 查找
3.1 formula 和 cask
3.2 python@2 delete
四. 解决
五. 参考
最近折腾 Python 环境有些需要在 Python@2 环境下才能进行,今天用 brew upgrade 后,发现 python@2 咋没了?
没办法,我猜 brew 的python@2 可能出问题了,卸载再安装报如下错误?
# 查找
$ brew search python@2
Error: No formulae or casks found for "python@2".
# 安装
$ brew install python@2
==> Searching for similarly named formulae...
Error: No similarly named formulae found.
Error: No available formula or cask with the name "python@2".
==> Searching for a previously deleted formula (in the last month)...
Error: No previously deleted formula found.
==> Searching taps on GitHub...
Error: No formulae found in taps.
以前好好的,咋安装不上了呢 ?
首先,看看本机环境都有哪些 python 版本:
# 本机环境版本
$ python --version
Python 2.7.16
# 本机环境,脚本命令路径
$ which python
/usr/bin/python
## python2,脚本命令路径
$ which python2
/usr/local/bin/python2
## python3,脚本命令路径
$ which python3
/usr/local/bin/python3
查看本机环境链接的脚本路径:
# 本机环境链接的系统Python 版本
$ ls -al /usr/bin/python*
lrwxr-xr-x 1 root wheel 75 10 11 2019 /usr/bin/python -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x 1 root wheel 82 10 11 2019 /usr/bin/python-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
lrwxr-xr-x 1 root wheel 75 10 11 2019 /usr/bin/python2 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x 1 root wheel 75 10 11 2019 /usr/bin/python2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x 1 root wheel 82 10 11 2019 /usr/bin/python2.7-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
-rwxr-xr-x 1 root wheel 31488 3 17 2020 /usr/bin/python3
lrwxr-xr-x 1 root wheel 76 10 11 2019 /usr/bin/pythonw -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7
lrwxr-xr-x 1 root wheel 76 10 11 2019 /usr/bin/pythonw2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7
可见,系统真实的引用路径为:
/System/Library/Frameworks/Python.framework/Versions/2.7/
系统本机环境提供的 python 版本不建议进行安装各种第三方和更新 python 版本,以免引起不必要的不可预见的麻烦;
推荐 brew install python 方式,然后使用virtualenv 虚拟环境方式,配置各个项目运行空间;
查看一下 python2 和 python3 引用的路径:
$ ls -al /usr/local/bin/python*
lrwxr-xr-x 1 root admin 18 12 20 2018 /usr/local/bin/python2 -> /usr/bin/python2.7
lrwxr-xr-x 1 xxx admin 40 5 18 10:19 /usr/local/bin/python3 -> ../Cellar/[email protected]/3.9.1_8/bin/python3
lrwxr-xr-x 1 xxx admin 47 5 18 10:19 /usr/local/bin/python3-config -> ../Cellar/[email protected]/3.9.1_8/bin/python3-config
lrwxr-xr-x 1 xxx admin 40 1 14 18:06 /usr/local/bin/python3.8 -> ../Cellar/[email protected]/3.8.5/bin/python3.8
lrwxr-xr-x 1 xxx admin 47 1 14 18:06 /usr/local/bin/python3.8-config -> ../Cellar/[email protected]/3.8.5/bin/python3.8-config
lrwxr-xr-x 1 xxx admin 42 5 18 10:19 /usr/local/bin/python3.9 -> ../Cellar/[email protected]/3.9.1_8/bin/python3.9
lrwxr-xr-x 1 xxx admin 49 5 18 10:19 /usr/local/bin/python3.9-config -> ../Cellar/[email protected]/3.9.1_8/bin/python3.9-config
目前发现,python3.x 版本链接的都是 homebrew 中安装的;但是 python2 之前卸载掉后,通过 homebrew 安装不上了,然后链接为本机系统的 python2.x 版本;
查看一下,各个python 版本的 pip 版本:
# python2 pip version
$ python2 -m pip --version
/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: No module named pip
# python3 pip version
$ python3 -m pip --version
pip 21.0.1 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)
目前发现,系统环境 Python 2.7.16 版本的 Python2.x 没有安装 pip;
注意:
macOS 系统Python 不提供pip 。如果需要,请按照pip 文档 为系统Python 安装它。
从如上分析看来,需要做如下步骤:
目前看来,步骤三是行不通的;
去Python 官网,https://www.python.org/downloads/ 发现如下信息:
Python 2.7是Python 2.x系列的结尾;作为2.x系列的最后一个版本,Python 2.7 在2020年之前获得了错误修复支持。
该支持在2020年1月1日正式停止,并且2.7.18 代码冻结在2020年1月1日发生,但最终版本在该日期之后发生。即不会有Python 2.8 版本;
哎,原来是官方不再支持和维护了;
formula 和 casks 如何翻译和如何区别:
formulae 意思是一些软件包(SDK),一般是命令行工具、开发库、一些字体、插件,共性是不提供界面,提供给终端或者是开发者使用。
cask 是用户软件(APP),比如 chrome、mvim、wechat、wechatwork 这些提供用户交互界面的软件。
把问题转向Homebrew Documentation -> Python 看看官网上对 Python 有啥说明没?
看到了如下说明:
Homebrew provided a
python@2
formula until the end of 2019, at which point it was removed due to the Python 2 deprecation.翻译:
Homebrew 提供了一个python@2 软件包,直到2019年底,由于Python 2 已被官方弃用,所以该软件包已被删除和不受支持。
Important: If you choose to use a Python which isn’t either of these two (system Python or brewed Python), the Homebrew team cannot support any breakage that may occur.
翻译:
如果您选择使用的Python不是这两种(系统Python或酿造Python)中的任何一种,那么Homebrew 团队将无法支持可能发生的任何损坏。
啥意思呢?
大致就是 Python 官网不再支持和维护 Python 2.x 了,那么 Homebrew 团队也将和官网同步不再支持和维护 Python 2.x 了,所以就从存储库里给删除了;
那咋办 ?
我去 Homebrew Github 上列举了一些issue 信息:
此处我个人是比较支持 50546 中,提问者观点的,然后看到
由于过于激烈而无法与合作者进行对话,此issue 被锁定,我就怂了;只敢在此处吐槽一下:
一个软件在整个生命周期内,哪怕过了软件最迟寿命支持周期,只要有一定的存量用户(规模性比较大的场景),原则上是不受支持的,但是道义上还是需要开个口子(给个继续使用的办法或升级到最新版本的方案,并行一段时间,或持续性特定版本后续不再维护,总之,有办法持续能用,但我不再维护。)支持一下的;
不能因为这个软件过时11 年了,有将近 10 年时间去更新到最新受官方支持的版本上去而打发人; 次观点我是不敢苟同(不是一个技术人该有的操作),实际上,日常生活和商业环境中,还有大量过时的机器和软件系统在跑着上古代码呢?
微软的 XP 系统,虽然官方早就不再支持了,但到现在还有很多人使用呢?适用的典型场景有打印店...老破旧古董机器上跑新系统压根有些就跑不起来,旧设施硬件下不得不装旧系统,而且还是有办法装 XP 系统的;
参考:
cd ~
wget https://raw.githubusercontent.com/Homebrew/homebrew-core/86a44a0a552c673a05f11018459c9f5faae3becc/Formula/[email protected]
brew install [email protected]
rm [email protected]
执行过程,如下:
# 1. 进入默认用户目录
$ cd ~
# 2. 下载 [email protected] 文件
$ wget https://raw.githubusercontent.com/Homebrew/homebrew-core/86a44a0a552c673a05f11018459c9f5faae3becc/Formula/[email protected]
--2021-05-18 16:47:21-- https://raw.githubusercontent.com/Homebrew/homebrew-core/86a44a0a552c673a05f11018459c9f5faae3becc/Formula/[email protected]
正在解析主机 raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.109.133, 185.199.110.133, 185.199.108.133, ...
正在连接 raw.githubusercontent.com (raw.githubusercontent.com)|185.199.109.133|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:13347 (13K) [text/plain]
正在保存至: “[email protected]”
[email protected] 100%[====================================================================>] 13.03K --.-KB/s 用时 0.002s
2021-05-18 16:47:22 (6.84 MB/s) - 已保存 “[email protected]” [13347/13347])
# 3. 修改 [email protected] 文件权限(可选)
$ chmod 755 python\@2.rb
# 4. 删除系统默认 /usr/local/bin/python2 -> /usr/bin/python2.7(可选)
$ rm /usr/local/bin/python2
# 5. 安装 [email protected]
$ brew install [email protected]
Error: Failed to load cask: [email protected]
Cask 'python@2' is unreadable: wrong constant name #
Warning: Treating [email protected] as a formula.
==> Downloading https://homebrew.bintray.com/bottles/python%402-2.7.17_1.catalina.bottle.tar.gz
Already downloaded: /Users/dovsnier/Library/Caches/Homebrew/downloads/a4e7e62c67a0ffb99906cfde7bfaf5b028f31c44f5a5ec3aec49740205151fc5--python@2-2.7.17_1.catalina.bottle.tar.gz
==> Pouring [email protected]_1.catalina.bottle.tar.gz
==> /usr/local/Cellar/python@2/2.7.17_1/bin/python -s setup.py --no-user-cfg install --force --verbose --single-version-externally-managed --recor
==> /usr/local/Cellar/python@2/2.7.17_1/bin/python -s setup.py --no-user-cfg install --force --verbose --single-version-externally-managed --recor
==> /usr/local/Cellar/python@2/2.7.17_1/bin/python -s setup.py --no-user-cfg install --force --verbose --single-version-externally-managed --recor
==> Caveats
Pip and setuptools have been installed. To update them
pip install --upgrade pip setuptools
You can install Python packages with
pip install
They will install into the site-package directory
/usr/local/lib/python2.7/site-packages
See: https://docs.brew.sh/Homebrew-and-Python
==> Summary
/usr/local/Cellar/python@2/2.7.17_1: 3,806 files, 51.7MB
# 6. 删除 [email protected] 文件
$ rm [email protected]
# 7. 如果有需要,卸载 [email protected](可选)
$ brew uninstall python\@2.rb
查看一下安装后,Python@2 引用:
$ ls -al /usr/local/bin/python*
lrwxr-xr-x 1 xxx admin 38 5 18 17:03 /usr/local/bin/python -> ../Cellar/python@2/2.7.17_1/bin/python
lrwxr-xr-x 1 xxx admin 45 5 18 17:03 /usr/local/bin/python-config -> ../Cellar/python@2/2.7.17_1/bin/python-config
lrwxr-xr-x 1 xxx admin 39 5 18 17:03 /usr/local/bin/python2 -> ../Cellar/python@2/2.7.17_1/bin/python2
lrwxr-xr-x 1 xxx admin 46 5 18 17:03 /usr/local/bin/python2-config -> ../Cellar/python@2/2.7.17_1/bin/python2-config
lrwxr-xr-x 1 xxx admin 41 5 18 17:03 /usr/local/bin/python2.7 -> ../Cellar/python@2/2.7.17_1/bin/python2.7
lrwxr-xr-x 1 xxx admin 48 5 18 17:03 /usr/local/bin/python2.7-config -> ../Cellar/python@2/2.7.17_1/bin/python2.7-config
lrwxr-xr-x 1 xxx admin 40 5 18 10:19 /usr/local/bin/python3 -> ../Cellar/[email protected]/3.9.1_8/bin/python3
lrwxr-xr-x 1 xxx admin 47 5 18 10:19 /usr/local/bin/python3-config -> ../Cellar/[email protected]/3.9.1_8/bin/python3-config
lrwxr-xr-x 1 xxx admin 40 1 14 18:06 /usr/local/bin/python3.8 -> ../Cellar/[email protected]/3.8.5/bin/python3.8
lrwxr-xr-x 1 xxx admin 47 1 14 18:06 /usr/local/bin/python3.8-config -> ../Cellar/[email protected]/3.8.5/bin/python3.8-config
lrwxr-xr-x 1 xxx admin 42 5 18 10:19 /usr/local/bin/python3.9 -> ../Cellar/[email protected]/3.9.1_8/bin/python3.9
lrwxr-xr-x 1 xxx admin 49 5 18 10:19 /usr/local/bin/python3.9-config -> ../Cellar/[email protected]/3.9.1_8/bin/python3.9-config
lrwxr-xr-x 1 xxx admin 39 5 18 17:03 /usr/local/bin/pythonw -> ../Cellar/python@2/2.7.17_1/bin/pythonw
lrwxr-xr-x 1 xxx admin 40 5 18 17:03 /usr/local/bin/pythonw2 -> ../Cellar/python@2/2.7.17_1/bin/pythonw2
lrwxr-xr-x 1 xxx admin 42 5 18 17:03 /usr/local/bin/pythonw2.7 -> ../Cellar/python@2/2.7.17_1/bin/pythonw2.7
(完)