Set Python version with update-alternatives

less than 1 minute read

On embedded systems where system Python is used, to persistently switch default Python version, use update-alternatives.

For example, to switch Python default between Python 3 and 2.7 (useful for Raspberry Pi on Raspbian 9.1)

update-alternatives --install /usr/bin/python python /usr/bin/python3 20 

update-alternatives --install /usr/bin/python python /usr/bin/python2 10 

Then at any time, select the default Python version with

update-alternatives --config python


You might expect something like --slave /usr/bin/pip pip /usr/bin/pip2 to be part of the python2 and python3 config. However, pip is a script with the shebang #!/usr/bin/python that is already affected by update-alternatives --config python. Therefore, we do NOT use --slave with Python --install.


Fix Ubuntu 17.04 apt install

Ubuntu 17.04 was giving errors on apt install or apt upgrade any program. The syntax errors were due to Python 3 being the default in Ubuntu 17.04, even though I had already installed Python 2.

I fixed this issue by using update-alternatives with Python 2.7 as above.

Leave a Comment