MacOS Sierra(10.12.6)下安全升级Python 2.x至2.7.14的方法咨询
Hey there, let’s walk through the safest, most reliable way to get Python 2.7.14 installed on your MacOS Sierra system—especially since that unexpected brew upgrade rolled your version back. The key here is avoiding changes to your system’s default Python (which macOS relies on for core tools) while getting the exact 2.7.x version you need. Here’s my go-to approach:
Use pyenv (The Cleanest, Most Isolated Method)
pyenv is a tool that lets you install and switch between multiple Python versions without messing with system files. It’s the safest option because it keeps your custom Python versions completely separate from macOS’s built-in one.
Install pyenv via Homebrew
First, make sure your Homebrew is up to date to avoid any formula conflicts:brew update brew install pyenvConfigure your shell to use pyenv
You need to add pyenv to your shell’s startup file so it loads every time you open a terminal.- If you use Bash, run these commands to update your
~/.bash_profile:echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile echo 'eval "$(pyenv init --path)"' >> ~/.bash_profile - If you use Zsh, update your
~/.zshrcinstead:echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc echo 'eval "$(pyenv init --path)"' >> ~/.zshrc
Then reload your shell settings:
# For Bash source ~/.bash_profile # For Zsh source ~/.zshrc- If you use Bash, run these commands to update your
Install Python 2.7.14
Now you can install the exact version you want with pyenv:pyenv install 2.7.14Pyenv will handle downloading, compiling, and installing the version in its own isolated directory—no system changes required.
Set Python 2.7.14 as your default
- To use 2.7.14 globally across all terminal sessions, run:
pyenv global 2.7.14 - If you only want to use it for a specific project directory, navigate to that folder and run:
pyenv local 2.7.14
This creates a
.python-versionfile in the directory that tells pyenv which version to use there.- To use 2.7.14 globally across all terminal sessions, run:
Verify the installation
Open a new terminal window to ensure the shell settings take effect and check your Python version:python --versionYou should see
Python 2.7.14printed out.
Why Avoid Modifying System Python or Homebrew’s Python@2?
- macOS Sierra’s built-in Python 2.7 is used by system utilities like
softwareupdateandinstaller—changing or overwriting it can break critical system functions. - Homebrew’s
python@2formula was archived after Python 2 reached end-of-life, so it may not provide the latest 2.7.14 version anymore, and maintaining it can lead to conflicts with other brew packages.
Give that a shot—this method keeps your system stable while giving you the exact Python version you need. If you run into any issues like compilation errors during the pyenv install, let me know and I can help troubleshoot!
内容的提问来源于stack exchange,提问作者Joe

