'How to solve No module named 'apt_pkg' error on Ubuntu 20.04.3 LTS x86_64

When I try sudo apt update command on Ubuntu 20.04.3 LTS x86_64 this traceback comes. How can I fix it please

Traceback (most recent call last):                                        
  File "/usr/lib/cnf-update-db", line 8, in <module>                      
    from CommandNotFound.db.creator import DbCreator                      
  File "/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py", lin
e 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 /u
sr/lib/cnf-update-db > /dev/null; fi'                                     
E: Sub-process returned an error code


Solution 1:[1]

This happened to me after trying to install other python3 versions and finding that none of them ended up in my path. I'd made symlink changes to /usr/bin/python3 and I guess the later versions did not have all the required python stuff that apt uses to update etc. I couldn't open a terminal session because of it, so had to use ctrl + alt + f3 to get a safe session.

My fix was to link python3 back to the 3.8 installation:

sudo ln -s $(which python3.8) /usr/bin/python3

But before doing so, I'd check what python versions you have in /usr/bin/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hawksight