MacOS重装pip报错:/lib/python2.7/site-packages/不支持.pth文件
Hey there, let’s work through this pip reinstall issue you’re hitting on your Mac. That error about /lib/python2.7/site-packages/ not supporting .pth files and being outside your PYTHONPATH is a typical hiccup with older Python 2.7 setups (even though Python 2 is end-of-life, I totally get needing it for legacy tools). Here’s how to fix it:
First, check and update your PYTHONPATH
The error says the target directory isn’t in your PYTHONPATH, so let’s fix that:- Run this in terminal to see your current PYTHONPATH:
echo $PYTHONPATH - If
/lib/python2.7/site-packages/isn’t listed, add it temporarily with:export PYTHONPATH="/lib/python2.7/site-packages/:$PYTHONPATH" - To make this permanent:
- For bash users: Edit
~/.bash_profileand add the export line above, then runsource ~/.bash_profileto apply changes. - For zsh users: Edit
~/.zshrcinstead, then runsource ~/.zshrc.
- For bash users: Edit
- Run this in terminal to see your current PYTHONPATH:
Fix directory permissions
Sometimes the issue is just wonky permissions on the site-packages folder. Let’s verify and fix that:- Check current permissions:
ls -ld /lib/python2.7/site-packages/ - If the owner isn’t your user, reset ownership with:
sudo chown -R $(whoami):staff /lib/python2.7/site-packages/
- Check current permissions:
Skip easy_install—use get-pip.py directly
easy_install is pretty outdated, so using the official get-pip script is often more reliable for Python 2.7:- Download the Python 2.7-compatible get-pip.py script (available from PyPA’s legacy bootstrap resources).
- Install pip with:
sudo python2.7 get-pip.py
Verify the install
Once done, run this to confirm pip is working:pip2 --version
A quick side note: Python 2.7 has been unsupported since 2020, so if you can migrate your projects to Python 3, you’ll avoid a lot of these legacy issues going forward. But if you need Python 2.7 for specific tools, the steps above should get you sorted.
内容的提问来源于stack exchange,提问作者Garrett Perkins

