20

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
  • 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. Commented Feb 27, 2022 at 14:56

1 Answer 1

34

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 
7
  • 1
    Works for me Ubuntu 20.04 :) Commented Oct 16, 2020 at 14:26
  • 1
    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 Commented Sep 3, 2021 at 1:55
  • worked for me on Ubuntu 20.04 as well Commented Jan 31, 2022 at 19:25
  • worked form me just now :) Commented Feb 4, 2022 at 9:51
  • 2
    Broke my system, now I can't open the terminal anymore Commented Dec 8, 2023 at 10:49

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.