安装Homebrew

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

别忘记添加环境变量

1
2
(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/nanyancc/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"

查找并安装Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
nanyancc@nanyanccdeMacBook-Air ~ % brew search python3
==> Formulae
boost-python3 python@3.11 python@3.8 ipython jython
python@3.10 python@3.12 python@3.9 bpython cython

If you meant "python3" specifically:
It was migrated from homebrew/cask to homebrew/core.

nanyancc@nanyanccdeMacBook-Air ~ % brew install python@3.12
......
......
Python has been installed as
/opt/homebrew/bin/python3

Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
/opt/homebrew/opt/python@3.12/libexec/bin

See: https://docs.brew.sh/Homebrew-and-Python

添加 Homebrew 安装的 Python 和 pip 路径

1
2
3
nano ~/.zshrc  # 如果使用的是 zsh
# 或者
nano ~/.bash_profile # 如果使用的是 bash

在打开的配置文件末尾添加以下内容:

1
export PATH="/opt/homebrew/opt/python@3.12/libexec/bin:$PATH"

立即生效

1
2
3
source ~/.zshrc  # 如果使用的是 zsh
# 或者
source ~/.bash_profile # 如果使用的是 bash

完成以上步骤后,pippython 命令将会使用 Homebrew 安装的版本。你可以通过以下命令验证路径是否正确:

1
2
which pip
which python

这应该会输出 /opt/homebrew/opt/python@3.12/libexec/bin/pip/opt/homebrew/opt/python@3.12/libexec/bin/python