update-notifier reports an AttributeError

E: Unknown error: “<class ‘AttributeError’>” (module ‘apt_pkg’ has no attribute ‘Error’).

Analysis

First I found that the service has some scripts under /usr/lib/update-notifier/, and apt-check -> apt_check.py among them is the culprit.

In my system, the erroring code looks like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import apt_pkg

...


def get_apt_pkg_esm_cache():
    ...
    try:
        esm_cache = apt_pkg.Cache(progress=None)
    except apt_pkg.Error:
        esm_cache = None

The apt_pkg.Error attribute does not exist, but according to python-apt Navigation, it should, and:

It inherits from SystemError, so make sure to catch this class first.

And apt_pkg is a dynamic library:

1
2
3
4
import apt_pkg

apt_pkg.__file__
# '/usr/lib/python2.7/dist-packages/apt_pkg.x86_64-linux-gnu.so'

Presumably it is a version problem, so let me reinstall it:

1
2
3
# 按照系统使用的python版本
sudo apt install --reinstall python-apt
sudo apt install --reinstall python3-apt

No effect, so I edited the file directly:

1
2
# except apt_pkg.Error:
except SystemError:

Done.

References