文章目录
  1. 1. 安装 WSLg
  2. 2. 升级 Python
    1. 2.1. 添加源
    2. 2.2. 安装 Python 3.10
    3. 2.3. 设置默认版本为 3.10
    4. 2.4. 修复 pip 错误

跟大神们学习做点小东西,然后发现他们都是 LinuxmacOS ,写出来的python代码最初是没有考虑 Windows 的。自己跑起来还是有些问题,就打算在Windows下的子系统Ubuntu来运行。

PS:那句话真是不假,除了自己的破烂系统,微软做的 LinuxmacOS 甚至手机上的相关软件和平台真是不错 😁

安装 WSLg

这个过程挺简单,因为已经在商店安装过WSL了,因此只需要打开命令行,先检查一下是不是 v2 版本,不是就切换一下,然后升级。

1
2
3
4
5
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 的默认官方源中,需要添加源单独安装。

添加源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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

1
sudo apt install python3.10

很简单的就安装完了 Python 3.10 ,现在可以使用命令 python3 --version 打印版本,这个时候发现仍然是老版本。

设置默认版本为 3.10

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 <enter> to keep the current choice[*], or type selection number:

这里将 3.10 作为可选版本加入了,并设置为了自动选择的版本。

修复 pip 错误

这个时候如果用 pip 命令来安装依赖,还是会报错。执行以下命令来修复:

1
2
3
4
5
6
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,一切正常。收工。

♦ 本文固定连接:https://www.gsgundam.com/2023/01/2023-01-18-z08-upgrade-python-3-in-ubuntu-of-wsl-windows/

♦ 转载请注明:GSGundam 2023年01月18日发布于 GSGUNDAM砍柴工

♦ 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。

♦ 原创不易,如果页面上有适合你的广告,不妨点击一下看看,支持作者。(广告来源:Google Adsense)

♦ 本文总阅读量