如何用Homebrew卸载Python2.7.10?保留Python3.7解决环境问题
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:
Verify your Homebrew Python 3.7 installation
First, make sure your existing Python 3.7 is healthy:brew info python@3.7Jot down the installation path (usually
/usr/local/Cellar/python@3.7/3.7.2_1) for reference.Uninstall Homebrew-installed Python 2.7 (if you have it)
Check if you installed Python 2 via Homebrew:brew list | grep python@2If it shows up, remove it completely:
brew uninstall --force python@2The
--forceflag ensures all associated files are deleted.Handle the system-default Python 2.7
Instead of deleting it directly (risky!), we can back it up and redirect thepythoncommand 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
pythoncalls 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.7If you run into issues later, restore the backup with:
sudo mv /usr/bin/python2.7.backup /usr/bin/python- Back up the system Python first:
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:
Uninstall all Homebrew Python packages
List all Python-related packages installed via Homebrew:brew list | grep pythonUninstall them all with force to remove every trace:
brew uninstall --force python@2 python@3.7 pythonClean 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/pipReinstall Python 3.7 via Homebrew
First, update Homebrew to make sure you get the correct version:brew updateInstall your desired Python 3.7 version:
brew install python@3.7Confirm the installation path with:
which python3It should show
/usr/local/bin/python3.Set Python 3.7 as the default
Add these lines to your shell config file (either~/.bash_profilefor Bash or~/.zshrcfor Zsh) to makepythonandpipdefault 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_profileRefresh the config to apply changes immediately:
source ~/.bash_profileVerify and test
Restart your terminal, then check the versions:python --version pip --versionBoth 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

