序言
前段时间, 我使用一个空闲的 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
最新评论
这个软件有bug的,客户端windows有些键不能用如逗号、句号
没有收到邮件通知
我的评论通知貌似坏掉了,定位一下问题
测试一下重新部署后的邮件功能
居然看到自己公司的MIB库,诚惶诚恐
那可能是RobotFramework-ride的版本问题。我装的1.7.4.2,有这个限制。我有空再尝试下旧版本吧,感谢回复。
你好!我在python2.7中安装RobotFramework-ride的时候提示wxPython的版本最高是2.18.12,用pip下载的wxPython版本是4.10,而且我在那个路径下没有找到2
真的太好了,太感谢了,在bilibili和CSDN上都找遍了,终于在你这里找到了