macOS自带Python2.7版本升级

为什么要升级

Mac自带的2.7版本将于2020年停止支持
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won’t be maintained after that date. A future version of pip will drop support for Python 2.7.

下载和安装

1、官网下载Python安装包

  • 下载地址:https://www.python.org/downloads/
  • 选择自己系统适合的pkg包下载,完成后点击安装
  • 安装完成后,默认安装路径为:/Library/Frameworks/Python.framework/Versions/3.9

2、使用brew安装

  • 安装brew

确保已经安装Xcode(或者Xcode命令行工具CommandLineTools)

  • 安装Python3.x

brew search python
brew install python3.x
第一行命令查看并选择python版本,看好要安装的版本,再使用第二行命令安装即可。

查看Python版本

python -V

神奇的事情发生了,你发现你的py版本还是2.7,这是因为python默认多版本可共存机制,而macOS默认版本指向的是原装的2.7版本。

将3.x版本设置为默认版本

查看不同安装方式的3.x安装地址

来源python安装路径
系统默认/System/Library/Frameworks/Python.framework/Versions/2.7
brew安装/usr/local/Cellar
官网pkg安装/Library/Frameworks/Python.framework/Versions/2.7

修改profile文件

  • 在terminal里运行open ~/.bash_profile打开~/.bash_profile
  • 修改文件
# Setting PATH for Python 3.9
# The original version is saved in .bash_profile.pysave
PATH="/usr/local/Cellar/python@3.9/3.9.4/bin:${PATH}"
export PATH
alias python="/usr/local/Cellar/python@3.9/3.9.4/bin/python3.9"

【一般情况下,在安装py3.x版本后系统会自动生成bash_profile文件,但是我在修改bash_profile文件时,发现并没有bash_profile文件存在,于是touch了一个新的文件,并进行了修改后续操作】

  • source .bash_profile更新文件

**【经过一番折腾】**重新查看Python版本,发现版本已更新

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值