Azure VM 上的 Ubuntu 18.04.6 LTS 无法运行 apt 升级:ModuleNotFoundError:没有名为“apt_pkg”的模块

Azure VM 上的 Ubuntu 18.04.6 LTS 无法运行 apt 升级:ModuleNotFoundError:没有名为“apt_pkg”的模块

当我尝试更新使用 APT 安装的软件时收到此错误消息:

$ sudo apt update && sudo apt upgrade
Hit:1 http://azure.archive.ubuntu.com/ubuntu bionic InRelease
Get:2 http://azure.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:3 http://azure.archive.ubuntu.com/ubuntu bionic-backports InRelease [83.3 kB]
Get:4 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Hit:5 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic InRelease
Hit:6 https://packages.microsoft.com/ubuntu/18.04/prod bionic InRelease
Fetched 261 kB in 1s (319 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

当我运行时也会发生类似的事情neofetch

$ neofetch
Traceback (most recent call last):
  File "/usr/lib/command-not-found", line 28, in <module>
    from CommandNotFound import CommandNotFound
  File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 19, in <module>
    from CommandNotFound.db.db import SqliteDatabase
  File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

我做的最后一件重要的事情是安装 Python 3.10,一切应该顺利。我不知道我做错了什么。

编辑:正如有人建议的那样,我已经尝试运行该命令apt-get install --reinstall python3-apt,但它并没有解决问题:

$ sudo apt-get install --reinstall python3-apt
Reading package lists... Done
Building dependency tree
Reading state information... Done
0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 0 not upgraded.
Need to get 149 kB of archives.
After this operation, 0 B of additional disk space will be used.
Get:1 http://azure.archive.ubuntu.com/ubuntu bionic-updates/main amd64 python3-apt amd64 1.6.5ubuntu0.7 [149 kB]
Fetched 149 kB in 0s (3207 kB/s)
(Reading database ... 132504 files and directories currently installed.)
Preparing to unpack .../python3-apt_1.6.5ubuntu0.7_amd64.deb ...
Unpacking python3-apt (1.6.5ubuntu0.7) over (1.6.5ubuntu0.7) ...
Setting up python3-apt (1.6.5ubuntu0.7) ...
/usr/lib/python3.10/subprocess.py:955: RuntimeWarning: line buffering (buffering=1) isn't supported in binary mode, the default buffer size will be used
  self.stdin = io.open(p2cwrite, 'wb', bufsize)

答案1

相关内容