ModuleNotFoundError: No module named 'CommandNotFound'

To clarify the situation:

  • the heart of Debian packaging is dpkg, which validates and installs deb packages; validates = check version, architecture, dependencies, conflicts; installs = put files in the right places and run maintainer scripts (to update configs, services, databases, etc)
  • APT is a layer on top that deals with repositories and downloads
  • to use the APT system, one invokes a front-end program such as apt-get, aptitude, a graphical package manager, or apt (recent command-line program that replaces apt-get, apt-cache, apt-show, etc)

Now if we look at the error message closely:

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'

This shows that APT, like dpkg, has hooks to run various programs before and after installing, updating or removing packages, for example apt-listbugs (written in ruby) to show known bugs in the new versions and give the operator a chance to change their mind. Here the program is command-not-found, which is a utility (written in python) invoked by the shell to show hints like this:

$ mplayer

Command 'mplayer' not found, but can be installed with:

apt install mplayer
Please ask your administrator.

The database used by command-not-found is updated when the APT package database is updated.
This is the command that fails on your system, probably because something messed up your system Python and causes the import to fail. You should follow the advice given by Steven and fix your OS, reinstalling (using apt or another frontend) the python3 and command-not-found packages. If the reinstall command fails because command-not-found is broken, edit /etc/apt/apt.conf.d/50-command-not-found to comment the script temporarily.

Hope this helps!

2 Likes