1. 通过 homebrew 安装 pyenv

brew update
brew install pyenv

2. 修改 zsh profile

否则通过pyenv切换python版本会不生效

# 编辑 .zshrc
vim ~/.zshrc

# 在配置下面增加
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/shims:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
 eval "$(pyenv init -)"
fi

# 让修改生效
source ~/.zshrc

3. pyenv 常用命令

# 查看 pyenv 已经管理了哪些 python 版本
pyenv versions
  system
  2.7.13
* 3.8.9 (set by /Users/yourname/.pyenv/version)

# 使用pyenv安装指定的Python版本
pyenv install -v 3.8.9  
pyenv install -v 2.7.13 

# 把Python切换到指定版本
pyenv global 3.8.9

# 查看系统当前python版本
python
  Python 3.8.9 (default, Jun 23 2021, 14:45:36)

# 查看pyenv当前支持哪些Python版本
pyenv install --list 

4.安装遇到的问题

OS X 11.0.1 pyenv No targets specified and no makefile found

解决办法:

sudo rm -rf /Library/Developer/CommandLineTools
xcode-select --install
/usr/bin/xcodebuild -version
but the last command resulted in

xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance
This led me to this other issue which suggested

sudo xcode-select --switch /Library/Developer/CommandLineTools
and now pyenv install 3.9.1 and pyenv install 3.9.0 went through without a problem.

Unfortunately I am still getting BUILD FAILED (OS X 11.2.1 using python-build 20180424) with
pyenv install 3.8.1