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

How to reinstall python@2 from Homebrew?

我一直在使用 openssl 和 python@2 使用 brew,。记录的重新安装 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

不幸的是,我仍然有一些依赖于 Brew 的 python@2 的 brew 配方。这些包括 awscliletsencrypt、pr sshuttle 例如

aws
zsh: /usr/local/bin/aws: bad interpreter: /usr/local/opt/python@2/bin/python2.7: no such file or directory

我不知道如何使用他们记录的这个 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

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

  1. linked brew extract link 确实没有帮助,您需要在这里寻找有关如何从提取的源中制作自己的水龙头的答案。
  2. linked 提交:028f11f9e 是错误的,因为它包含已删除的文件。
  3. brew extract命令doesn't even work correctly,因为包名中有@。

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

cd ~
wget https://raw.githubusercontent.com/Homebrew/homebrew-core/86a44a0a552c673a05f11018459c9f5faae3becc/Formula/python@2.rb
brew install python@2.rb
rm python@2.rb

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

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

brew install pr0d1r2/python2/python@2.7.17 --build-from-source

如何从本地 tap

安装 python@2

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

$ 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

brew tap-new 命令在 /usr/local/Homebrew/Library/Taps/<user>/homebrew-python2 中创建了一个新的本地点击模板。 tap 名称需要一个 <user> 和一个 <repo> 组件,由 / 分隔。实际值是任意的。上面的命名遵循 How to Create and Maintain a Tap 的约定。如果您想将水龙头推到 GitHub,您将使用您的 GitHub 用户名作为用户。推送到 GitHub 不是必需的(并且未在上述说明中执行)。

brew extract 命令将最近版本的公式从回购历史中提取到给定的(本地)tap 中。在我们的例子中 python@2.7.17.rb 被提取。

brew install 命令终于安装了公式。

为什么这是必要的?

上面讨论的方法(从 GitHub commit URL 安装旧版本的公式)对于 python@2 和当前版本的 brew 不再适用] (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.

您可以使用 pyenv 安装 python:

brew install pyenv
pyenv install 2.7.18

可选择将其设置为全局默认值:

pyenv global 2.7.18

Nice article 为什么使用 pyenv 比使用 brew 来管理您的 python 安装更好。

要使 python 二进制文件在全球范围内可用,请将垫片添加到 PATH:

PATH=$(pyenv root)/shims:$PATH

为了后代,在 macOS 10.15 (May/2021) 上工作:

/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

我使用此页面中的方法在 Mac 上安装了 Python 2.7 一年。 但是 2021 年 5 月,我尝试了此页面上的大多数建议,但都失败了。

可能 Python 2.7 越来越难安装,或者我的新 macOS Big Sur 11.4 导致了问题。

我能够通过重用本机 Python 2.7.16

以这种方式设置工作的 Python 2.7 环境
  • 手动安装 pip
  • 设置虚拟环境
  • 在虚拟环境中用pip安装依赖[=​​23=]

安装步骤如下:

wget https://bootstrap.pypa.io/pip/2.7/get-pip.py
python get-pip.py
pip install virtualenv
~/Library/Python/2.7/bin/pip  install virtualenv
virtualenv --python=/usr/bin/python venv
source venv/bin/activate
此页面上的

None 答案对我 1 在 MacOS 蒙特雷有用。如果这对任何人都有帮助,这里有一个替代解决方案,从技术上讲,它不是通过 Homebrew 直接安装的——只是间接安装。

对我来说,一个简单有效的解决方案是通过 Homewbrew 安装 Anaconda,然后为 Python 2.7.

创建一个虚拟环境

注意:安装 Anaconda 会在您的计算机上占用一些 space,而不仅仅是通过 Homebrew 或 Pyenv 安装 Python 2.7:

Anaconda is a distribution of the Python and R programming languages for scientific computing (data science, machine learning applications, large-scale data processing, predictive analytics, etc.), that aims to simplify package management and deployment. The distribution includes data-science packages suitable for Windows, Linux, and macOS. https://en.wikipedia.org/wiki/Anaconda_(Python_distribution)

基本上,步骤如下 - 但您可能需要参考完整的安装指南以了解更多详细信息。

从 Homebrew 安装 Anaconda:

brew install --cask anaconda

安装后,在您选择的文件夹中为 Python 2.7 创建虚拟环境,在本例中为 Python 2.7.18:

conda create --prefix=/MY_FOLDER/NAME_OF_ENVIRONMENT python=2.7.18

您可以列出环境:

conda env list

通过以下方式激活环境:

conda activate NAME_OF_ENVIRONMENT

现在您可以像往常一样使用 pip install <package>conda install <package>.

安装软件包等

注意:如果您的 Anaconda 安装是全新的,

  1. 可能会提示您运行 conda init一次,然后才能激活虚拟环境。

  2. 您可能必须 运行 下面才能找到虚拟环境(例如通过 conda list env),这将在 [=18= 中添加一行]:

    conda config --append envs_dirs /MY_FOLDER/NAME_OF_ENVIRONMENT

  3. 如果您对每次打开终端时可能显示“Py base”的文本感到恼火,请按照此 执行以下操作,这将在 [=18= 中添加一行]:

    conda config --set auto_activate_base false

  4. 激活环境后,要缩短在终端处于活动状态时出现在终端中的路径,请将此行添加到 ~/.condarc:

    env_prompt: ({name})


1 我不确定是不是因为与 Python 安装有关的东西在我的 Mac 上损坏了 – 所以你可能没有相同的问题。但是我 运行 遇到了所有解决方案的问题:从安装 virtualenv 到本机 MacOS Python 安装的所有内容都没有成功,构建错误时尝试通过 pyenv 安装 2.7.18,brew extract 方法也失败,等等

这应该有效!!!

wget https://bootstrap.pypa.io/pip/2.7/get-pip.py
python get-pip.py
pip install virtualenv # This will not work, use below
~/Library/Python/2.7/bin/pip  install virtualenv
~/Library/Python/2.7/bin/virtualenv --python=/usr/bin/python venv_twisted
source venv_twisted/bin/activate

对于出现在此处Apple 在 macOS 12.3python 中删除系统 python 后 的用户,这里是安装和 运行 python2 的方法和 python3.

Python 2

python, python2 -> python 2.7

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

# 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

Python 3

python3 -> python 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"

测试一下

~ % python --version
Python 2.7.16

~ % python2 --version
Python 2.7.16

~ % 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

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

# 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

与使用 Homebrew 无关,但 asdf-python 对我来说完美无缺。 (top-voted 答案似乎在我的系统上失败,M1 Max MBP with MacOS 12.3。其他一些答案似乎也很复杂并且没有用。)

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

我在使用 brew 时遇到错误,无法安装。

错误:不支持从 GitHub 提交 URL 安装 python@2! brew extract python@2 改为稳定点击 GitHub。

您可以直接从他们的网站 python 下载 version 2.7.18 and for list of available version click here