2

If I run a non-existing command in the terminal (as for example lasdfese or vlsefoserlks) I get the output below. Why? How to get back to normal behaviour? I could add that this happend after updating to python3.8, and that I am using ubuntu 18.04.

Traceback (most recent call last):
  File "/usr/lib/command-not-found", line 28, in <module>
    from CommandNotFound import CommandNotFound
  File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 19, in <module>
    from CommandNotFound.db.db import SqliteDatabase
  File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, 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/command-not-found", line 28, in <module>
    from CommandNotFound import CommandNotFound
  File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 19, in <module>
    from CommandNotFound.db.db import SqliteDatabase
  File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Johan hvn
  • 281
  • 3
  • 7

2 Answers2

2

I had the same issue. I resolved it.

There is a python script called command-not-found in /usr/lib/command-not-found.

This script, gets updated after you upgrade your python version. At least that's what happened to me. I upgraded from Python3.6 to Python3.8.

Now, the issue is, This script is working based on the previous version of Python. So I went into this file, and changed the first line from

#!/usr/bin/python3 (which refers to python3.8)

to:

#!/usr/bin/python3.6 (which is python version comes with Ubuntu 18.04)

0

I faced the same error with the "ubuntu-drivers devices" command. Inspired by the @flowfelis' answer, I updated python version and it resolved the problem.

$ sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).

Selection Path Priority Status

0 /usr/bin/python3.7 2 auto mode

  • 1 /usr/bin/python3.7 2 manual mode 2 /usr/bin/python3.8 1 manual mode

Press <enter> to keep the current choice[*], or type selection number: 2 update-alternatives: using /usr/bin/python3.8 to provide /usr/bin/python3 (python3) in manual mode

Haldun
  • 1