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

如何用Homebrew卸载Python2.7.10?保留Python3.7解决环境问题

Fixing Python Version Issues on Mac 10.14.2 with Homebrew

Hey there! Let's work through this Python version problem step by step. You've got Python 2.7.10 (the system default) and Python 3.7.2_1 installed, but can't use virtualenv or Flask—so you want to either remove Python 2.7 entirely or wipe all Python versions and reinstall 3.7. Here are two solid Homebrew-based solutions:

Option 1: Keep Python 3.7, Remove Python 2.7 (Proceed with Caution)

First, a critical note: The Python 2.7 that comes pre-installed with Mac 10.14 is tied to some system tools. Deleting it outright might break things like older system scripts, so think carefully before going this route. If you're set on it, here's how:

  1. Verify your Homebrew Python 3.7 installation
    First, make sure your existing Python 3.7 is healthy:

    brew info python@3.7
    

    Jot down the installation path (usually /usr/local/Cellar/python@3.7/3.7.2_1) for reference.

  2. Uninstall Homebrew-installed Python 2.7 (if you have it)
    Check if you installed Python 2 via Homebrew:

    brew list | grep python@2
    

    If it shows up, remove it completely:

    brew uninstall --force python@2
    

    The --force flag ensures all associated files are deleted.

  3. Handle the system-default Python 2.7
    Instead of deleting it directly (risky!), we can back it up and redirect the python command to 3.7:

    • Back up the system Python first:
      sudo mv /usr/bin/python /usr/bin/python2.7.backup
      
    • Create a symlink to Python 3.7 so python calls it by default:
      sudo ln -s /usr/local/bin/python3 /usr/bin/python
      

    If you absolutely must delete the system Python 2.7 files (not recommended), you can run these commands—but be aware this might break system tools:

    sudo rm -rf /Library/Frameworks/Python.framework/Versions/2.7
    sudo rm -rf /usr/bin/python2.7
    sudo rm -rf /usr/bin/pip2.7
    

    If you run into issues later, restore the backup with:

    sudo mv /usr/bin/python2.7.backup /usr/bin/python
    

Option 2: Wipe All Python Versions, Reinstall Python 3.7 (Safer Choice)

This is the more reliable approach because it avoids touching the system's default Python entirely. Let's clean everything up and start fresh:

  1. Uninstall all Homebrew Python packages
    List all Python-related packages installed via Homebrew:

    brew list | grep python
    

    Uninstall them all with force to remove every trace:

    brew uninstall --force python@2 python@3.7 python
    
  2. Clean up leftover files
    Delete residual Python files from user and system directories:

    rm -rf ~/Library/Python
    sudo rm -rf /Library/Frameworks/Python.framework
    rm -rf ~/.pip
    rm -rf ~/.cache/pip
    
  3. Reinstall Python 3.7 via Homebrew
    First, update Homebrew to make sure you get the correct version:

    brew update
    

    Install your desired Python 3.7 version:

    brew install python@3.7
    

    Confirm the installation path with:

    which python3
    

    It should show /usr/local/bin/python3.

  4. Set Python 3.7 as the default
    Add these lines to your shell config file (either ~/.bash_profile for Bash or ~/.zshrc for Zsh) to make python and pip default to 3.7:

    echo 'export PATH="/usr/local/opt/python@3.7/bin:$PATH"' >> ~/.bash_profile
    echo 'alias python="python3"' >> ~/.bash_profile
    echo 'alias pip="pip3"' >> ~/.bash_profile
    

    Refresh the config to apply changes immediately:

    source ~/.bash_profile
    
  5. Verify and test
    Restart your terminal, then check the versions:

    python --version
    pip --version
    

    Both should show Python 3.7.2. Now you can install virtualenv and Flask without issues:

    pip install virtualenv flask
    

Final Notes

  • Option 2 is highly recommended because it avoids messing with system components that could cause unexpected problems.
  • If you go with Option 1 and run into system errors, restoring the backed-up Python 2.7 file should fix things.

内容的提问来源于stack exchange,提问作者Eddie Avinashi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 09:30:33