轶哥

📚 Having fun with AI Agent. Always learning.

    brew安装python2
    •   更新:2022-04-17 14:56:43
    •   首发:2021-02-03 14:18:06
    •   教程
    •   32133

    Homebrew是MacOS中绝佳的包管理工具。通过brew安装pyton3非常容易brew install python@3,将会自动添加python3pip3命令。

    关于python的安装,在Homebrew官网给出了详细的说明:https://docs.brew.sh/Homebrew-and-Python

    Homebrew provided a python@2 formula until the end of 2019, at which point it was removed due to the Python 2 deprecation.

    根据提示,我们知道python@2已经被移除。在2020年初,python@2仍然可以正常安装,但就在不久前,官方移除了python@2。我对Homebrew官方的操作表示支持和理解,毕竟技术的更新和迭代是不可逆转的,是需要大家一起支持的。

    Python官方宣布 2020 年 1 月后不再更新维护 Python2,然而由于历史原因以及各国生态差异,某些情况下不得不再次安装python2。即使距离python3最初版本发布已经超过10年,仍然有很多项目依赖python2(这里没有吐槽的意思,但JavaScript生态必须感谢babel)。

    brew安装python2的方式:

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

    安装python2是不被推荐的。

    其它说明

    1. 通过brew extract安装python2的方式已经失效了,参见
    2. 上述安装方法参考自:https://stackoverflow.com/questions/60298514/how-to-reinstall-python2-from-homebrew
    3. 在M1芯片的Mac设备上,此安装方式不适用于原生支持 ARM 架构的Homebrew实验性版本。需要安装Rosetta加持的X86版本Homebrew,然后使用上述方法进行安装。
    4. 如果上述wget命令执行失败,有可能是网络原因导致的。可以考虑使用 https://cdn.wyr.me/files/2021-02-03/python@2.rb 地址代替。

    M1芯片使用Intel版本Homebrew

    来自 https://stackoverflow.com/questions/64963370/error-cannot-install-in-homebrew-on-arm-processor-in-intel-default-prefix-usr

    For what it's worth, before installing Homebrew you will need to install Rosetta2 emulator for the new ARM silicon (M1 chip). I just installed Rosetta2 via terminal using:

    /usr/sbin/softwareupdate --install-rosetta --agree-to-license

    This will install rosetta2 with no extra button clicks.

    After installing Rosetta2 above you can then use the Homebrew cmd and install Homebrew for ARM M1 chip: arch -x86_64 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

    Once Homebrew for M1 ARM is installed use this Homebrew command to install packages: arch -x86_64 brew install <package>

    特别提示

    从MacOS 12.3 Beta版本开始,Apple不再内置python2且将无法正常安装python2,无论是intel芯片还是Apple芯片的设备都无法安装。原因是/usr/bin/python的软链接无法正常被删除或覆盖。

    2022年04月17日14:02:24更新: 从MacOS 12.4 Beta版(21F5048e) 开始,可以通过pyenv在intel和Apple芯片中安装python2。

    例如在M1中安装 2.7.18 版本的 python2。

    brew install pyenv
    pyenv install 2.7.18
    export PATH="$(pyenv root)/shims:${PATH}"
    pyenv global 2.7.18
    python --version
    

    如果一切顺利,将可以看到Python 2.8.18的输出。

    需要将上述路径添加到环境变量里面,例如:

    echo 'PATH=$(pyenv root)/shims:$PATH' >> ~/.zshrc
    

    此方法可以与brew install python3方式安装的python3共存。

    如果遇到VS(Visual Studio Code)无法使用code命令,可以参阅博文:《MacOS 12.3 无法正常使用code命令的解决方法》

    打赏
    交流区(3)
    咖啡兔

    已顺利安装python2,多谢分享!

    2022年4月24日 08:35回复
    轶哥

    🎉️

    2022年4月24日 08:36回复
    詹文平

    Apple芯片安装python2.7加速

    v=2.7.18; curl -L https://npm.taobao.org/mirrors/python/$v/Python-$v.tar.xz -o ~/.pyenv/cache/Python-$v.tar.xz; pyenv install $v

    2023年10月24日 06:20回复
    尚未登陆
    发布
      上一篇 (win10链路聚合,双网卡带宽叠加负载均衡)
    下一篇 (JavaScript练习题)  

    评论回复提醒