Mac OS X 上快速切换Python2和Python3

Max OS X 自带python 2,直接输入命令即可查看版本:

$ python

输出:

Python 2.7.10 (default, Oct  6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

那么,问题来了,我们想要使用Python 3 怎么办?如何还能快速切换?

如果只是本机使用,那么有一个非常简单的办法,使用配置文件中的别名——alias——即可。

安装Python 3

使用brew安装:

$ brew install python3

成功后,先看下Python安装地址:

$ which python

输出:

/usr/bin/python

这是python2 的命令地址,完整地址是在:
/System/Library/Frameworks/Python.framework/Versions/2.7/
再来看看 python 3的命令地址:

$ which python3

输出:

/usr/local/bin/python3

使用brew安装的实际地址是在:

/usr/local/Cellar/python/3.7.0/

如果是用官网的pkg安装的话,实际地址是在:

/Library/Frameworks/Python.framework/Versions/3.7

编辑配置文件:

vi ~/.bash_profile、~/.bashrc 或 ~/.zshrc

追加如下配置:

alias python2="/usr/bin/python"
alias python3="/usr/local/bin/python3"
alias python=python3

生效配置,即可使用python3的版本,后续按照自己的使用来调整即可。

$ source ~/.zshrc
    原文作者:mickjoust
    原文地址: https://blog.csdn.net/mickjoust/article/details/82345504
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞