Mac 升级Python 2.7 到 Python 3.7

MAC上默认内置安装了Python 2.7,但是Python 2.7到2020年就会停止维护了,并且有时候会出现依赖库的不兼容问题,那么怎么安装Python 3.X,并且将Python 3.X的版本设置为默认版本呢?

 

安装Python 3.7

1、下载地址如下:https://www.python.org/downloads/

2、点击下载好的pkg文件进行安装

3、安装完成之后,python 3.70的默认安装路径如下:/Library/Frameworks/Python.framework/Versions/3.7

 

修改profile文件

在terminal里运行open  ~/.bash_profile,打开~/.bash_profile,直接修改文件即可

1、修改方式

添加:

alias python="/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7"

或者:

source ~/.bash_profile
# Setting PATH for Python 3.7

# The original version is saved in .bash_profile.pysave

export PATH=$PATH:/Library/Frameworks/Python.framework/Versions/3.7/bin:

alias python="/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7"

2、重启一下Terminal,使修改后的文件生效。

 

Python 2.7与Python 3.7版本的切换

  1. 使用Python 2.7时,直接将~/.bash_profile文件中3.7的版本注释掉,即

#aliaspython="/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7"

  1. 使用Python 3.7时,直接将~/.bash_profile文件中3.7的版本打开,即

aliaspython="/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7"


验证

在终端中直接 输入 python -v,显示版本号为3.7

你可能感兴趣的:(Python)