【问题标题】:How to default Python3.8 on my Mac using Homebrew?如何使用 Homebrew 在我的 Mac 上默认 Python3.8?
【发布时间】:2023-03-23 17:29:01
【问题描述】:

我已将我的 python 3 更新到最新版本 3.8:

brew search python
==> Formulae
app-engine-python   gst-python          python ✔            python@3.8 ✔
boost-python        ipython             python-markdown     wxpython
boost-python3       micropython         python-yq

==> Casks
awips-python               kk7ds-python-runtime       mysql-connector-python

但是当我在我的 mac 上查看 python3 版本时,它仍然显示 3.7:

python3 --version

Python 3.7.6

如何使用 Homebrew 将 python3 默认为最新的 3.8 版本?

编辑: 当我尝试使用 brew switch 时,它告诉我我只安装了 python 3.7.6,但最后一个 brew upgrade 我很确定 python3.8.1 是随 Homebrew 一起安装的

brew switch python 3.8.1
python does not have a version "3.8.1" in the Cellar.
python's installed versions: 3.7.6_1

【问题讨论】:

标签: python-3.x linux macos homebrew


【解决方案1】:

解决方法如下:

如果现有的符号链接属于 python 3.7,您应该取消链接它们:
brew unlink python

基本上所有你需要做的:
brew link --force python@3.8

或强制链接并覆盖所有冲突的文件:
brew link --force --overwrite python@3.8

或者,如果需要,列出所有将被删除的文件:
brew link --overwrite --dry-run python@3.8

因此,您可以切换到 Homebrew 存储库中可用的任何 python 版本。

还可以查看this 答案以了解pyenv 用法

【讨论】:

    【解决方案2】:

    好的,感谢来自 Homebrew 社区讨论的@gromgit (https://discourse.brew.sh/t/how-to-default-python-3-8-on-my-mac-using-homebrew/7050)

    解决办法如下:

    $ brew info python@3.8
    python@3.8: stable 3.8.1 (bottled) [keg-only]
    ...
    ==> Caveats
    Python has been installed as
      /usr/local/opt/python@3.8/bin/python3
    ...
    python@3.8 is keg-only, which means it was not symlinked into /usr/local,
    because this is an alternate version of another formula.
    
    If you need to have python@3.8 first in your PATH run:
      echo 'export PATH="/usr/local/opt/python@3.8/bin:$PATH"' >> ~/.bash_profile
    
    For compilers to find python@3.8 you may need to set:
      export LDFLAGS="-L/usr/local/opt/python@3.8/lib"
    
    For pkg-config to find python@3.8 you may need to set:
      export PKG_CONFIG_PATH="/usr/local/opt/python@3.8/lib/pkgconfig"
    

    此时我会坚持python(v3.7.6),等待后续版本无缝升级v3.8.1。

    【讨论】:

      【解决方案3】:

      您可能必须在 ~/.bash_profile 中将 python 3.8.1 添加到您的 PATH 中并将其放在首位,以便它覆盖以前的安装。首先找出 python 3.8.1 的安装位置,然后将其添加到您的路径中,如下所示:

      导出 PATH="/PATH_TO_PYTHON/:${PATH}"

      【讨论】:

      • python@3.8: stable 3.8.1 (bottled) [keg-only] Interpreted, interactive, object-oriented programming language https://www.python.org/ /usr/local/Cellar/python@3.8/3.8.1 (4,127 files, 63.4MB)
      【解决方案4】:

      我有一台预装了 Python 2.7 的公司 Mac 来运行旧软件。

      $ brew install pyenv (successful)
      $ pyenv install 3.9.2 (successful)
      $ python --version
      Python 2.7
      $ pyenv global 3.9.2
      $ python --version
      Python 2.7 
      

      它仍然显示 Python 2.7,所以,我执行了以下命令

      pyenv init
      eval "$(pyenv init -)"
      
      $ python --version
      Python 3.9.2
      

      更多详情: Link

      【讨论】:

        猜你喜欢
        • 2021-02-28
        • 2012-08-24
        • 2012-12-05
        • 1970-01-01
        • 2011-11-03
        • 2017-04-04
        • 1970-01-01
        • 2016-09-01
        • 1970-01-01
        相关资源
        最近更新 更多