欢迎光临!
若无相欠,怎会相见

修复 Ubuntu 18.04 默认 Python

序言

前段时间, 我使用一个空闲的 VPS 搭建 Taiga, 但是在折腾的过程中, 对 Ubuntu 18.04 自带的 Python3 动手了, 结果出现了一系列的问题, 现在记录一下修复过程。

补充一下, 默认的 Python 是 3.6 的, apt 包管理工具依赖于这个默认的 Python, 所以最好不要动它。

正文

我是在部署 Taiga 项目的时候, 发现自带的 Python3 版本是 3.6 的, 库不支持, 无奈只能安装 Python 3.8, 然后手贱, 使用了如下命令, 把python3.8设置为默认的了。

root@liangz:/home/taiga# update-alternatives --install /usr/bin/python3 python3.8 /usr/bin/python3.8 1
update-alternatives: using /usr/bin/python3.8 to provide /usr/bin/python3 (python3.8) in auto mode

刚开始很高兴, 因为可以使用 Python 3.8 新版本了, 但是我还没开始部署 Python 项目, 只是执行 sudo apt update 就报错了:

root@liangz:/home/taiga# sudo apt update
Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Hit:2 http://us.archive.ubuntu.com/ubuntu bionic InRelease
Hit:3 https://deb.nodesource.com/node_12.x xenial InRelease
Get:4 http://us.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Hit:5 https://esm.ubuntu.com/infra/ubuntu bionic-infra-security InRelease
Hit:6 https://esm.ubuntu.com/infra/ubuntu bionic-infra-updates InRelease
Get:7 http://us.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Fetched 252 kB in 1s (172 kB/s)
Traceback (most recent call last):
  File "/usr/lib/cnf-update-db", line 8, in <module>
    from CommandNotFound.db.creator import DbCreator
  File "/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py", line 11, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Reading package lists... Done
E: Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/lib/command-not-found/ -a -e /usr/lib/cnf-update-db; then /usr/lib/cnf-update-db > /dev/null; fi'
E: Sub-process returned an error code
root@liangz:/home/taiga#

这可真是难受, 把 apt 给干跪了, 这影响太大了, 所以不要动系统基础包。 只能尝试修复了, 不然得重新安装 VPS 系统, 更麻烦,VPS 内的数据都会消失。

Python3.8 直接卸载后在执行 python3 会出现如下报错, 而且使用 update-alternatives 命令会找不到 python3

root@liangz:/home# python3
-bash: /usr/lib/command-not-found: /usr/bin/python3: bad interpreter: No such file or directory
root@liangz:/usr/lib/python3.6# update-alternatives --list python3
update-alternatives: error: no alternatives for python3

接下来修复 Python3
首先使用 ls 命令查看 /usr/bin/ 目录的 Python 详情:

root@liangz:/usr/lib/python3.6# ls -la /usr/bin/python*
lrwxrwxrwx 1 root root       9 Apr 16  2018 /usr/bin/python -> python2.7
lrwxrwxrwx 1 root root       9 Apr 16  2018 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 3620744 Mar 18 13:21 /usr/bin/python2.7
lrwxrwxrwx 1 root root      27 May 28 09:58 /usr/bin/python3 -> /etc/alternatives/python3.8
-rwxr-xr-x 2 root root 4526456 Mar 15 13:55 /usr/bin/python3.6
lrwxrwxrwx 1 root root      33 Mar 15 13:55 /usr/bin/python3.6-config -> x86_64-linux-gnu-python3.6-config
-rwxr-xr-x 2 root root 4526456 Mar 15 13:55 /usr/bin/python3.6m
lrwxrwxrwx 1 root root      34 Mar 15 13:55 /usr/bin/python3.6m-config -> x86_64-linux-gnu-python3.6m-config
lrwxrwxrwx 1 root root      16 Oct 25  2018 /usr/bin/python3-config -> python3.6-config
lrwxrwxrwx 1 root root      10 Oct 25  2018 /usr/bin/python3m -> python3.6m
lrwxrwxrwx 1 root root      17 Oct 25  2018 /usr/bin/python3m-config -> python3.6m-config
root@liangz:/usr/lib/python3.6#

然后发现 python3 是 /etc/alternatives/python3.8 的软链接, 接着使用如下命令删除软连接

root@liangz:/etc/alternatives# sudo update-alternatives --remove-all python3

/etc/alternatives/python3.8 删掉, 或者直接在 /etc/alternatives 目录下删掉 python3.8

删完后再将 /usr/bin/ 目录的 python3 删掉, 因为如果还存在会导致新的软链接错误

root@liangz:/usr/bin# sudo ln -s /usr/bin/python3.6 /usr/bin/python3
ln: failed to create symbolic link '/usr/bin/python3': File exists
root@liangz:/usr/bin# rm -f python3
root@liangz:/usr/bin# sudo ln -s /usr/bin/python3.6 /usr/bin/python3
root@liangz:/usr/bin#
root@liangz:/usr/bin#
root@liangz:/usr/bin# python3
Python 3.6.9 (default, Mar 15 2022, 13:55:28)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
root@liangz:/usr/bin#

apt 也正常了:

root@liangz:/usr/bin# sudo apt update
Hit:1 https://deb.nodesource.com/node_12.x xenial InRelease
Hit:2 http://us.archive.ubuntu.com/ubuntu bionic InRelease
Get:3 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:5 http://us.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:6 https://esm.ubuntu.com/infra/ubuntu bionic-infra-security InRelease [7,458 B]
Get:7 https://esm.ubuntu.com/infra/ubuntu bionic-infra-updates InRelease [7,457 B]
Fetched 267 kB in 2s (155 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.
root@liangz:/usr/bin#

还好修复了, 虚惊一场

结语

部署新项目的时候最后使用项目本身推荐的操作系统, 如果系统太老, 有些库就可能不支持。

后来我还是把操作系统给升级了, 才完美解决这个问题。

如有错误,敬请指出,感谢指正!    — 2022-06-05  16:13:42

赞(0) 打赏
转载请注明:飘零博客 » 修复 Ubuntu 18.04 默认 Python
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

欢迎光临