系统多版本 python 切换
前言:mac系统自带python(一般是python2.7),虽然老版本对项目的运行影响不是很大,但由于python最新的3.X版本与2.X版本在一些语法上有些不同,导致我们跟着网上教程做项目的时候经常会报错,为了减少一系列头大事件,最好还是将python升级到3.X版本
查看本地默认python 版本
python版本
xzq-Mac-mini:Frameworks a120307$ python -V
Python 2.7.16
python3版本
xzq-Mac-mini:Frameworks a120307$ python3 -V
Python 3.8.2
python默认版本
xzq-Mac-mini:Frameworks a120307$ 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.16 (default, Dec 21 2020, 23:00:36)
[GCC Apple LLVM 12.0.0 (clang-1200.0.30.4) [+internal-os, ptrauth-isa=sign+stri on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
当前我们可以看到默认启动版本是python2.7.16
查看python安装路径
xzq-Mac-mini:Frameworks a120307$ which python3
/usr/bin/python3
xzq-Mac-mini:Frameworks a120307$ which python
/usr/bin/python
配置本地默认启用python
然后在终端输入,打开bash文件
open ~/.bash_profile
可能会提示“profile not found”文件不存在,那我们就手动创建一个 终端输入:
cd ~/
touch .bash_profile
open -e .bash_profile
文件中写入
# Setting PATH for Python 3
PATH="/Library/Frameworks/Python.framework/Versions/3/bin:${PATH}"
export PATH
PATH=$PATH/usr/bin/python3
alias python="/usr/bin/python3"
alias python2="/usr/bin/python"
备注:如果本地无/Library/Frameworks/Python.framework/Versions/3/bin 文件,需要手动创建。
保存文件,执行
source ~/.bash_profile
再查看本地默认python版本
xzq-Mac-mini:Frameworks a120307$ python
Python 3.8.2 (default, Apr 8 2021, 23:19:18)
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>