1. 程式人生 > >ubuntu中修改預設Python版本號,親測有用

ubuntu中修改預設Python版本號,親測有用

ubuntu中預設的Python版本是Python2.X,但是現在Python的最新版本是Python3.X,我一直認為軟體用最新版本的永遠沒有錯。所以下面就講講該怎麼修改ubutun系統預設的Python直譯器。

首先檢視系統中是否安裝Python3.X:

:~$ whereis python3
  • 1

如果已經安裝,大致會輸出這些:

:~$ whereis python3
python3: /usr/bin/python3.5m /usr/bin/python3.5 /usr/bin/python3 /usr/lib/python3.5 /usr/lib/python3.6 /usr/lib/python3 /etc/python3.5
/etc/python3 /usr/local/lib/python3.5 /usr/include/python3.5m /usr/share/python3 /usr/share/man/man1/python3.1.gz
  • 1
  • 2

如果沒有安裝,則使用以下命令安裝:

sudo apt-get install python3
  • 1

1. 不需要修改預設設定,只是在命令列輸入對應版本號即可使用不同的直譯器執行程式。

呼叫Python2.X

:~$ python
  • 1

呼叫Python3.X

:~$ python3
  • 1

2. 修改預設Python直譯器

刪除原有的Python連線檔案

~$: sudo rm /usr/bin/python
  • 1

建立指向Python3.X的連線

~$: ln -s /usr/bin/python3.5 /usr/bin/python
  • 1

把路徑/usr/bin/加入環境變數PATH中(一般情況下不需要這一步!)

~$: PATH=/usr/bin:$PATH
  • 1

OK,現在輸入python,命令列就會輸出

Python 3.5.2+ (default, Sep 22 2016, 12:18:14) 
[GCC 6.2.0 20160927] on linux
Type "help", "copyright", "credits" or "license" for more information.
  • 1
  • 2
  • 3

3. 還原2.X版本

刪除原有的Python連線檔案

~$:
rm /usr/bin/python
  • 1

建立指向Python2.X的連線

~$: ln -s /usr/bin/python2.7 /usr/bin/python