macOS系统下新版本发布时,如何更新已安装的Python 3.6.5?
Hey there! Since you're running macOS with Python 3.6.5 already, let's walk through the most reliable ways to update to a newer Python version—no messy system overrides, promise:
方法1:官方安装包(最简单直接)
If you want a no-fuss, straightforward update, go with the official Python installer:
- Head to the official Python website and download the latest stable release's
.pkginstaller. - Open the downloaded file and follow the installation wizard. By default, it’ll install the new version in
/Library/Frameworks/Python.framework/Versions/. - After installation, check your current Python version with
python3 --version. If it still shows 3.6.5, you’ll need to add the new version to your system path:- Open your shell config file (either
~/.zshrcfor Zsh or~/.bash_profilefor Bash) in a text editor. - Add this line, replacing
[NEW_VERSION]with the actual version number (like3.12):export PATH="/Library/Frameworks/Python.framework/Versions/[NEW_VERSION]/bin:$PATH" - Save the file, then run
source ~/.zshrc(orsource ~/.bash_profile) to apply the changes. Nowpython3 --versionshould show the new version.
- Open your shell config file (either
方法2:Homebrew更新(适合已用Homebrew的用户)
If you already use Homebrew for package management, this is super smooth:
- First, make sure Homebrew is up to date:
brew update - Install the latest Python release:
brew install python - Homebrew will automatically set the new Python version as your default. Verify with
python3 --version. - If you have old Python versions lingering, clean them up with:
brew cleanup python
方法3:pyenv(推荐管理多版本)
If you need to switch between multiple Python versions (like keeping 3.6.5 for old projects and using a new version for others), pyenv is your best bet—it keeps versions isolated so you don’t mess up your system or existing projects:
- Install pyenv via Homebrew:
brew install pyenv - Configure your shell to use pyenv. Open your
~/.zshrcor~/.bash_profileand add these lines:export PYENV_ROOT="$HOME/.pyenv" command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH" eval "$(pyenv init -)" - Save the file and run
source ~/.zshrc(or your respective config file) to activate the changes. - List all available Python versions you can install:
pyenv install --list - Install the version you want (replace
3.12.0with your target version):pyenv install 3.12.0 - Set the new version as your global default:
pyenv global 3.12.0 - Verify with
python --version(pyenv makespythonpoint to your selected version now).
Quick Notes:
- Never mess with the system Python: macOS relies on its built-in Python version for system tools. All methods above install separate, independent Python versions so you won’t break your OS.
- When using
pip, the new Python version will have its ownpip(usuallypip3). Runpip3 install [package-name]to install packages for the new version.
内容的提问来源于stack exchange,提问作者berkb
相关产品推荐
相关产品推荐

