【问题标题】:How to reinstall python@2 from Homebrew?如何从 Homebrew 重新安装 python@2?
【发布时间】:2020-06-03 12:20:29
【问题描述】:

我在使用 brew 时遇到了 openssl 和 python@2 问题,which have explained here (unresolved)。记录的重新安装 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、公关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 macos homebrew python-2.x


    【解决方案1】:

    看来自制软件的工作人员确实尽可能地在 macOS 上使用 Python 2.7。

    1. 链接的brew extract 链接确实没有帮助,您需要在此处寻找有关如何从提取的资源中自己挖掘的答案。
    2. 链接的提交: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 历史中的提交是尽可能稳定的。

    【讨论】:

    • 只需将 PR 的链接放在这里,python@2 已被删除(2020 年 2 月 4 日合并)github.com/Homebrew/homebrew-core/pull/49796
    • @petschki 不是我们想要的答案,而是我们想知道的答案!
    • 这 (brew install &lt;raw url&gt;) 似乎不再适用于当前版本的 Homebrew,它会产生以下错误:Error: Calling Installation of python@2 from a GitHub commit URL is disabled! Use 'brew extract python@2' to stable tap on GitHub instead.
    • 我就是这样做的:cd ~ - wget https://raw.githubusercontent.com/Homebrew/homebrew-core/86a44a0a552c673a05f11018459c9f5faae3becc/Formula/python@2.rb - brew install python@2.rb
    • 上次提交后无法正常工作...==&gt; Downloading https://ghcr.io/v2/homebrew/core/python/2/manifests/2.7.17_1 ##O#- # curl: (22) The requested URL returned error: 404 Error: Failed to download resource "python@2_bottle_manifest" Download failed: https://ghcr.io/v2/homebrew/core/python/2/manifests/2.7.17_1
    【解决方案2】:

    如何从本地水龙头安装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/&lt;user&gt;/homebrew-python2 中创建一个新的本地点击模板。分接头名称需要&lt;user&gt;&lt;repo&gt; 组件,由/ 分隔。实际值是任意的。上面的命名遵循How to Create and Maintain a Tap 的约定。如果您想将点击推送到 GitHub,您可以使用您的 GitHub 用户名作为用户。推送到 GitHub 不是必需的(并且没有在上面的说明中执行)。

    brew extract 命令从 repos 历史记录中提取公式的最新版本到给定的(本地)tap。在我们的例子中,python@2.7.17.rb 被提取出来。

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

    为什么需要这样做?

    上面讨论的方法(从 GitHub 提交 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.
    

    【讨论】:

    • 这对我尝试安装旧版本的 openssl 很有帮助。注意:上例中的&lt;user&gt; 是您的Github 用户名。
    • 感谢您提供更新的答案。 不必是你的 github 用户名;它可以是任何东西,也可以什么都不是。在本地计算机上使用您的登录名是避免它与您计算机上的其他用户(如果有的话)发生冲突的好方法。
    • 这应该是答案,因为接受的答案不再有效。
    • 不幸的是,这些说明对我不起作用。尝试brew extract python@2 &lt;user&gt;/homebrew-python2 时,我得到:Error: python@2: undefined method `[]' for nil:NilClass
    【解决方案3】:

    你可以使用pyenv来安装python:

    brew install pyenv
    pyenv install 2.7.18
    

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

    pyenv global 2.7.18
    

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

      • 这行得通,但这也重新安装了我的 Python3。这意味着它删除了我随它安装的所有软件包。不过还是谢谢。
      【解决方案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
      

      【讨论】:

        【解决方案6】:

        在 MacOS Monterey 中,此页面上的所有答案均不适合我1。如果这对任何人都有帮助,这里有一个替代解决方案,从技术上讲,它不是通过 Homebrew 直接安装的——只是间接安装。

        对我来说可行的简单解决方案是通过 Homewbrew 安装 Anaconda,然后为 Python 2.7 创建一个虚拟环境。

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

        Anaconda 是 Python 和 R 编程语言的发行版 用于科学计算(数据科学、机器学习应用、 大规模数据处理、预测分析等),旨在 简化包管理和部署。该发行版包括适用于 Windows、Linux 和 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 &lt;package&gt;conda install &lt;package&gt; 安装软件包等。

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

        1. 在激活虚拟环境之前,系统可能会提示您运行一次conda init

        2. 您可能必须运行以下命令才能找到虚拟环境(例如通过conda list env),这将在~/.condarc 中添加一行:

          conda config --append envs_dirs /MY_FOLDER/NAME_OF_ENVIRONMENT

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

          conda config --set auto_activate_base false

        4. 激活环境后,若要缩短在终端激活时出现的路径,请将这一行添加到~/.condarc

          env_prompt: ({name})


        1 我不确定这是否是因为我的 Mac 上与 Python 安装有关的某些东西被破坏了——所以你可能不会遇到同样的问题。但是我遇到了所有解决方案的问题:从为本地 MacOS Python 安装安装 virtualenv 的所有内容都没有成功,尝试通过 pyenv 安装 2.7.18 时出现构建错误,brew extract 方法也失败了,等等.

        【讨论】:

          【解决方案7】:

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

          也许 Python 2.7 越来越难安装,或者我的新 macOS Big Sur 11.4 导致了这个问题。

          通过重用本机 Python 2.7.16,我能够以这种方式设置工作的 Python 2.7 环境

          • 手动安装 pip
          • 设置虚拟环境
          • 在虚拟环境中使用 pip 安装依赖项

          安装步骤如下:

          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
          

          【讨论】:

          • virtualenv 使用这种方法安装在哪里?我得到了command not found: virtualenv,尽管它似乎已经安装在某个地方。
          猜你喜欢
          • 2020-06-24
          • 2013-09-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-31
          • 2013-08-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多