Python安装:Mac 使用brew 安装Python2 和 Python3

  1. 安装python

    ## python2
    brew install python  
    
    ## python3
    brew install python3
    
  2. 出现错误

    Error: An unexpected error occurred during the `brew link` step
    The formula built, but is not symlinked into /usr/local
    Permission denied @ dir_s_mkdir - /usr/local/Frameworks
    Error: Permission denied @ dir_s_mkdir - /usr/local/Frameworks
    
  3. 解决

    手动创建目录

    $ sudo mkdir /usr/local/Frameworks
    $ sudo chown $(whoami):admin /usr/local/Frameworks
    
  4. 查看版本

    $ which python
    /usr/bin/python
    
    $ python --version
    Python 2.7.16
    
  5. 设置环境变量

    ## 打开配置文件,如果没有就在~目录下面创建一个
    $ open ~/.bash.profile
    

    设置python环境变量路径

    # Setting PATH for Python 2.7 系统自带python2
    PATH="/System/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"  -
    export PATH
    
    # Setting PATH for Python 3.7.5 brew安装的python3
    PATH="/usr/local/Cellar/python/3.7.5/bin:${PATH}"
    
    ## 设置别名 注意目录看下是否正确
    alias python2='/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7'
    alias python3='/usr/local/Cellar/python/3.7.5/bin/python3'
    # alias python=python3
    

    生效配置文件

    source ~/.bash.profile 
    
  6. 验证

    ## python 和 python2 都指向python2
    $ python --version
    $ python2 --version
    $ python3 --version
    

你可能感兴趣的:(Python安装:Mac 使用brew 安装Python2 和 Python3)