Python users typically use a non-system Python distribution such as
These Python distributions typically allow easy switching between Python version.
Where system Python must be used, the default Python version can be switched persistently with
Update-alternatives without sudo
This is a one-time setup.
We configure Bash shell to use
~/.local/bin instead of system-wide
/usr as follows:
~/.bashrc, adding the line:
Enable switching Python default between Python 3 and 2.7 (useful for Raspberry Pi on Raspbian 9.1) with these one-time commands:
update-alternatives --install $HOME/.local/bin/python python /usr/bin/python3 20 update-alternatives --install $HOME/.local/bin/python python /usr/bin/python2 10
Then one can at any time select the default system Python version, without sudo, by
update-alternatives --config python
With old Linux distros,
apt update may fail after making system Python default to Python 3
Just temporarily switch back to Python 2 as default if this becomes an issue.
Pip update-alternatives is NOT needed
You might expect something like
--slave /usr/bin/pip pip /usr/bin/pip2 to be part of the
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
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.