如何从 Homebrew 重新安装 python@2?

Pau*_*ine 58 python macos homebrew python-2.x

我在使用 brew 时遇到了 openssl 和 python@2 问题,这里已经解释了(未解决)。重新安装 Python 和 openssl 的文档化解决方法不起作用,所以我决定卸载并重新安装 Python。

问题是,当您尝试使用 brew 安装 Python 2 时,您会收到以下消息:

brew install python@2
Error: No available formula with the name "python@2"
==> Searching for a previously deleted formula (in the last month)...
Warning: homebrew/core is shallow clone. To get complete history run:
  git -C "$(brew --repo homebrew/core)" fetch --unshallow

python@2 was deleted from homebrew/core in commit 028f11f9e:
  python@2: delete (https://github.com/Homebrew/homebrew-core/issues/49796)
  EOL 1 January 2020.
  We gave it 1 month more to live so that people had time to migrate.
  All in all, developers had 11 years to do their migration.
  You can use the `brew extract` command and maintain python@2 in your own
  tap if necessary:
  https://docs.brew.sh/How-to-Create-and-Maintain-a-Tap

To show the formula before removal run:
  git -C "$(brew --repo homebrew/core)" show 028f11f9e^:Formula/python@2.rb

If you still use this formula consider creating your own tap:
  https://docs.brew.sh/How-to-Create-and-Maintain-a-Tap
Run Code Online (Sandbox Code Playgroud)

不幸的是,我仍然有许多依赖于 Brew 的 python@2 的 brew 公式。那些包括awscli, letsencrypt, prsshuttle例如

aws
zsh: /usr/local/bin/aws: bad interpreter: /usr/local/opt/python@2/bin/python2.7: no such file or directory
Run Code Online (Sandbox Code Playgroud)

我不知道如何使用brew extract他们记录的这个命令来重新安装 Python@2。它需要一个公式和一个水龙头。我想公式是python@2. 我不确定水龙头需要什么。

此外,重新安装水龙头,例如awsletsencrypt也不能很好地工作。

重新安装awscli( brew reinstall awscli) 后,运行 aws 命令仍然会出错。

aws
/usr/local/Cellar/awscli/2.0.0/libexec/lib/python3.8/site-packages/jmespath/visitor.py:32: SyntaxWarning: "is" with a literal. Did you mean "=="?
  if x is 0 or x is 1:
/usr/local/Cellar/awscli/2.0.0/libexec/lib/python3.8/site-packages/jmespath/visitor.py:32: SyntaxWarning: "is" with a literal. Did you mean "=="?
  if x is 0 or x is 1:
/usr/local/Cellar/awscli/2.0.0/libexec/lib/python3.8/site-packages/jmespath/visitor.py:34: SyntaxWarning: "is" with a literal. Did you mean "=="?
  elif y is 0 or y is 1:
/usr/local/Cellar/awscli/2.0.0/libexec/lib/python3.8/site-packages/jmespath/visitor.py:34: SyntaxWarning: "is" with a literal. Did you mean "=="?
  elif y is 0 or y is 1:
/usr/local/Cellar/awscli/2.0.0/libexec/lib/python3.8/site-packages/jmespath/visitor.py:260: SyntaxWarning: "is" with a literal. Did you mean "=="?
  if original_result is 0:
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:

  aws help
  aws <command> help
  aws <command> <subcommand> help
aws: error: the following arguments are required: command
Run Code Online (Sandbox Code Playgroud)

hyp*_*not 158

似乎自制软件的工作人员真的尽可能地在 macOS 上使用 Python 2.7 变得尽可能困难。

  1. 链接的brew extract链接真的没有帮助,您需要在此处寻找有关如何从提取的源中制作自己的水龙头的答案。
  2. 链接提交:028f11f9e 是错误的,因为它包含已删除的文件。
  3. 由于包名称中的@,该brew extract命令甚至无法正常工作

解决方案非常简单,您只需要下载最新的已知提交并从该文件安装:

cd ~
wget https://raw.githubusercontent.com/Homebrew/homebrew-core/86a44a0a552c673a05f11018459c9f5faae3becc/Formula/python@2.rb
brew install python@2.rb
rm python@2.rb
Run Code Online (Sandbox Code Playgroud)

可能会有关于“不稳定”的警告,我不明白这是因为 Git 历史记录中的提交尽可能稳定。

  • 这个(`brew install &lt;raw url&gt;`)似乎不再适用于当前版本的 Homebrew,它会产生以下错误:`错误:从 GitHub 提交 URL 调用安装 python@2 被禁用!使用“brew extract python@2”来稳定 GitHub 上的点击。 (20认同)
  • 执行brew install python@2.rb时出现以下错误错误:python@2:无效的瓶子标签符号 (14认同)
  • 只需将 PR 的链接放在这里,其中 python@2 已被删除(于 2020 年 2 月 4 日合并)https://github.com/Homebrew/homebrew-core/pull/49796 (11认同)
  • 从上次提交开始不起作用...`==&gt;正在下载 https://ghcr.io/v2/homebrew/core/python/2/manifests/2.7.17_1 ##O#- #curl: (22) 请求的URL返回错误:404错误:无法下载资源“python@2_bottle_manifest”下载失败:https://ghcr.io/v2/homebrew/core/python/2/manifests/2.7.17_1` (8认同)
  • @petschki 不是我们要求的答案,而是我们想知道的答案! (7认同)
  • 我就是这样做的: `cd ~` - `wget https://raw.githubusercontent.com/Homebrew/homebrew-core/86a44a0a552c673a05f11018459c9f5faae3becc/Formula/python@2.rb` - `brew install python@2.rb` (7认同)
  • 好的,我从 Python 发布页面下载并且安装程序可以运行,但安装证书的脚本却不能运行。https://www.python.org/downloads/release/python-2716/ (6认同)
  • 我通过 @MrTomRod 更新了基于 wget 的版本的答案。Homebrew 团队确实不遗余力地试图在 macOS 上禁止 Python 2.7:删除所有提到它的论坛主题、删除 GitHub 问题、禁止 GitHub 帐户打开问题,现在还将源代码中的提交 ID 列入黑名单。 (4认同)
  • 这不适用于 M1 / Apple Silicon mac,因为此公式不支持它。它给出以下错误:“configure: error: Unexpected output of 'arch' on OSX” 这可能是因为在 M1 发布之前,brew 放弃了对 python2 的支持。 (4认同)
  • 似乎这不再适用于 M1 Mac...某种类型的构建错误... (3认同)
  • 这里有人试图在系统版本已被完全删除的最新版本 MacOS 12.3 Monterey 上安装 Python 2 吗?这个方法不再有效了。起初,“brew”抱怨无法识别的瓶子名称,但即使在我注释掉公式的“bottle do”部分之后,安装也会失败并出现 Python 错误:“File "./setup.py", line 805, in detector_modules (tuple(int(n) for n in dep_target.split('.')[0:2]) AttributeError: 'int' 对象没有属性 'split'` (3认同)
  • 这个错误有什么解决办法吗?python@2:无效的瓶子标签符号 (2认同)

y3s*_*3sh 122

对于Apple 在 macOS 12.3 中删除系统 python 后出现在这里的用户,以下是如何安装和运行 python2 和 python3。

蟒蛇2

python, python2-> 蟒蛇 2.7

# Download/run the legacy macOS installer (pick which one for your sys)
https://www.python.org/downloads/release/python-2718/

# Add pip for python2.7
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip2.py
python2 get-pip2.py

# Optionally check for pip updates (in case of post-eol patches)
python2 -m pip install --upgrade pip

# Optionally add the helpers like easy_install back onto your path
# In your ~/.zprofile or whatever bash/shell profile equivalent
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH

# Optionally add some helpers while editing shell profile
alias pip2="python2 -m pip"
alias venv2="virtualenv -p python2"
alias venv3="virtualenv -p python3"

# Optionally some apple-specific std libraries are missing, search
# and download them. Example: plistlib.py
curl https://raw.githubusercontent.com/python/cpython/2.7/Lib/plistlib.py -o /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plistlib.py

# Lastly, there is no symlink /usr/bin/python anymore
# /usr/bin is system protected so you can't add one either
# 
# Change your programs to use /usr/local/bin/python
# or google how to disable macOS SIP to make a symlink in /usr/bin
Run Code Online (Sandbox Code Playgroud)

蟒蛇3

python3-> 蟒蛇3

brew update
brew install python3

# Add pip for python 3 in case it is missing
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py

# Check for pip updates
python3 -m pip install --upgrade pip

# Optionally add a helper in ~/.zprofile
alias venv3="virtualenv -p python3"
Run Code Online (Sandbox Code Playgroud)

测试一下

~ % python --version
Python 2.7.18

~ % python2 --version
Python 2.7.18

~ % python3 --version
Python 3.9.10

# Running older python2
python2 -m pip install...
python2 ...

# Testing the venv2 alias from above
venv2 foo
source foo/bin/activate
pip -V # pip 20... from... python2.7
pip install -y -r req.txt
pip uninstall -y -r req.txt
pip freeze
deactivate

# Testing the venv3 alias from above
venv3 foo3
source foo3/bin/activate
pip -V # pip22... from ...python3.9
pip install -y -r req.txt
pip uninstall -y -r req.txt
pip freeze
deactivate
Run Code Online (Sandbox Code Playgroud)

通过卸载/重新安装进行故障排除

# Credit to https://www.macupdate.com/app/mac/5880/python/uninstall  
# for many of the tips in this section.

# Sometimes there are problems related to accepting xcode 
# tool agreement. Open XCode to make sure it finished 
# installing its tool updates.

# Remove old python Application installs
# open the apps dir and delete Python 2, 3 via Finder
open /Applications

# Remove old brew installs 
brew list | grep python
brew uninstall python
brew uninstall python3

# find/remove lingering unlinked kegs
ls /usr/local/Cellar/ | grep python 

# Cleanup binaries
sudo rm -rf /Library/Frameworks/Pyth*
rm /usr/local/bin/pip*

# Cleanup symlinks
which -a python # check results, and rm each one
which -a python2 # check results, and rm each one
which -a python3 # check results, and rm each one

brew cleanup # prunes symlinks
Run Code Online (Sandbox Code Playgroud)

  • 先生,您值得一枚勋章。非常感谢! (21认同)

jvf*_*jvf 50

如何python@2从本地水龙头安装

以下方法适用于 的当前版本 ( c9b8a3ef6) brew

$ brew tap-new <user>/homebrew-python2
$ brew extract python@2 <user>/homebrew-python2
$ brew install /usr/local/Homebrew/Library/Taps/<user>/homebrew-python2/Formula/python@2.7.17.rb
Run Code Online (Sandbox Code Playgroud)

brew tap-new命令在/usr/local/Homebrew/Library/Taps/<user>/homebrew-python2. 水龙头名称需要一个<user>和一个<repo>由 分隔的组件/。实际值是任意的。上面的命名遵循如何创建和维护 Tap 中的约定。如果您想将点击推送到 GitHub,您可以使用您的 GitHub 用户名作为用户。没有必要推送到 GitHub(并且没有在上面的说明中执行)。

这些brew extract命令从 repos 历史中提取最新版本的公式到给定的(本地)tap 中。在我们的情况下python@2.7.17.rb被提取。

brew install命令最终会安装公式。

为什么这是必要的?

上面讨论的方法(从 GitHub 提交 URL 安装旧版本的公式)不再适用python@2brew( c9b8a3ef6)的当前版本,它会产生以下错误:

$ brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/86a44a0a552c673a05f11018459c9f5faae3becc/Formula/python@2.rb
Updating Homebrew...
==> Auto-updated Homebrew!
Updated Homebrew from 88f17b8b6 to c9b8a3ef6.
...
Error: Calling Installation of python@2 from a GitHub commit URL is disabled! Use 'brew extract python@2' to stable tap on GitHub instead.
Run Code Online (Sandbox Code Playgroud)

  • 这应该是答案,因为接受的答案不再有效。 (2认同)
  • 不幸的是,这些说明对我不起作用。当尝试“brew extract python@2 &lt;user&gt;/homebrew-python2”时,我得到:*错误:python@2:nil:NilClass的未定义方法“[]”* (2认同)

小智 11

请检查以下命令(我在 macOS 10.13 上使用它,对于较新的 macOS,它可能无需源代码编译即可运行):

brew install pr0d1r2/python2/python@2.7.17 --build-from-source
Run Code Online (Sandbox Code Playgroud)

  • 这有效,但这也重新安装了我的Python3。这意味着它删除了我安装的所有软件包。不过谢谢。 (3认同)

小智 11

这对我在 m1(12.3.1) 上工作:
不要使用brew,直接从python官方网站下载。
这个问题折磨了我很久:(

  • 这解决了我的问题。真的很简单,谢谢 (2认同)

don*_*ner 9

您可以使用以下pyenv命令安装 python:

brew install pyenv
pyenv install 2.7.18
Run Code Online (Sandbox Code Playgroud)

(可选)将其设置为您的全局默认值:

pyenv global 2.7.18
Run Code Online (Sandbox Code Playgroud)

关于为什么使用pyenvbrew用于管理您的 python 安装更好的好文章

  • 在 macOS 12.3 Monterey 删除了 python 2.7 后,这对我来说效果最好。 (15认同)
  • 抱歉,我有一个新手问题,但是我可以使用全局“python”或“python2”吗?执行上述步骤后,“which python”显示“未找到 python”。(“which python3”有效并返回“/opt/homebrew/bin/python3”)。 (10认同)
  • 我不推荐这样做,因为它尝试从源代码构建 Python,这将永远花费时间并且可能会失败(就像我在 M1 Mac 上所做的那样)。另外,“pyenv”是您可能不需要的又一个移动部分。 (5认同)

ab7*_*b77 5

对于后代,在 macOS 10.15(2021 年 5 月)上工作:

/usr/local/bin/brew tap-new ${USER}/homebrew-python2

/usr/local/bin/brew extract python@2 ${USER}/homebrew-python2

/usr/local/bin/brew install /usr/local/Homebrew/Library/Taps/${USER}/homebrew-python2/Formula/python@2.7.17.rb

# https://github.com/Homebrew/brew/issues/5734#issuecomment-464705002
/usr/local/bin/brew untap ${USER}/python2
Run Code Online (Sandbox Code Playgroud)