Mac下安装Python2和Python3共存

  一般是python2默认安装了,python3没有安装,这时候一般使用命令:brew install python3 进行安装
  这里有个基础知识要记录一下,就是不同方法安装python的路径是不一样的,如下所示:

    来源Python安装路径
    系统安装路径/System/Library/Frameworks/Python.framework/Versions/2.7
    brew安装/usr/local/Cellar/python
    官网PKG安装/Library/Frameworks/Python.framework/Versions/2.7
  1. 控制台输入:vim ~/.bash_profile 并输入如下代码:
    # Setting PATH for Python 2.7
    PATH="/System/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
    export PATH
    # Setting PATH for Python 3.7.3
    PATH="/usr/local/Cellar/python/3.7.3/bin:${PATH}"
  2. 控制台输入:vim ~/.bashrc 完成别名的命名
    alias python2='/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7'
    alias python3='/usr/local/Cellar/python/3.7.3/bin/python3.7'
    alias python=python3
  3. 需要source一下
    source ~/.bash_profile
    source ~/.bashrc