macOS 安装 python

本文档介绍了如何在macOS Sierra 10.12.1上使用HomeBrew安装Python,建议避免使用系统自带的Python,通过HomeBrew安装最新版本,并通过`brew switch`命令切换到正确版本。同时提到了Pip的升级和使用pip2或pip3确保与对应Python版本匹配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境:  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:
```sh
brew update
brew install python # or brew install python3
```
Now confirm that we're working with our newly installed Python:
```sh
ls -lh `which python`
```
...should show a symbolic link to a path with "Cellar" in it like:
```sh
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`命令切了一下, 就正常了, 结果如下:
```sh
?  ~ 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!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值