Another option that would work across different (and older) versions of Ubuntu is the "alternatives" system, either the command-line update-alternatives (should be installed) or GUI galternatives (not installed by default). It lets one configure your "preferred" version when you have multiple versions, or completely different but compatible implementations, (e.g. compilers, editors; see for example this dump: update-alternatives --get-selections).
To see which versions of python are currently the default (note: these are symbolic links to specific versions):
$ type python python2 python3
python not found
python2 is /usr/bin/python2
python3 is /usr/bin/python3
Usually (like, always, without exception) you want the default python to be python2, for compatibility reasons (otherwise you will break apt and other system utilities!) and then just explicitly invoke python3 if and when you really mean Python3.
To see what current alternatives are configured for python:
$ sudo update-alternatives --list python
error: no alternatives found
If alternatives are found, skip the following (see --config, below). Otherwise, we can configure both python2 and python3 as "alternatives" for the python command and you can switch between them (or just set python2 here and you're done; the rest is here for completeness & as an exercise):
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 40
using /usr/bin/python2 to provide /usr/bin/python (python) in auto mode
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 20
using /usr/bin/python3 to provide /usr/bin/python (python) in auto mode
$ type python
/usr/bin/python
$ python --version
Python 2.7.18
(Important! if your default python is showing as python3, then change it here to python2, otherwise apt and other system utilities may be broken!)
To use update-alternatives to change the version currently selected, do the following and choose the version you want:
$ sudo update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).
Selection Path Priority Status
- 0 /usr/bin/python2 40 auto mode
1 /usr/bin/python2 40 manual mode
2 /usr/bin/python3 20 manual mode
Press <enter> to keep the current choice[*], or type selection number: 0
To completely remove all python alternative configurations (e.g., to start over, or just undo everything if anything weird is going on):
$ sudo update-alternatives --remove-all python