Thanks to jordanm,
The solution was to follow https://github.com/pyenv/pyenv/issues/1740#issuecomment-738749988
brew reinstall zlib bzip2
From people’s comments (thanks to Lukasz Czerwinski and Alex Veksler) it seems many view this next step as optional (and even dangerous) and prefer to skip it; though, it’s worth a try if nothing else is working:
sudo rm -rf /Library/Developer/CommandLineTools
xcode-select --install
Whether you chose to skip the last step or not, continue by editing .zshrc or .bashrc using:
nano ~/.zshrc or nano ~/.bashrc
and add:
export PATH="$HOME/.pyenv/bin:$PATH"
export PATH="/usr/local/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
export LDFLAGS="-L/usr/local/opt/zlib/lib -L/usr/local/opt/bzip2/lib"
export CPPFLAGS="-I/usr/local/opt/zlib/include -I/usr/local/opt/bzip2/include"
after editing and saving that file, run:
. ~/.zshrc or . ~/.bashrc
Then run the command below to install (changing 3.6.0 for the desired patch)
CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include" LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib" pyenv install --patch 3.6.0 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)