Q: apt update fails due to script hooks on Ubuntu Mate I am running Ubuntu Mate on my Raspberry Pi 3 (aarch64). I think the problem is specific to Debian in general, and not particular to RPi3.
I installed python3 and pip using apt install python3-pip and ran a script successfully.
However, after a day, Python stopped working, complaning:
No module named "threading"
...

I have been getting the same error when doing apt update, apt install etc.
Currently the state of Python on my machine breaks apt and I have no idea how I have to fix this. I tried to remove the files that were breaking the functionality of apt, but I think things got worse.
Now, a simple apt update shows:
$ sudo apt -oDebug::RunScripts=true update
Hit:1 http://ports.ubuntu.com/ubuntu-ports focal InRelease
Get:2 http://ports.ubuntu.com/ubuntu-ports focal-security InRelease [107 kB]
Hit:3 http://ppa.launchpad.net/ubuntu-pi-flavour-makers/ppa/ubuntu focal InRelease
Get:4 http://ports.ubuntu.com/ubuntu-ports focal-updates InRelease [111 kB]
Get:5 http://ports.ubuntu.com/ubuntu-ports focal-updates/main arm64 Packages [303 kB]
Get:6 http://ports.ubuntu.com/ubuntu-ports focal-updates/universe arm64 Packages [159 kB]
Fetched 680 kB in 5s (135 kB/s)                          
Running external script: '[ ! -f /var/run/dbus/system_bus_socket ] || /usr/bin/dbus-send --system --dest=org.debian.apt --type=signal /org/debian/apt org.debian.apt.CacheChanged || true'
Running external script: '/usr/bin/test -e /usr/share/dbus-1/system-services/org.freedesktop.PackageKit.service && /usr/bin/test -S /var/run/dbus/system_bus_socket && /usr/bin/gdbus call --system --dest org.freedesktop.PackageKit --object-path /org/freedesktop/PackageKit --timeout 4 --method org.freedesktop.PackageKit.StateHasChanged cache-update > /dev/null; /bin/echo > /dev/null'
Running external script: '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'
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Python path configuration:
  PYTHONHOME = (not set)
  PYTHONPATH = (not set)
  program name = '/usr/bin/python3'
  isolated = 0
  environment = 1
  user site = 1
  import site = 1
  sys._base_executable = '/usr/bin/python3'
  sys.base_prefix = '/usr'
  sys.base_exec_prefix = '/usr'
  sys.executable = '/usr/bin/python3'
  sys.prefix = '/usr'
  sys.exec_prefix = '/usr'
  sys.path = [
    '/usr/lib/python38.zip',
    '/usr/lib/python3.8',
    '/usr/lib/lib-dynload',
  ]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'

Current thread 0x0000ffff9547b010 (most recent call first):
<no Python frame>
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

How can I fix it without reinstalling the whole system?

A: 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'

points to the culprit: /usr/lib/cnf-update-db. Disabling that should allow your apt update to go further:
sudo mv /etc/apt/apt.conf.d/50command-not-found{,.disabled}

Remember to re-enable it once everything's fixed:
sudo mv /etc/apt/apt.conf.d/50command-not-found{.disabled,}

