Mac使用Homebrew升级Python3.6.5后,旧版本3.4路径残留报错
Hey there, let's work through this path problem step by step—it's a super common hiccup when upgrading Python with Homebrew, so we'll get it sorted without needing to download from the Python website.
The error you're seeing means your shell is still clinging to the old Python 3.4 path that no longer exists, even though Homebrew installed 3.6.5 correctly. Here's how to fix the root cause:
Clear your shell's command cache first
Bash stores paths of recently run commands in a hash table to speed things up. Sometimes it holds onto the old Python 3 path even after you've installed a new version. Run this command to reset the cache:hash -rNow try
python3 --versionagain—this might fix it immediately.Check and update your PATH environment variable
Homebrew installs Python 3 to/usr/local/bin/python3, but if your PATH lists the old Python framework directory (/Library/Frameworks/Python.framework/Versions/3.4/bin) before/usr/local/bin, your shell will still look there first.- Run
echo $PATHto see your current path order./usr/local/binshould appear near the start. - If it doesn't, open your shell config file (usually
~/.bash_profilefor bash) in a text editor:nano ~/.bash_profile - Add this line at the top to prioritize Homebrew's binaries:
export PATH="/usr/local/bin:$PATH" - Save and exit (in nano, press
Ctrl+O, hitEnter, thenCtrl+X), then reload the config:source ~/.bash_profile
- Run
Check for invalid aliases
You might have an alias forcingpython3to point to the old 3.4 version. Run this to check:alias python3If you see output like
alias python3='/Library/Frameworks/Python.framework/Versions/3.4/bin/python3', remove that alias:- Open your shell config file (same as above:
~/.bash_profileor~/.bashrc) - Delete the line starting with
alias python3= - Reload the config with
source ~/.bash_profile
- Open your shell config file (same as above:
Re-link Homebrew's Python 3
Sometimes Homebrew's symlinks get messed up, makingpython3point to the wrong place. Fix the symlinks with these commands:brew unlink python3 brew link python3If you hit a permission error, add
sudobefore thebrew linkcommand (though Homebrew usually doesn't require this if your/usr/localpermissions are set correctly).Restart your terminal
If none of the above work, close and re-open your terminal. This ensures all config changes and cache resets take full effect.
After trying these steps, python3 --version should show Python 3.6.5 as expected.
内容的提问来源于stack exchange,提问作者Brosef

