2

I think I may have broken something with python because I previously tried to update to python3, linked it to /usr/bin/python (which I now realize is the wrong thing to do) don't have the commands specifically that were used.

I can no longer get apt-get update or any apt-get commands to work on my system.

Here is the result of an apt-get update:

Hit:1 http://archive.ubuntu.com/ubuntu bionic InRelease
Hit:2 http://archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:3 https://packages.microsoft.com/repos/azure-cli bionic InRelease
Hit:4 http://archive.ubuntu.com/ubuntu bionic-backports InRelease
Hit:5 http://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:6 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic InRelease
Hit:7 https://apt.releases.hashicorp.com bionic 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'
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

I also was able to presumably fix my pythonpath:

~$ sudo apt --fix-broken install python
Reading package lists... Done
Building dependency tree
Reading state information... Done
python is already the newest version (2.7.15~rc1-1).
0 upgraded, 0 newly installed, 0 to remove and 182 not upgraded.

Now my python links look like this:

~$ ls -al /usr/bin/python
lrwxrwxrwx 1 root root 18 Apr  1 10:30 /usr/bin/python -> /usr/bin/python2.7

and python3 is no longer linked to /usr/bin/python:

~$ ls -al /usr/bin/python3
lrwxrwxrwx 1 root root 18 Mar  1 21:22 /usr/bin/python3 -> /usr/bin/python3.8

Made some slight progress here by using update-alternatives:

update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.8


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

  Selection    Path                Priority   Status
------------------------------------------------------------
  0            /usr/bin/python3.8   2         auto mode
* 1            /usr/bin/python2.7   1         manual mode
  2            /usr/bin/python3.8   2         manual mode

Whether I leave it at python3.8 or switch to the python2.7 (with update-alternatives --config python) I keep getting the same output from apt-get

Does anyone have any theories on how to fix this? It is still pointing to python3 despite setting the python alternatives to 2.7. I presume there is a problem with update-conf-db somehow.

1
  • please add apt-cache policy python3 to your question.
    – nobody
    Apr 2, 2022 at 15:22

2 Answers 2

4

The errors you’re getting aren’t caused by apt directly, but by one of its hooks; specifically, the updates for command-not-found (the helper which tells you which package to install when you try to run a command which doesn’t exist on your system). apt update itself has completed successfully.

To avoid the errors, disable the hook:

sudo mv /etc/apt/apt.conf.d/50command-not-found{,.disabled}

You should then be able to run apt update without seeing errors. If another hook fails, you can disable that using the same technique as above (see also Debian buster arm64 apt return error message, package installation works anyway).

update-cnf-db is a Python 3 script, so your Python 3 setup might be broken as well. Determining how to fix that in your situation would require more information; you should make sure that your /usr/bin/python3 points to Ubuntu’s version of Python 3.

Once you’ve got update-cnf-db working, you can re-enable the hook by reversing the command above:

sudo mv /etc/apt/apt.conf.d/50command-not-found{.disabled,}
4
  • This seems more in line with what my problem might be; thank you for answering. What is "Ubuntu's version of python 3?" It points to an installed version presently which is probably not what I want? Apr 1, 2022 at 15:54
  • I mean the version provided by the Ubuntu packages; it’s just a hunch, but the “ModuleNotFoundError: No module named 'apt_pkg'” error means that whatever /usr/bin/python3 is, it can’t find the appropriate apt_pkg module, and that’s version-dependent — e.g. on my system with python3.9, it’s /usr/lib/python3/dist-packages/apt_pkg.cpython-39-x86_64-linux-gnu.so. If python3 points to another version of Python, there won’t be a corresponding apt_pkg module. Apr 1, 2022 at 16:17
  • It could also be that python3-apt is broken, but that would be a little more surprising. Apr 1, 2022 at 16:17
  • Interesting; Thank you it looks like /usr/lib/python3/dist-package directory is missing the apt_pkg module so that might confirm a broken python3 - trying to fix that now, presumably one of the python3 versions would be the correct one. Apr 4, 2022 at 13:14
-1

The same error occurred in ubuntu 18.04.

after execute sudo apt update occurred error like this:

Hit:4 http://archive.ubuntu.com/ubuntu bionic-backports 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'
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

OS environment is

NAME="Ubuntu"
VERSION="18.04.6 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.6 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

I tried to :

cd /usr/lib/python3/dist-packages
ls -la /usr/lib/python3/dist-packages
sudo cp apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so

It's all OK.

2
  • This is not an answer.
    – Daniel
    Jun 13, 2023 at 0:49
  • Does "It's all OK" mean (1) those commands ran successfully but you still have the overall problem, or (2) after running those commands, the problem is resolved?
    – Jeff Schaller
    Jun 16, 2023 at 13:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .