Mac升级Python3
Mac安全地升级Python3,不会删除Mac自带的Python2.x,homebrew安装新版本Python后,通过软连接来设置python访问的是python3。
Mac自带的python是2.7.x,这个太老了在运行python命令时会提示建议用python3,下面介绍两种方式安全地升级python3。
----------(相关知识拓展)
UNIX create a symbolic link command(创建软连接)
To create a symbolic link, enter:
$ ln -s {/path/to/file-name} {link-name}
$ ln -s /shared/sales/data/file.txt sales.data.txt
$ vi sales.data.txt
$ ls -l sales.data.txt
----------
方法一:升级最新版系统后使用系统自带的python3
升级到Mac 10.15后,安装最新版的Xcode,系统中有Python3,只需要创建软连接将/usr/local/bin/python指向系统的python3即可,其实/usr/bin/python3使用的是Xcode中的python(/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/Resources/Python.app/Contents/MacOS/Python)
wood@wutengfeideMacBook-Pro ~ % ln -s /usr/bin/python3 /usr/local/bin/python
wood@wutengfeideMacBook-Pro ~ % ln -s /usr/bin/pip3 /usr/local/bin/pip
wood@wutengfeideMacBook-Pro ~ % ls -al /usr/local/bin/pip
lrwxr-xr-x 1 wood admin 13 12 24 18:15 /usr/local/bin/pip -> /usr/bin/pip3
wood@wutengfeideMacBook-Pro ~ % ls -al /usr/local/bin/python
lrwxr-xr-x 1 wood admin 16 12 24 18:14 /usr/local/bin/python -> /usr/bin/python3
-------------------------------------------------------
方法二:通过homebrew安装python3(参考The right and wrong way to set Python 3 as default on a Mac)
brew install python
brew upgrade python
wood@wutengfeideMacBook-Pro ~ % which python
/usr/local/bin/python
wood@wutengfeideMacBook-Pro ~ % which python3
/usr/local/bin/python3
wood@wutengfeideMacBook-Pro ~ % unlink /usr/local/bin/python
wood@wutengfeideMacBook-Pro ~ % ln -s /usr/local/bin/python3 /usr/local/bin/python
wood@wutengfeideMacBook-Pro ~ % which pip
/usr/local/bin/pip
wood@wutengfeideMacBook-Pro ~ % which pip3
/usr/local/bin/pip3
wood@wutengfeideMacBook-Pro ~ % unlink /usr/local/bin/pip
wood@wutengfeideMacBook-Pro ~ % ln -s /usr/local/bin/pip3 /usr/local/bin/pip
这种处理方式有问题,新打开Terminal无效,需要同时配置~/.bash_aliases 和~/.bash_profile
--------(相关知识拓展)
/usr/bin 下面的都是系统预装的可执行程序,会随着系统升级而改变。
/usr/local/bin 目录是给用户放置自己的可执行程序的地方,推荐放在这里,不会被系统升级而覆盖同名文件。
如果两个目录下有相同的可执行程序,谁优先执行受到PATH环境变量的影响,比如我的一台服务器的PATH变量为。
wood@wutengfeideMacBook-Pro ~ % echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
可以看到 /usr/local/bin 是优先 /usr/bin 的,所以如果which python出现多个选项,则会受PATH的影响。