6

after installing Python 3.7 and Mysql on Ubuntu 18 LTS, I am getting following error after firing command "sudo apt update"

Get:7 http://us.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]      
Fetched 252 kB in 1s (334 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/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/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

Now while installing any new software I am getting same error again and again.

Is there any solution on this?

2 Answers2

12

You have problem with python-apt package.Try sudo apt-get remove --purge python3-apt and sudo apt autoremove then install: apt-get install python3-apt

gongarek
  • 327
4

This may be because /usr/bin/python3 symlink is not pointing to the Python3 version that ubuntu expects. Ubuntu 18.04 is presently on 3.6.9.

To correct this you may need to re-create the symlink /usr/bin/python3 to point to /usr/bin/python3.6.

If you cannot find the python3.6 executable or it does not execute correctly, you may need to purge and re-install python3-apt as per gongarek's comment.

I came across this today because last night I pointed /usr/bin/python3 to my Python 3.7 installation due to a cron issue. I use Python 3.7 for all new code to leverage the futures feature (from __future__ import print_function) which is not supported in Python 3.6. This was an issue with cron jobs as cron was using /usr/bin/python3 to execute the script which failed to execute. My work around now has to be explicitly specifying the correct path to the python 3.7 executable.

With this cron issue, you would see in the cron execution stderr/stdout logging:

SyntaxError: future feature annotations is not defined
Traceback (most recent call last):
  File "./mem_check.py", line 20, in <module>
    import qcorelite as Q_
  File "/home/tquinn/_data/tqmain/sys/memory_check/qcorelite.py", line 1
    from __future__ import annotations, print_function
Timothy C. Quinn
  • 1,213
  • 1
  • 14
  • 22
  • This was my exact issue. Thank you. – Ksyqo Jul 06 '21 at 15:11
  • Cool. I have since moved away from touching the built-in Python that Ubuntu uses and using pyenv to manage my userland python scripts. It takes a bit of getting used to but has saved me a world of trouble mucking around in the Python that Ubuntu uses. Good Luck! – Timothy C. Quinn Jul 07 '21 at 03:27