升级Windows11 wsl Ubuntu子系统的 Python 到最新版本(3.10) | GSGUNDAM砍柴工
安装 WSL
这个过程挺简单,因为已经在商店安装过WSL了,因此只需要打开命令行,先检查一下是不是 v2 版本,不是就切换一下,然后升级。
C:\WINDOWS\system32>wsl -l -v NAME STATE VERSION * Ubuntu Running 1 C:\WINDOWS\system32>wsl --set-version Ubuntu 2 C:\WINDOWS\system32>wsl --update |
搞定。然后就是折腾 Python 版本的问题。
升级 Python
首先,Python3.10 并不在 Ubuntu 20.04 的默认官方源中,需要添加源单独安装。
添加源
gsgundam@NUCHome:~$ sudo add-apt-repository ppa:deadsnakes/ppa gsgundam@NUCHome:~$ sudo apt update gsgundam@NUCHome:~$ apt list | grep python3.10 WARNING: apt does not have a stable CLI interface. Use with caution in scripts. idle-python3.10/focal 3.10.9-1+focal1 all libpython3.10-dbg/focal 3.10.9-1+focal1 amd64 libpython3.10-dev/focal 3.10.9-1+focal1 amd64 libpython3.10-minimal/focal,now 3.10.9-1+focal1 amd64 [installed,automatic] libpython3.10-stdlib/focal,now 3.10.9-1+focal1 amd64 [installed,automatic] libpython3.10-testsuite/focal 3.10.9-1+focal1 all libpython3.10/focal 3.10.9-1+focal1 amd64 libqgispython3.10.4/focal 3.10.4+dfsg-1ubuntu2 amd64 python3.10-dbg/focal 3.10.9-1+focal1 amd64 python3.10-dev/focal 3.10.9-1+focal1 amd64 python3.10-distutils/focal,now 3.10.9-1+focal1 all [installed] python3.10-examples/focal 3.10.9-1+focal1 all python3.10-full/focal 3.10.9-1+focal1 amd64 python3.10-gdbm-dbg/focal 3.10.9-1+focal1 amd64 python3.10-gdbm/focal 3.10.9-1+focal1 amd64 python3.10-lib2to3/focal,now 3.10.9-1+focal1 all [installed,automatic] python3.10-minimal/focal,now 3.10.9-1+focal1 amd64 [installed,automatic] python3.10-tk-dbg/focal 3.10.9-1+focal1 amd64 python3.10-tk/focal 3.10.9-1+focal1 amd64 python3.10-venv/focal 3.10.9-1+focal1 amd64 python3.10/focal,now 3.10.9-1+focal1 amd64 [installed] |
安装 Python 3.10
sudo apt install python3.10 |
很简单的就安装完了 Python 3.10 ,现在可以使用命令 python3 --version 打印版本,这个时候发现仍然是老版本。
设置默认版本为 3.10
gsgundam@NUCHome:~$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1 update-alternatives: using /usr/bin/python3.8 to provide /usr/bin/python3 (python3) in auto mode gsgundam@NUCHome:~$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 2 update-alternatives: using /usr/bin/python3.10 to provide /usr/bin/python3 (python3) in auto mode gsgundam@NUCHome:~$ 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.10 2 auto mode 1 /usr/bin/python3.10 2 manual mode 2 /usr/bin/python3.8 1 manual mode Press to keep the current choice[*], or type selection number: |
这里将 3.10 作为可选版本加入了,并设置为了自动选择的版本。
修复 pip 错误
这个时候如果用 pip 命令来安装依赖,还是会报错。执行以下命令来修复:
gsgundam@NUCHome:~$ sudo apt remove --purge python3-apt gsgundam@NUCHome:~$ sudo apt autoclean gsgundam@NUCHome:~$ sudo apt install python3-apt gsgundam@NUCHome:~$ sudo apt install python3.10-distutils gsgundam@NUCHome:~$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py gsgundam@NUCHome:~$ sudo python3.10 get-pip.py |
再跑 pip install -r requirements.txt,然后 python3 main.py,一切正常。
rm -rf get-pip.py
收工。