分类: Python

  • Ubuntu系统切换Python版本

    本文使用的Ubuntu系统是WSL的Ubuntu-26.04版本,当前系统默认安装的Python版本是3.14。

    本文主要通过Deadsnakes的PPA源来安装其他Python版本,并通过`update-alternatives` 命令来切换Python版本。

    安装仓库并添加PPA

    # 安装仓库
    sudo apt install software-properties-common
    # 添加ppa
    sudo add-apt-repository ppa:deadsnakes/ppa
    # 更新apt
    sudo apt update

    安装Python

    查看系统当前python版本:

    $ python3 --version
    Python 3.14.4

    安装Python3.13版本:

    $ apt install python3.13
    

    查看系统所有python版本:

    $ ll /usr/bin/python*
    lrwxrwxrwx 1 root root      10 Mar 21 17:46 /usr/bin/python3@ -> python3.14
    -rwxr-xr-x 1 root root 6671680 Aug  6 19:06 /usr/bin/python3.13*
    -rwxr-xr-x 1 root root 7477096 Apr  8 12:02 /usr/bin/python3.14*

    切换Python版本

    向 “`update-alternatives“`添加需要管理的版本:

    # 添加Python3.13版本
    $ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.13 1
    update-alternatives: using /usr/bin/python3.13 to provide /usr/bin/python3 (python3) in auto mode
    
    # 添加Python3.14版本
    $ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.14 2
    update-alternatives: using /usr/bin/python3.14 to provide /usr/bin/python3 (python3) in auto mode
    
    $ sudo update-alternatives --config python3
    There are 2 choices for the alternative python3 (providing /usr/bin/python3).
    
      Selection    Path                 Priority   Status
    ------------------------------------------------------------
    * 0            /usr/bin/python3.14   2         auto mode
      1            /usr/bin/python3.13   1         manual mode
      2            /usr/bin/python3.14   2         manual mode
    
    Press <enter> to keep the current choice[*], or type selection number: 1
    update-alternatives: using /usr/bin/python3.13 to provide /usr/bin/python3 (python3) in manual mode
    
    # 查看系统当前Python版本号
    $ python3 --version
    Python 3.13.15