首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >运行apt更新时获取错误: ModuleNotFoundError:没有名为“apt_pkg”的模块

运行apt更新时获取错误: ModuleNotFoundError:没有名为“apt_pkg”的模块
EN

Ask Ubuntu用户
提问于 2023-01-31 09:49:58
回答 1查看 620关注 0票数 2

当我在我公司用来托管Netbox的服务器apt update 18.04.6LTS上运行<#>Ubuntu时,我会遇到以下问题:

代码语言:javascript
运行
复制
$ sudo apt update
Hit:1 http://azure.archive.ubuntu.com/ubuntu bionic InRelease
Hit:2 http://azure.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:3 http://azure.archive.ubuntu.com/ubuntu bionic-backports InRelease
Hit:4 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic InRelease
Hit:5 https://packages.microsoft.com/ubuntu/18.04/prod bionic InRelease
Hit:6 http://security.ubuntu.com/ubuntu bionic-security InRelease
Traceback (most recent call last):
  File "/usr/lib/cnf-update-db", line 8, in 
    from CommandNotFound.db.creator import DbCreator
  File "/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py", line 11, in 
    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'

我在运行neofetch时遇到了类似的问题:

代码语言:javascript
运行
复制
$ neofetch
Traceback (most recent call last):
  File "/usr/lib/command-not-found", line 28, in 
    from CommandNotFound import CommandNotFound
  File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 19, in 
    from CommandNotFound.db.db import SqliteDatabase
  File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, in 
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

我的一位同事让我运行这些程序,然后再试一次,但我也遇到了同样的问题:

代码语言:javascript
运行
复制
sudo apt-get remove command-not-found
sudo apt-get clean
sudo apt-get update
sudo apt-get install command-not-found

然后,我检查了这个服务器上安装了多少个python版本,我计算了4:

代码语言:javascript
运行
复制
$ ls -ls /usr/bin/python*
   0 lrwxrwxrwx 1 root root       9 Apr 16  2018 /usr/bin/python -> python2.7
   0 lrwxrwxrwx 1 root root       9 Apr 16  2018 /usr/bin/python2 -> python2.7
3544 -rwxr-xr-x 1 root root 3628904 Nov 28 18:51 /usr/bin/python2.7
   0 lrwxrwxrwx 1 root root      25 Jan 10 08:10 /usr/bin/python3 -> /etc/alternatives/python3
   0 lrwxrwxrwx 1 root root      16 Oct 25  2018 /usr/bin/python3-config -> python3.6-config
   4 -rwxr-xr-x 1 root root    1018 Oct 28  2017 /usr/bin/python3-jsondiff
   4 -rwxr-xr-x 1 root root    3661 Oct 28  2017 /usr/bin/python3-jsonpatch
   4 -rwxr-xr-x 1 root root    1342 May  1  2016 /usr/bin/python3-jsonpointer
   4 -rwxr-xr-x 1 root root     398 Nov 15  2017 /usr/bin/python3-jsonschema
5356 -rwxr-xr-x 1 root root 5481816 Dec  7 01:11 /usr/bin/python3.10
4424 -rwxr-xr-x 2 root root 4526456 Nov 25 14:10 /usr/bin/python3.6
   0 lrwxrwxrwx 1 root root      33 Nov 25 14:10 /usr/bin/python3.6-config -> x86_64-linux-gnu-python3.6-config
4424 -rwxr-xr-x 2 root root 4526456 Nov 25 14:10 /usr/bin/python3.6m
   0 lrwxrwxrwx 1 root root      34 Nov 25 14:10 /usr/bin/python3.6m-config -> x86_64-linux-gnu-python3.6m-config
   0 lrwxrwxrwx 1 root root      10 Oct 25  2018 /usr/bin/python3m -> python3.6m
   0 lrwxrwxrwx 1 root root      17 Oct 25  2018 /usr/bin/python3m-config -> python3.6m-config

运行python3 --version返回Python 3.10.9

有没有人知道我如何解决这个问题而不必迁移托管的应用程序?那将是一场噩梦:

编辑:

我转到/usr/lib/python3/dist-packages,通过ls -la | grep "apt_pkg.cpython"查找apt_pkg,找到了文件apt_pkg.cpython-36m-x86_64-linux-gnu.so,我将文件重命名为apt_pkg.so,现在我可以使用新的错误消息运行apt update

代码语言:javascript
运行
复制
$ sudo apt update && sudo apt upgrade
[sudo] password for admin-federico-dostuni:
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]
Hit:4 http://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:5 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic InRelease
Hit:6 https://packages.microsoft.com/ubuntu/18.04/prod bionic InRelease
Fetched 172 kB in 1s (176 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
1 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] y

Setting up update-notifier-common (3.192.1.15) ...
Traceback (most recent call last):
  File "/usr/lib/update-notifier/package-data-downloader", line 24, in 
    import debian.deb822
  File "/usr/lib/python3/dist-packages/debian/deb822.py", line 78, in 
    class TagSectionWrapper(collections.Mapping):
AttributeError: module 'collections' has no attribute 'Mapping'
dpkg: error processing package update-notifier-common (--configure):
 installed update-notifier-common package post-installation script subprocess returned error exit status 1
Errors were encountered while processing:
 update-notifier-common
E: Sub-process /usr/bin/dpkg returned an error code (1)
EN

回答 1

Ask Ubuntu用户

回答已采纳

发布于 2023-02-14 03:52:58

由于文件/usr/lib/update-notifier/package-data-downloader,其第一行包含以下内容:

#!/usr/bin/python3

从前面的信息来看,你的蟒蛇是Python 3.10.9

apt_pkg是apt_pkg.cpython-36m-x86_64-linux-gnu.so,这意味着它只匹配python3.6,您可以这样做:

sudo rm /usr/bin/python3

sudo ln -f /usr/bin/python3.6 /usr/bin/python3

确保python3 --versionpython3.6.x

运行update命令进行测试,

sudo apt update

票数 3
EN
页面原文内容由Ask Ubuntu提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://askubuntu.com/questions/1452764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档