HomeBrew破坏Python安装后,如何在macOS切换Python2/3?
Got it, let's fix your Python environment and set up that version switching you need. Let's break this down into manageable steps since you messed up your Homebrew setup earlier.
First, let's get Homebrew back on track, then make sure your Python versions are intact:
- Run Homebrew's diagnostic tool to spot and fix issues:
Follow the on-screen prompts — it'll tell you if there are permission issues, missing dependencies, or broken links to fix.brew doctor - Reinstall (or install) Python 3 via Homebrew to ensure a clean working copy:
If it wasn't installed before, swapbrew reinstall python@3reinstallwithinstall. - Check if macOS's built-in Python 2 is still intact (Homebrew no longer supports Python 2, so the system version is your best bet):
If this returns a Python 2.x version, you're good to go. If not, you may need to restore system defaults (though this is rare unless you overwrote the system Python)./usr/bin/python --version
python Command Switching Between Python 2 and 3 There are a few ways to do this, pick the one that fits your workflow best:
Option 1: Temporary Aliases (Per Terminal Session)
Great for one-off switches without changing your default setup:
- To make
pythonpoint to Python 2 for the current session:alias python="/usr/bin/python" - To switch back to Python 3:
Verify with# For Intel Macs alias python="/usr/local/bin/python3" # For Apple Silicon (M-series) Macs alias python="/opt/homebrew/bin/python3"python --versionafter each command.
Option 2: Permanent Aliases (All Terminal Sessions)
If you want a default python version plus easy access to both:
- Open your shell config file (Mac uses zsh by default, so it's
~/.zshrc; for bash it's~/.bash_profileor~/.bashrc):nano ~/.zshrc - Add these lines at the end (adjust paths if needed for your Mac type):
alias python2="/usr/bin/python" alias python3="/usr/local/bin/python3" # Use /opt/homebrew/bin/python3 for M-series alias python=python3 # Set Python 3 as default; change to python2 if you prefer - Save and exit (for nano:
Ctrl+O, thenEnter, thenCtrl+X), then apply the changes:
Now you can typesource ~/.zshrcpythonfor your default version, orpython2/python3to explicitly use either.
Option 3: Pyenv (Professional Version Management)
Perfect if you switch between multiple Python versions regularly (not just 2 and 3):
- Install pyenv via Homebrew:
brew install pyenv - Add pyenv to your shell config (again, using
~/.zshrcfor zsh):echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc echo 'eval "$(pyenv init -)"' >> ~/.zshrc - Apply the changes:
source ~/.zshrc - Install the Python versions you need:
pyenv install 2.7.18 # Last stable Python 2 release pyenv install 3.11.4 # Example stable Python 3 release (check for newer versions!) - Set a global default version:
pyenv global 3.11.4 - Switch versions temporarily:
- For the current terminal session:
pyenv shell 2.7.18 - For the current directory (persists in a
.python-versionfile):pyenv local 2.7.18
- For the current terminal session:
- List all installed versions:
pyenv versions
No matter which method you choose, verify everything works by running:
python --version python2 --version python3 --version
You should see the correct versions for each command.
内容的提问来源于stack exchange,提问作者Alexander Mills

