mac上设置默认运行python3

默认情况的mac终端输入python会显示Python 2.7

xxx@caijiadeMacBook-Pro Versions % python

WARNING: Python 2.7 is not recommended. 
This version is included in macOS for compatibility with legacy software. 
Future versions of macOS will not include Python 2.7. 
Instead, it is recommended that you transition to using 'python3' from within Terminal.

Python 2.7.18 (default, Oct  2 2021, 04:20:38) 
[GCC Apple LLVM 13.0.0 (clang-1300.0.29.1) [+internal-os, ptrauth-isa=deploymen on darwin
Type "help", "copyright", "credits" or "license" for more information.

修改方法:

1.终端输入

which python3

(说明:which 可执行文件命令:在PATH变量制定的路径中,搜索某个系统命令的位置并返回第一个搜索结果 即查找PATH路径下配置的文件)

结果返回:

/usr/bin/python3

2.终端输入

vim ~/.bash_profile

(说明:vim filename :打开filename文件)
3.按 i 或 a 进入编辑模式(Insert Code)(i-在当前光标位置左边添加,a-右边),输入

alias python="/usr/bin/python3"

(说明:alias 别名 = “指令名称”)
4.esc退回正常模式,然后输入 :wq (保存编辑并退出)进入命令模式
5.在终端输入

source ~/.bash_profile

(说明:source filename/.filename用于重新执行刚修改的初始化文件

简单理解:.bash_profile 中修改环境变量只对当前窗口有效,Mac 要执行source ~/.bash_profile 配置的环境 

Mac 将使用 zsh 作为默认登录 Shell 和交互式 Shell变量才生效,相当于 windows 的开机启动的环境变量,可在 .zshrc 文件中加一行 source .bash_profile 解决需要 source 才能使用的问题

)
5.在终端编辑 zsh 的配置文件/.zshrc

vi ~/.zshrc

6.按照上面3,4步在zshrc文件中加入

source ~/.bash_profile

7.返回终端输入python,显示:

Python 3.8.9 (default, Jul 19 2021, 09:37:30) 
[Clang 13.0.0 (clang-1300.0.27.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

完成

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值