默认情况的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.
完成