Here is an answer that I hopes differentiate between using the python-is-python3 and update-alternatives options described in different answers. At first I believed that using BOTH of these options gave me an optimal solution, after testing this for a short while I realized I had a problem.
I am using Ubuntu 20.04.6 LTS (Focal Fossa), running under the Windows Subsystem for Linux (WSL) on Windows 10.
My first observation is that I didn't have python2 installed at all; I did have python3.8, provided as the default "system" Python 3 package for this distro. I noticed that the python
command didn't work:
$ python --version
Command 'python' not found, did you mean:
command 'python3' from deb python3
command 'python' from deb python-is-python3
I solved this by installing python-is-python3, which creates symlinks in /usr/bin/
:
$ ls -l /usr/bin/python*
lrwxrwxrwx 1 root root 7 Apr 15 2020 /usr/bin/python -> python3
lrwxrwxrwx 1 root root 9 Mar 13 2020 /usr/bin/python3 -> python3.8
This may be enough, but I have multiple versions of Python 3 installed for... reasons. I wanted to be able to choose between different versions of Python 3 so the update-alternatives package was useful:
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 8
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 10
The effect of this was (1) to make python3.10
the default on my system, since it has the highest priority--the number at the end--and (2) allows me to switch the system-wide version of python via:
$ sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/python3.10 10 auto mode
1 /usr/bin/python3.10 10 manual mode
2 /usr/bin/python3.8 8 manual mode
Note that this is using python3 as the command/alias.
Note: I don't recommend using update-alternatives to switch (frequently) between different default versions of Python 3.x for your projects. It is better to use virtual environments within each project, installed using an explicit version of Python, e.g.
$ python3.8 -m venv .venv
UPDATE
Since using the approach described above I have discovered a problem: when trying to upgrade my disto using apt-get update
, etc. I saw problems such as this: ModuleNotFoundError: No module named 'apt_pkg'.
The cause was that I had changed my default version of python3
to be python3.10
and this was incompatible with many system scripts in Ubuntu 20.04, which expected python3.8
.
Thus I have reverted my default to be python3.8
and will explicitly use python3.10
to create venvs for my projects.