How to upgrade to Python 3.9.0 on Ubuntu 18.04 LTS without internal bleeding!

SPOILER ALERT:
Upgrading the Python version on Ubuntu breaks some packages like apt and pip in your system!
[Bleeding SysAdmin crawling]

In this article we’re going to upgrade python and fix the issues.

Start reading this article and executing the instruction:
How to upgrade to Python 3.9.0 on Ubuntu 18.04 LTS
It will guide you through updating the repo and basic updating stuff.

After that, try running sudo apt update.
See!

Traceback (most recent call last): File "/usr/lib/cnf-update-db", line 8, in from CommandNotFound.db.creator import DbCreator File "/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py", line 11, in import apt_pkg ModuleNotFoundError: No module named 'apt_pkg'
ModuleNotFoundError: No module named ‘apt_pkg’

or event a simple pip3 install virtualenv:

Traceback (most recent call last):   File "/usr/bin/pip3", line 9, in <module>     from pip import main   File "/usr/lib/python3/dist-packages/pip/__init__.py", line 29, in <module>     from pip.utils import get_installed_distributions, get_prog   File "/usr/lib/python3/dist-packages/pip/utils/__init__.py", line 23, in <module>     from pip.locations import (   File "/usr/lib/python3/dist-packages/pip/locations.py", line 9, in <module>     from distutils import sysconfig ImportError: cannot import name 'sysconfig' from 'distutils' (/usr/lib/python3.9/distutils/__init__.py)
ImportError: cannot import name ‘sysconfig’ from ‘distutils’ (/usr/lib/python3.9/distutils/__init__.py)

Well, that’s a lot of errors!
BUT:
This is GNU/Linux bruh! We always have solutions! [D’Oh!]

At first, you need to resync old python packages to new corresponding paths, we’ll first navigate to python3 dist-packages folder and use ln command to make links.

cd /usr/lib/python3/dist-packages sudo ln -s apt_pkg.cpython-{36m,39m}-x86_64-linux-gnu.so
Linking old python packages to new ones

If you consider your old python version 3.6 and the new one 3.9 the ln command would be something like above: (change 36 and 39 numbers accordingly)

Then uninstall any existing python-apt packages using two apt purge commands and reinstall them just like a normal apt package

Reinstalling python-apt package

BEWARE: using apt with --reinstall flag doesn’t work!
Now run sudo apt update.

YaY! It’s fixed! [Mic Drop!]

But wait a second, I still can’t install pip packages!
Just run this command and you’ll be good to go:
sudo apt install python3.9-distutils python3.9-dev

Happy SysAdminning!

Thanks to the free software community for this beautiful world we’re living in!

Sources:
How to upgrade to Python 3.9.0 on Ubuntu 18.04 LTS
StackOverFlow: python-dev installation error: ImportError: No module named apt_pkg
AskUbuntu: ImportError: cannot import name ‘sysconfig’ from ‘distutils’ (/usr/lib/python3.9/distutils/__init__.py)

https://stackoverflow.com/questions/13708180/python-dev-installation-error-importerror-no-module-named-apt-pkg