mac系統(tǒng)python多版本選擇

系統(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.
>>> 
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容