Skip to content

Instantly share code, notes, and snippets.

@rrmhearts
Created October 9, 2019 14:44
Show Gist options
  • Save rrmhearts/5728df70d8ebafc1014a69183e3abaef to your computer and use it in GitHub Desktop.
Save rrmhearts/5728df70d8ebafc1014a69183e3abaef to your computer and use it in GitHub Desktop.
Python Switch Version

Change python version system-wide

To change python version system-wide we can use update-alternatives command. Logged in as a root user, first list all available python alternatives:

# update-alternatives --list python
update-alternatives: error: no alternatives for python

The above error message means that no python alternatives has been recognized by update-alternatives command. For this reason we need to update our alternatives table and include both python2.7 and python3.4:

# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
# update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2
update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python (python) in auto mode

The --install option take multiple arguments from which it will be able to create a symbolic link. The last argument specified it priority means, if no manual alternative selection is made the alternative with the highest priority number will be set. In our case we have set a priority 2 for /usr/bin/python3.4 and as a result the /usr/bin/python3.4 was set as default python version automatically by update-alternatives command.

# python --version
Python 3.4.2

Next, we can again list all python alternatives:

# update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.4

From now on, we can anytime switch between the above listed python alternative versions using below command and entering a selection number:

# update-alternatives --config python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment