You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.

Step 1: Repair Homebrew and Your Python Installs

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:
    brew doctor
    
    Follow the on-screen prompts — it'll tell you if there are permission issues, missing dependencies, or broken links to fix.
  • Reinstall (or install) Python 3 via Homebrew to ensure a clean working copy:
    brew reinstall python@3
    
    If it wasn't installed before, swap reinstall with install.
  • 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):
    /usr/bin/python --version
    
    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).
Step 2: Set Up 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 python point to Python 2 for the current session:
    alias python="/usr/bin/python"
    
  • To switch back to Python 3:
    # For Intel Macs
    alias python="/usr/local/bin/python3"
    # For Apple Silicon (M-series) Macs
    alias python="/opt/homebrew/bin/python3"
    
    Verify with python --version after 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_profile or ~/.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, then Enter, then Ctrl+X), then apply the changes:
    source ~/.zshrc
    
    Now you can type python for your default version, or python2/python3 to 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 ~/.zshrc for 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-version file): pyenv local 2.7.18
  • List all installed versions:
    pyenv versions
    
Final Check

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:20:16