CommandNotFound.db.creator 在 ubuntu 上导入 DbCreator

CommandNotFound.db.creator 在 ubuntu 上导入 DbCreator

在 Ubuntu 18 LTS 上安装 Python 3.7 和 Mysql 后,我在执行命令“sudo apt update”后收到以下错误

Get:7 http://us.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]      
Fetched 252 kB in 1s (334 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/CommandNotFound/db/creator.py", line 11, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
    import apport.fileutils
  File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
    from apport.packaging_impl import impl as packaging
  File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 24, in <module>
    import apt
  File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

Original exception was:
Traceback (most recent call last):
  File "/usr/lib/cnf-update-db", line 8, in <module>
    from CommandNotFound.db.creator import DbCreator
  File "/usr/lib/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

现在,在安装任何新软件时,我都会一次又一次地遇到同样的错误。

有没有什么解决办法?

答案1

您的 python-apt 包有问题。请尝试sudo apt-get remove --purge python3-apt然后sudo apt autoremove安装:apt-get install python3-apt

答案2

这可能是因为/usr/bin/python3符号链接未指向 ubuntu 期望的 Python3 版本。Ubuntu 18.04 目前处于3.6.9

要纠正这个问题,您可能需要重新创建/usr/bin/python3指向的符号链接/usr/bin/python3.6

python3-apt如果您找不到 python3.6 可执行文件或者它无法正确执行,您可能需要按照 gongarek 的评论清除并重新安装。

我今天遇到了这个问题,因为昨晚我指出/usr/bin/python3我的Python 3.7安装是由于 cron 问题。我使用Python 3.7所有新代码来利用 Futures 功能(from __future__ import print_function),但该功能在 中不受支持Python 3.6。这是 cron 作业的问题,因为cron用于/usr/bin/python3执行脚本但执行失败。我现在的解决方法是明确指定python 3.7可执行文件的正确路径。

对于此 cron 问题,您会在 cron 执行 stderr/stdout 日志中看到:

SyntaxError: future feature annotations is not defined
Traceback (most recent call last):
  File "./mem_check.py", line 20, in <module>
    import qcorelite as Q_
  File "/home/tquinn/_data/tqmain/sys/memory_check/qcorelite.py", line 1
    from __future__ import annotations, print_function

相关内容