首先去Python官网下载最新的Python安装包。

安装完成需要添加环境变量,编辑 ~/.bash_profile

# Setting PATH for Python 3.7
 # The original version is saved in .bash_profile.pysave
 export PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}"
 alias python="/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7" # 新增
 export PATH

编辑完成后,需要用source ~/.bash_profile使其生效。但是我发现重启Terminal后,刚刚配置的Python3.7并没有生效,Python还是之前的Python2.7。搜了一下发现,原来我的macOS已经用zsh,需要在 ~/.zshrc文件的末尾添加如下的代码:

source ~/.bash_profile

Python 3.7中自带了pip, 但是名字是pip3,我们可以在.bash_profile中设置别名为pip,这样就可以像Python2.7中用pip一样使用了。

# Setting PATH for Python 3.7
 # The original version is saved in .bash_profile.pysave
 export PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}"
 alias python="/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7" # 新增
 alias pip="/Library/Frameworks/Python.framework/Versions/3.7/bin/pip3.7" # 新增
 13 export PATH