系統(tǒng)多版本 python 切換
前言:mac系統(tǒng)自帶python(一般是python2.7),雖然老版本對項目的運行影響不是很大,但由于python最新的3.X版本與2.X版本在一些語法上有些不同,導致我們跟著網(wǎng)上教程做項目的時候經(jīng)常會報錯,為了減少一系列頭大事件,最好還是將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”文件不存在,那我們就手動創(chuàng)建一個 終端輸入:
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 文件,需要手動創(chuàng)建。
保存文件,執(zhí)行
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.
>>>