更改Ubuntu默認python版本的方法

 

當你安裝 Debian Linux 時,安裝過程有可能同時爲你提供多個可用的 Python 版本,所以系統中會存在多個 Python 的可執行二進制文件。通常Ubuntu默認的Python版本都爲2.x, 如何改變Python的默認版本呢?下面來一塊兒看看吧。
 

你能夠按照如下方法使用 ls 命令來查看你的系統中都有那些 Python 的二進制文件可供使用。html

1
2
ls  /usr/bin/python *
/usr/bin/python  /usr/bin/python2  /usr/bin/python2 .7  /usr/bin/python3  /usr/bin/python3 .4  /usr/bin/python3 .4m  /usr/bin/python3m

執行以下命令查看默認的 Python 版本信息:python

1
2
$ python --version
Python 2.7.8

一、基於用戶修改 Python 版本:mysql

想要爲某個特定用戶修改 Python 版本,只須要在其 home 目錄下建立一個 alias(別名) 便可。打開該用戶的 ~/.bashrc文件,添加新的別名信息來修改默認使用的 Python 版本。sql

1
alias  python= '/usr/bin/python3.4'

一旦完成以上操做,從新登陸或者從新加載 .bashrc 文件,使操做生效。bash

1
$ . ~/.bashrc

檢查當前的 Python 版本。python2.7

1
2
$ python --version
Python 3.4.2

二、 在系統級修改 Python 版本ide

咱們能夠使用 update-alternatives 來爲整個系統更改 Python 版本。以 root 身份登陸,首先羅列出全部可用的 python 替代版本信息:post

1
2
# update-alternatives --list python
update-alternatives: error: no alternatives  for  python

若是出現以上所示的錯誤信息,則表示 Python 的替代版本還沒有被 update-alternatives 命令識別。想解決這個問題,咱們須要更新一下替代列表,將 python2.7 和 python3.4 放入其中。學習

1
2
3
4
# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives: using  /usr/bin/python2 .7 to provide  /usr/bin/python  (python)  in  auto mode
# update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2
update-alternatives: using  /usr/bin/python3 .4 to provide  /usr/bin/python  (python)  in  auto mode

--install 選項使用了多個參數用於建立符號連接。最後一個參數指定了此選項的優先級,若是咱們沒有手動來設置替代選項,那麼具備最高優先級的選項就會被選中。這個例子中,咱們爲 /usr/bin/python3.4 設置的優先級爲2,因此update-alternatives 命令會自動將它設置爲默認 Python 版本。spa

1
2
# python --version
Python 3.4.2

接下來,咱們再次列出可用的 Python 替代版本。

1
2
3
# update-alternatives --list python
/usr/bin/python2 .7
/usr/bin/python3 .4

如今開始,咱們就能夠使用下方的命令隨時在列出的 Python 替代版本中任意切換了。

1
# update-alternatives --config python

1
2
# python --version
Python 2.7.8

三、移除替代版本

一旦咱們的系統中再也不存在某個 Python 的替代版本時,咱們能夠將其從 update-alternatives 列表中刪除掉。例如,咱們能夠將列表中的 python2.7 版本移除掉。

1
2
3
4
# update-alternatives --remove python /usr/bin/python2.7
 
update-alternatives: removing manually selected alternative - switching python to auto mode
update-alternatives: using  /usr/bin/python3 .4 to provide  /usr/bin/python  (python)  in  auto mode

方法二、移除軟鏈接

1
2
3
rm  -rf  /data/logs
 
ln  -s  /temp/logs  /data/logs

解決軟鏈接ln報錯-bash: /usr/local/bin/mysql: Too many levels of symbolic links

總結

以上就是這篇文章的所有內容了,但願本文的內容對你們的學習或者工做能帶來必定的幫助,若是有疑問你們能夠留言交流。

轉自:https://blog.csdn.net/fang_chuan/article/details/60958329

相關文章
相關標籤/搜索