1

I am running Ubuntu 18.04 and I noticed that at the top-left corner of the screen, suddenly this icon had appeared. I thought that it was due to a brief disconnection, so I didn't worry that much.

Now, whenever I run sudo apt-get update the process stops with this error:

Hit:1 http://it.archive.ubuntu.com/ubuntu bionic InRelease
Hit:2 http://it.archive.ubuntu.com/ubuntu bionic-updates InRelease             
Hit:3 http://it.archive.ubuntu.com/ubuntu bionic-backports InRelease           
Hit:4 http://security.ubuntu.com/ubuntu bionic-security InRelease              
Hit:5 http://packages.ros.org/ros/ubuntu bionic InRelease                      
Hit:6 https://download.sublimetext.com apt/stable/ InRelease                   
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

The only "wicked" thing I recall doing before seeing such icon is entering sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 0 to prioritize this newer version of Python.

Since by the error I can see that some python import fails, I thought I'd make it clear.
So far I've tried:

  • sudo apt-get clean
  • sudo dpkg --configure -a
  • sudo apt-get autoremove
  • sudo apt-get upgrade
  • sudo apt-get install apt_pkg --> E: Unable to locate package apt_pkg
  • sudo update-alternatives --remove-all python && sudo update-alternatives --remove-all python3

--> output error changed to

(...)
sh: 1: /usr/lib/cnf-update-db: not found           
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

Moreover: I can't access my terminal anymore. The only unconvenient way I find is to right-click anywhere and then select "Open terminal". For this, I've tried

  • changing the language settings, even if they never got corrupted.
  • launching the terminal from the 'Apps', which results in a notification saying failed to execute child process gnome-terminal: no such file or directory
  • launching it via 'right click' and entering $ gnome-terminal results in bash: /usr/bin/gnome-terminal: /usr/bin/python3: bad interpreter: No such file or directory

Hope that solving one solves the other..

davide m.
  • 121
  • could you be more specific on how to do so? would it break some configs that i have? – davide m. Sep 23 '19 at 17:37
  • sudo apt-get install python3-apt (not sudo apt-get install apt_pkg) –  Sep 23 '19 at 18:02
  • I have got exactly the same problem and I am exactly at the stage in which @davidem. was on Sep 23 '19 at 17:36. is the re-installation of Ubuntu really the only solution? – Alberto Mar 30 '20 at 23:15
  • @Alberto for me it was, the solutions I was able to find had root problems that didn't match with mine. – davide m. Mar 31 '20 at 10:04

3 Answers3

1

Use update-alternatives to put the default python version back to what it was. I am also running Ubuntu 18.04 my python version is 2.7.15

$ python --version
Python 2.7.15+

Then I would recommend looking into setting up virtual python environments using venv (venv in python3 and virtualenv for python2) for testing or running programs that require python 3.

First, make sure you have pip installed by running sudo apt install python3-pip or ensure that it is up to date by running python3 -m pip install --user --upgrade pip.

Then install venv by running python3 -m pip install --user virtualenv

I hate to link you to an outside webpage but there are clear instructions on doing this on the python.org website. You can find a link to the site at here

Gordster
  • 1,719
  • I'll tell you the truth, i don't actually need to run programs that require python3. I was just testing a compatibility, but for some reason python3 was an older version, so i added that alternative in order to prioritize the 3.7 version. that's all – davide m. Sep 23 '19 at 16:50
  • 1
    Ahhh yes. You have to be careful when using update-alternatives because so much of the operating system relies on the default version of python. Something I had to learn the hard way. – Gordster Sep 23 '19 at 16:53
  • well, had i know this before i'd have kept myself at distance. what can you suggest me? i've already removed all the update-alternatives but the only result is the output error changing. i've edited the question – davide m. Sep 23 '19 at 16:55
  • What version of Ubuntu is this? Please add that in the question as well – Gordster Sep 23 '19 at 17:07
  • forgot to say, 18.04, edited – davide m. Sep 23 '19 at 17:17
  • I updated my answer. The default python version should be 2.7.15. use update-alternatives --config to put it back. and then check to make sure it is correct by running python --version – Gordster Sep 23 '19 at 17:20
  • python --version does give me the expected output, but still the same errors occurr. i have edited the question to include more errors when launching the terminal – davide m. Sep 23 '19 at 17:36
1

Well, I ended up reinstalling Ubuntu: no matter how many solutions I found, the causing problems were too diverse, even if all resembled the fact of somehow messing with the Python default version.

I guess Python truly is something that you should be extra-careful when handling with Ubuntu. Lesson learned the hard way.

davide m.
  • 121
0

Hm, try the "windows-way"

sudo apt install --reinstall  python3-commandnotfound  command-not-found            
nobody
  • 5,437