macOS M1芯片下使用Homebrew管理多个Python版本安装记录

一、安装Homebrew(如果还未安装)

国内加速版 推荐

正常安装版(慢)

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

确保你的PATH中已加入/opt/homebrew/bin,可以执行:

echo 'export PATH="/opt/homebrew/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

二、安装Python多个版本

1. 安装Python 3.10

brew install python@3.10
# 输出确认安装路径
brew --prefix python@3.10

示例输出:

/opt/homebrew/opt/python@3.10

2. 安装Python 3.12

brew install python@3.12
brew --prefix python@3.12

示例输出:

/opt/homebrew/opt/python@3.12

三、确认安装内容

ls /opt/homebrew/opt/python@3.10/bin/
# 应看到有:python3.10、pip3.10等

四、设置别名方便调用

编辑你的 ~/.zshrc,添加:

# Python 3.10
alias python3.10="/opt/homebrew/opt/python@3.10/bin/python3.10"

# Python 3.12
alias python3.12="/opt/homebrew/opt/python@3.12/bin/python3.12"

保存后,执行:

source ~/.zshrc

五、切换版本(示例)

切换到 Python 3.10:

python3.10 --version

切换到 Python 3.12:

python3.12 --version

六、用brew link方式管理(可选)

如果想让python3指向某个版本:

# 切换到 Python 3.10
brew unlink python@3.12
brew link --force --overwrite python@3.10
# 切换到 Python 3.12
brew unlink python@3.10
brew link --force --overwrite python@3.12

七、总结

  • 安装路径:
    • Python 3.10:python@3.10"">`/opt/homebrew/opt/python@3.10/bin/python3.10`
    • Python 3.12:python@3.12"">`/opt/homebrew/opt/python@3.12/bin/python3.12`
  • 管理方式:用 alias 快速调用
  • 版本切换:用 brew unlinkbrew link
作者:admin  创建时间:2025-10-24 22:17
最后编辑:admin  更新时间:2025-10-24 22:21