Based on https://chatgpt.com/share/67b9ff44-bd6c-8007-84f4-a51d8dddfd95
You can switch between the registered Python versions using the update-alternatives
interactive selection tool. Here’s how to do it:
-
Run the Configuration Command:
sudo update-alternatives --config python3
-
Select the Desired Version:
After running the command, you’ll see a list similar to this:
There are 2 choices for the alternative python3 (providing /usr/bin/python3). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/bin/python3.9 2 auto mode 1 /usr/bin/python3.8 1 manual mode 2 /usr/bin/python3.9 2 manual mode Press <enter> to keep the current choice[*], or type selection number:
-
Enter the Number:
Type the number corresponding to Python 3.8 (in this example, it might be 1) and press Enter. This action will change the default
python3
to Python 3.8. -
Verify the Change:
Check the currently active Python version with:
python3 --version
This process tells the alternatives system to update the symbolic link for /usr/bin/python3
to point to your chosen version.
# sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
# sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
(last digits are priorities - to be adjusted based on your needs, higher value = higher priority , so in above case "pythion 3.9" has priority 2 and has higher prioryt then python 3.8)
By default it's python 3.9 in this setup but let's switch to showcase how to do switchgin:
switch to "(auto)" pointing to python3.9
# 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.9 2 auto mode
* 1 /usr/bin/python3.8 1 manual mode
2 /usr/bin/python3.9 2 manual mode
Press <enter> to keep the current choice[*], or type selection number: 0
update-alternatives: using /usr/bin/python3.9 to provide /usr/bin/python3 (python3) in auto mode
Example command not workign with python3.9 (at least as of 2025-02-22):
# sudo add-apt-repository universe
Traceback (most recent call last):
File "/usr/bin/add-apt-repository", line 12, in <module>
from softwareproperties.SoftwareProperties import SoftwareProperties, shortcut_handler
File "/usr/lib/python3/dist-packages/softwareproperties/SoftwareProperties.py", line 68, in <module>
from gi.repository import Gio
File "/usr/lib/python3/dist-packages/gi/__init__.py", line 42, in <module>
from . import _gi
ImportError: cannot import name '_gi' from partially initialized module 'gi' (most likely due to a circular import) (/usr/lib/python3/dist-packages/gi/__init__.py)
Now let's switch to python3.8 :
# 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.9 2 auto mode
1 /usr/bin/python3.8 1 manual mode
2 /usr/bin/python3.9 2 manual mode
Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/python3.8 to provide /usr/bin/python3 (python3) in manual mode
and command works:
# sudo add-apt-repository universe
'universe' distribution component is already enabled for all sources.
# 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.9 2 auto mode
* 1 /usr/bin/python3.8 1 manual mode
2 /usr/bin/python3.9 2 manual mode
Press <enter> to keep the current choice[*], or type selection number: 0
update-alternatives: using /usr/bin/python3.9 to provide /usr/bin/python3 (python3) in auto mode