更新程序包並安裝變異依賴環境
sudo apt update && sudo apt upgrade
sudo apt install wget build-essential libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev
開始正式安裝
1.下載Python源碼包
可以前往Python官網獲取最新源碼
cd ~
wget https://www.python.org/ftp/python/3.10.0/Python-3.10.0.tgz
2.解壓Python源碼
將下載好的源碼包進行解壓,默認放在當前文件夾下的壓縮包同名文件夾內
tar xzf Python-3.10.0.tgz
3.編譯Python源碼
進入解壓后的文件夾內,進行選項配置
cd Python-3.10.0
./configure --enable-optimizations
#--enable-optimizations為優化性能選項,其余類似的還有 --prefix=PATH 指定安裝目錄……,可根據需要進行選擇。
#默認安裝路徑為 /usr/local/bin
4.安裝Python 3.10
make altinstall
#altinstall用於防止編譯器覆蓋默認Python版本
5.驗證安裝
root@raspberrypi:~ # python3.10
Python 3.10.0 (default, Dec 5 2021, 22:46:09) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
至此,已完成Python3.10的安裝
接下來可以根據需要選擇是否需要更改默認Python為Python3.10
切換Python版本
可以使用以下兩個命令 whereis或which 確定已安裝python的版本和路徑:
#whereis:適用於查看目前已安裝的所有Python版本及路徑
root@raspberrypi:~ # whereis python
python: /usr/bin/python2.7-config /usr/bin/python /usr/bin/python3.9 /usr/bin/python2.7 /usr/bin/python3.9-config /usr/lib/python3.9 /usr/lib/python2.7 /etc/python3.9 /etc/python2.7 /usr/local/bin/python3.10-config /usr/local/bin/python3.10 /usr/local/lib/python3.9 /usr/local/lib/python2.7 /usr/local/lib/python3.10 /usr/include/python3.9m /usr/include/python3.9 /usr/include/python2.7 /usr/share/man/man1/python.1.gz
#which:適用於查看具體某個python版本的安裝路徑
root@raspberrypi:~ # which python3.10
/usr/local/bin/python3.10
為單個用戶切換Python版本
只需要在該用戶home目錄下的 .bashrc 文件下新增 Alias 即可
alias python='/usr/local/bin/python3.10'
#python具體版本和路徑可根據個人需要確定
修改完畢后,使用source ~/.bashrc
命令,重新加載 .bashrc 文件,使其生效
系統級切換Python版本
使用update-alternatives --list python
命令,為整個系統更改Python版本
1.列出所有可用Python替代版本
root@raspberrypi:~ # update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.9
/usr/local/bin/python3.10
2.添加替代版本列表
如果運行后出現錯誤信息:update-alternatives: error:no alternatives for python
則為沒有更新替代版本列表,使用以下命令添加:
#注意:update-alternatives --install <link> <name> <path> <priority>
#1.<link>一般情況下,直接使用 /usr/bin/python 即可
#2.<name>即為需要更換的python
#3.<path>為需要添加的python版本的安裝路徑,可以在上文中確定
#4.<priorit>為優先級。數字越大,優先級越高
root@raspberrypi:~ # update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives: 使用 /usr/bin/python2.7 來在自動模式中提供 /usr/bin/python (python)
root@raspberrypi:~ # update-alternatives --install /usr/bin/python python /usr/bin/python3.9 2
update-alternatives: 使用 /usr/bin/python3.9 來在自動模式中提供 /usr/bin/python (python)
root@raspberrypi:~ # update-alternatives --install /usr/bin/python python /usr/local/bin/python3.10 3
update-alternatives: 使用 /usr/local/bin/python3.10 來在自動模式中提供 /usr/bin/python (python)
至此,系統已默認Python版本為3.10,驗證如下:
root@raspberrypi:~ # python
Python 3.10.0 (default, Dec 5 2021, 22:46:09) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
3.進行版本切換
使用update-alternatives --config python
命令即可
root@raspberrypi:~ # update-alternatives --config python
有 3 個候選項可用於替換 python (提供 /usr/bin/python)。
選擇 路徑 優先級 狀態
------------------------------------------------------------
* 0 /usr/local/bin/python3.10 3 自動模式
1 /usr/bin/python2.7 1 手動模式
2 /usr/bin/python3.9 2 手動模式
3 /usr/local/bin/python3.10 3 手動模式
要維持當前值[*]請按<回車鍵>,或者鍵入選擇的編號:2
update-alternatives: 使用 /usr/bin/python3.9 來在手動模式中提供 /usr/bin/python (python)
root@raspberrypi:~ # python
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
參考鏈接:
- 月燈依舊:怎樣在Debian 10上安裝Python 3.9
- weixin_39634876:python升級命令debian_如何將 Debian Linux 中的默認的 Python 版本切換為替代版本
- YanniZhang的博客:linux 查看python安裝路徑,版本號