环境: macOS Sierra 10.12.1
不要用mac系统自带的python, 用brew重新装一个,然后把/usr/local/bin/python指向brew目录下的python.
摘自: http://stackoverflow.com/questions/17271319/how-to-install-pip-on-mac-os-x
Installing a separate copy of Python is a popular option, even though Python already comes with MacOS. You take on the responsibility to make sure you're using the copy of Python you intend. But, the benefits are having the latest Python release and some protection from hosing your system if things go badly wrong.
To install Python using HomeBrew:
brew update
brew install python # or brew install python3
Now confirm that we're working with our newly installed Python:
ls -lh `which python`
...should show a symbolic link to a path with "Cellar" in it like:
lrwxr-xr-x 1 chris admin 35B Dec 2 13:40 /usr/local/bin/python -> ../Cellar/python/2.7.8_2/bin/python
**cyper notes: **我没有指到这个目录, 我用brew switch python x
命令切了一下, 就正常了, 结果如下:
➜ ~ brew switch python x
Error: python does not have a version "x" in the Cellar.
Versions available: 2.7.12_2
➜ ~ brew switch python 2.7.12_2
Cleaning /usr/local/Cellar/python/2.7.12_2
39 links created for /usr/local/Cellar/python/2.7.12_2
➜ ~ ls -lh `which python`
lrwxr-xr-x 1 cyper admin 36B Dec 5 16:31 /usr/local/bin/python -> ../Cellar/python/2.7.12_2/bin/python
➜ ~
Pip should be installed along with Python. You might want to upgrade it by typing:
pip install --upgrade pip
Now you're ready to install any of the 50,000+ packages on PyPI.
Other Notes
Formerly, I've used get-pip.py to install pip. But, the docs warn that get-pip.py does not coordinate with package managers and may leave your system in an inconsistent state. Anyway, there's no need, given that pip is now included with Python as of 2.7.9.
Note that pip isn't the only package manager for Python. There's also easy_install. It's no good to mix the two, so don't do it.
Finally, if you have both Python 2 and 3 installed, pip will point to whichever Python you installed last. Get in the habit of explicitly using either pip2 or pip3, so you're sure which Python is getting the new library.
Happy hacking!