1

Prior to this error: I did these steps

sudo apt-get install python3.7
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1 
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
sudo update-alternatives --config python3
alias python=python3

Here is the error message when I try to update sudo apt-get update

browser@virtual-machine:/usr/lib/python3/dist-packages$ sudo apt-get update
Hit:1 http://no.archive.ubuntu.com/ubuntu bionic InRelease
Hit:2 http://no.archive.ubuntu.com/ubuntu bionic-updates InRelease                                                                          
Hit:3 https://download.docker.com/linux/ubuntu bionic InRelease                                                                             
Hit:4 http://no.archive.ubuntu.com/ubuntu bionic-backports InRelease                                                                        
Ign:5 http://dl.google.com/linux/chrome/deb stable InRelease                                                                                
Hit:6 http://dl.google.com/linux/chrome/deb stable Release                                                                                  
Get:7 http://security.ubuntu.com/ubuntu bionic-security InRelease [88,7 kB]                                                                 
Hit:8 http://apt.postgresql.org/pub/repos/apt bionic-pgdg InRelease                   
Fetched 88,7 kB in 1s (133 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/python3/dist-packages/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/python3/dist-packages/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

2 Answers 2

1

The solution was to switch the python alternative to 3.6 instead of 3.7. As python 3.7 apparently broke "sudo apt-get update"

1
  • How is this a solution? Or more appropriately how long is not solution, the solution? I still get the problem in 3.8 as well. Using python for OS packages seems like a very bad idea. Commented Mar 19, 2021 at 21:58
-1

The following worked for me:

cd  /usr/lib/python3/dist-packages
ls -la /usr/lib/python3/dist-packages
sudo cp apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so

or:

sudo cp apt_pkg.cpython-36m-arm-linux-gnueabihf.so apt_pkg.so

depending on your plattform. cheers!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .