8

I get the following error in various situations, for example when I try to start a package that is not installed:

   $ tldr
    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'

I've tried the solution in https://askubuntu.com/questions/1190328/modulenotfounderror-no-module-named-apt-pkg but that didn't help. I've also tried installing and reinstalling different versions of python, and change the update-alternatives links. I suspect that I somehow ruined the configuration of the python the OS uses.

Some general info:

$ cat /etc/os-release
NAME="KDE neon"
VERSION="5.18"
ID=neon
ID_LIKE="ubuntu debian"
PRETTY_NAME="KDE neon User Edition 5.18"
VARIANT="User Edition"
VERSION_ID="18.04"
HOME_URL="http://neon.kde.org/"
SUPPORT_URL="http://neon.kde.org/"
BUG_REPORT_URL="http://bugs.kde.org/"
LOGO=start-here-kde-neon
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

python2:

$ update-alternative s --config python
    There is only one alternative in link group python (providing /usr/bin/python): /usr/bin/python2.7
    Nothing to configure.

$ python --version
Python 2.7.17

python3:

$ update-alternatives --config python3
There is only one alternative in link group python3 (providing /usr/bin/python3): /usr/bin/python3.7
Nothing to configure.

$ python3 --version
Python 3.7.5
1
  • In my case, none of the solutions for various questions worked. However, after reading your question, I did check for the python version and realized it was set to 3.7: moving to 3.6 fixed the issue. Feb 27 at 14:56

1 Answer 1

18

Original answer from https://askubuntu.com/q/1242009/747557, credit to @Hmdbbgh. The solution that worked for me was to reinstall python3-apt:

sudo apt remove python3-apt
sudo apt autoremove
sudo apt autoclean
sudo apt install python3-apt
5
  • 1
    Works for me Ubuntu 20.04 :) Oct 16, 2020 at 14:26
  • I couldn't open Terminal on Ubuntu for months, had to do alternate between between Ctrl+Alt+F3 and Ctrl+Alt+F7 for a while now. Today I finally looked this up and ran above commands and it fixed my "Terminal not opening on Ubuntu" problem. Thanks
    – Tin Tran
    Sep 3, 2021 at 1:55
  • worked for me on Ubuntu 20.04 as well Jan 31 at 19:25
  • worked form me just now :) Feb 4 at 9:51
  • work for me, thanks!
    – Tung
    Feb 17 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.