Ubuntu Desktop 24.04 LTS - System issue after remove default python

Ubuntu Support Template

Ubuntu Version:
24.04 LTS

Desktop Environment (if applicable):
GNOME

Problem Description:
System issue after remove default python (3.12) on Ubuntu Dekstop 24.04 LTS,
and im update to 3.11 using PPA , & update manually build using official site 3.13.9 (https://www.python.org/ftp/python/3.13.9/Python-3.13.9.tgz)

AI (chat GPT,Gemma3, Deepseek R1, ETC) cant solved my problem

help me

sudo apt update

Hit:1 http://id.archive.ubuntu.com/ubuntu noble InRelease
Hit:2 https://download.docker.com/linux/ubuntu noble InRelease
Hit:3 http://id.archive.ubuntu.com/ubuntu noble-updates InRelease
Hit:4 http://id.archive.ubuntu.com/ubuntu noble-backports InRelease
Hit:5 https://nvidia.github.io/libnvidia-container/stable/deb/amd64  InRelease
Hit:6 https://deb.nodesource.com/node_22.x nodistro InRelease
Hit:7 http://security.ubuntu.com/ubuntu noble-security InRelease
Hit:8 https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu noble InRelease
Hit:9 https://pkg.cloudflare.com/cloudflared any InRelease
Traceback (most recent call last):
  File "/usr/lib/cnf-update-db", line 3, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Reading package lists... Done
N: Skipping acquire of configured file 'main/binary-i386/Packages' as repository 'https://deb.nodesource.com/node_22.x nodistro InRelease' doesn't support architecture 'i386'
N: Skipping acquire of configured file 'main/binary-i386/Packages' as repository 'https://pkg.cloudflare.com/cloudflared any InRelease' doesn't support architecture 'i386'
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
stackoverflow*com/questions/56218562/how-to-fix-modulenotfounderror-no-module-named-apt-pkg
sudo apt-get install --reinstall  python3-apt
sudo apt-get install --reinstall  python3-commandnotfound 
sudo apt-get install python3.11
sudo apt-get install python3.12
root@VM-RND-001:~# ls -l /usr/lib/python3/dist-packages/apt_pkg.*
lrwxrwxrwx 1 root root     70 Oct 25 18:48 /usr/lib/python3/dist-packages/apt_pkg.cpython-311-x86_64-linux-gnu.so -> /usr/lib/python3/dist-packages/apt_pkg.cpython-312-x86_64-linux-gnu.so
-rw-r--r-- 1 root root 347328 Jul 25 22:23 /usr/lib/python3/dist-packages/apt_pkg.cpython-312-x86_64-linux-gnu.so
lrwxrwxrwx 1 root root     70 Nov  9 20:46 /usr/lib/python3/dist-packages/apt_pkg.cpython-313-x86_64-linux-gnu.so -> /usr/lib/python3/dist-packages/apt_pkg.cpython-312-x86_64-linux-gnu.so
root@VM-RND-001:~# python3 -c "import apt, apt_pkg; print('APT OK')"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    import apt, apt_pkg; print('APT OK')
    ^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'apt'

only one can be fixed, but just py3compile to change 3.13.9

mv /usr/bin/py3compile /usr/bin/py3compile.orig
ln -s $(which true) /usr/bin/py3compile

still not solve the problem

python list

root@VM-RND-001:~# sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                       Priority   Status
------------------------------------------------------------
  0            /usr/local/bin/python3.13   2         auto mode
  1            /usr/bin/python3.11         1         manual mode
* 2            /usr/local/bin/python3.13   2         manual mode

Press <enter> to keep the current choice[*], or type selection number: ^C
root@VM-RND-001:~# sudo update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                       Priority   Status
------------------------------------------------------------
  0            /usr/local/bin/python3.13   2         auto mode
  1            /usr/bin/python3.11         1         manual mode
* 2            /usr/local/bin/python3.13   2         manual mode

Press <enter> to keep the current choice[*], or type selection number: ^C

No doubt that you have found out the hard way, python3 is an integral part of the Ubuntu plumbing.

Using Ubuntu 24.04 (gnome 46), I have 1020 installed packages dependent on python3

mark@gmktec:~$ apt-cache rdepends python3 | grep -v python | wc -l
1020
mark@gmktec:~$

Next, ubuntu-minimal depends on python3

mark@gmktec:~$ apt show ubuntu-minimal
Package: ubuntu-minimal
Version: 1.539.2
Priority: important
Section: metapackages
Source: ubuntu-meta
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 17.4 kB
Depends: adduser, apt, apt-utils, console-setup, debconf, debconf-i18n, dhcpcd-base, e2fsprogs, eject, init, iproute2, iputils-ping, kbd, kmod, less, locales, lsb-release, mawk, mount, netbase, netcat-openbsd, netplan.io, passwd, procps, python3, sensible-utils, sudo, tzdata, ubuntu-keyring, ubuntu-pro-client, udev, vim-tiny, whiptail
Recommends: rsyslog
Breaks: initramfs-tools (<< 0.142ubuntu9~)
Task: minimal
Download-Size: 11.1 kB
APT-Manual-Installed: yes
APT-Sources: http://gb.archive.ubuntu.com/ubuntu noble-updates/main amd64 Packages
Description: Minimal core of Ubuntu
 This package depends on all of the packages in the Ubuntu minimal system,
 that is a functional command-line system with the following capabilities:
 .
  - Boot
  - Detect hardware
  - Connect to a network
  - Install packages
  - Perform basic diagnostics
 .
 It is also used to help ensure proper upgrades, so it is recommended that
 it not be removed.

N: There is 1 additional record. Please use the '-a' switch to see it
mark@gmktec:~$

Two commands above found here


I’m pretty sure that you have now guessed the solution to your predicament?

  • Using a “Try Ubuntu” live session, backup your data (if, at all, possible, given what has happened)
  • Install Ubuntu 24.04 again
  • Restore your data

Up and running in an hour?

4 Likes

You have learned the hard way that you should never remove any python system packages that are the default on your computer as they are an integral part of the OS.
There are some situations I believe where you can add other additional versions of some python packages but removing the originals is almost certain to end in tears. And please do not assume this is still correct as I’m simply passing on a comment I read years ago and it may now be totally wrong.

2 Likes

I’m curious… That’s a downgrade, not an upgrade. What made you decide to do this?

2 Likes

This topic was automatically closed after 30 days. New replies are no longer allowed.