Background
Let us get Python version 2 to be the default Python version on our machine.
Outline
- List installed Python Versions
- If not installed, Install Python version 2
- Review Installed Locations
- Set Python Version 2 as the default version of Python
- Confirm Current Version of Python
List Installed Python Versions
Outline
- alternatives
- compgen
alternatives
Outline
- List currently installed versions
- Update-alternatives –display
Syntax
update-alternatives --display [name]
Sample
update-alternatives --display python
Output
Output-Text
>update-alternatives --display python python - status is auto. link currently points to /usr/libexec/no-python /usr/libexec/no-python - priority 404 slave unversioned-python: (null) slave unversioned-python-man: /usr/share/man/man1/unversioned-python.1.gz /usr/bin/python3 - priority 300 slave unversioned-python: /usr/bin/python3 slave unversioned-python-man: /usr/share/man/man1/python3.1.gz Current `best' version is /usr/libexec/no-python. >
Explanation
- Status
- Auto
- Current
- /usr/libexec/no-python
- /usr/libexec/no-python – priority 404
- /usr/libexec/no-python
- Other
- /usr/bin/python3
- /usr/bin/python3 – priority 300
- /usr/bin/python3
compgen
Syntax
compgen -A command [cmd]
Sample
compgen -A command python
Output
Output Image
Output-Text
>compgen -A command python python3.6 python3.6m python3
Explanation
- Python Version Installed
- v3.6
Install Python Version -v2
Outline
- yum
yum
Outline
- Install Python v2 using yum
- yum install python2
Syntax
yum install [package]
Sample
sudo yum install -y python2
Output
Output Image
Output-Text
Transaction Summary ============================================================================================================ Install 6 Packages Total download size: 10 M Installed size: 39 M Downloading Packages: (1/6): python2-2.7.16-12.module_el8.1.0+219+cf9e6ac9.x86_64.rpm 307 kB/s | 109 kB 00:00 (2/6): python2-pip-9.0.3-14.module_el8.1.0+219+cf9e6ac9.noarch.rpm 822 kB/s | 2.0 MB 00:02 (3/6): python2-pip-wheel-9.0.3-14.module_el8.1.0+219+cf9e6ac9.noarch.rpm 494 kB/s | 1.2 MB 00:02 (4/6): python2-setuptools-39.0.1-11.module_el8.1.0+219+cf9e6ac9.noarch.rpm 1.0 MB/s | 643 kB 00:00 (5/6): python2-setuptools-wheel-39.0.1-11.module_el8.1.0+219+cf9e6ac9.noarc 523 kB/s | 289 kB 00:00 (6/6): python2-libs-2.7.16-12.module_el8.1.0+219+cf9e6ac9.x86_64.rpm 1.3 MB/s | 6.0 MB 00:04 ------------------------------------------------------------------------------------------------------------ Total 2.1 MB/s | 10 MB 00:04 Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : python2-setuptools-wheel-39.0.1-11.module_el8.1.0+219+cf9e6ac9.noarch 1/6 Installing : python2-pip-wheel-9.0.3-14.module_el8.1.0+219+cf9e6ac9.noarch 2/6 Installing : python2-libs-2.7.16-12.module_el8.1.0+219+cf9e6ac9.x86_64 3/6 Installing : python2-pip-9.0.3-14.module_el8.1.0+219+cf9e6ac9.noarch 4/6 Installing : python2-setuptools-39.0.1-11.module_el8.1.0+219+cf9e6ac9.noarch 5/6 Installing : python2-2.7.16-12.module_el8.1.0+219+cf9e6ac9.x86_64 6/6 Running scriptlet: python2-2.7.16-12.module_el8.1.0+219+cf9e6ac9.x86_64 6/6 Verifying : python2-2.7.16-12.module_el8.1.0+219+cf9e6ac9.x86_64 1/6 Verifying : python2-libs-2.7.16-12.module_el8.1.0+219+cf9e6ac9.x86_64 2/6 Verifying : python2-pip-9.0.3-14.module_el8.1.0+219+cf9e6ac9.noarch 3/6 Verifying : python2-pip-wheel-9.0.3-14.module_el8.1.0+219+cf9e6ac9.noarch 4/6 Verifying : python2-setuptools-39.0.1-11.module_el8.1.0+219+cf9e6ac9.noarch 5/6 Verifying : python2-setuptools-wheel-39.0.1-11.module_el8.1.0+219+cf9e6ac9.noarch 6/6 Installed: python2-2.7.16-12.module_el8.1.0+219+cf9e6ac9.x86_64 python2-pip-9.0.3-14.module_el8.1.0+219+cf9e6ac9.noarch python2-setuptools-39.0.1-11.module_el8.1.0+219+cf9e6ac9.noarch python2-libs-2.7.16-12.module_el8.1.0+219+cf9e6ac9.x86_64 python2-pip-wheel-9.0.3-14.module_el8.1.0+219+cf9e6ac9.noarch python2-setuptools-wheel-39.0.1-11.module_el8.1.0+219+cf9e6ac9.noarch Complete! >
Explanation
- Python v2 installed
List Python Folders – Binary
Outline
- whereis
- ls
whereis
Outline
- Where are our python binaries
Syntax
whereis [app]
Sample
>whereis -b -B /usr/bin -f python
Guide
- whereis
- -b
- Look for binaries
- -b
Output Image
Output-Text
>whereis -b -B /usr/bin -f python python: /usr/bin/python3.6 /usr/bin/python3.6m /usr/bin/python2.7 /usr/bin/python >
Explanation
- Python is available via the following folders
- v3
- /usr/bin/python3.6m
- /usr/bin/python3.6
- v2
- /usr/bin/python2
- v3
ls
Outline
- List folders that have python binaries
Syntax
ls /usr/bin
Sample
ls -lrt /usr/bin/python* | grep ^l | grep -v "/alternatives"
Guide
- ls’
- -lrt
- Include Soft links identifiers
- -lrt
- grep
- ^l
- Make sure l is the first character
- /alternatives
- skip entries that have alternatives in their name
- ^l
Output
Output Image
Output-Text
>ls -lrt /usr/bin/python* | grep ^l | grep -v "/alternatives" lrwxrwxrwx. 1 root root 9 Nov 16 2019 /usr/bin/python2 -> python2.7 lrwxrwxrwx. 1 root root 32 Nov 20 14:48 /usr/bin/python3.6m -> /usr/libexec/platform-python3.6m lrwxrwxrwx. 1 root root 31 Nov 20 14:48 /usr/bin/python3.6 -> /usr/libexec/platform-python3.6 >
Explanation
- Python is available via these softlinks
- v2
- /usr/bin/python2
- v3
- /usr/bin/python3.6m
- /usr/bin/python3.6
- v2
List Installed Python Versions – Take 2
Outline
- alternatives
alternatives
Outline
- List currently installed versions
- Update-alternatives –display
Syntax
update-alternatives --display [name]
Sample
update-alternatives --display python
Output
Output Image
Output-Text
>update-alternatives --display python python - status is auto. link currently points to /usr/libexec/no-python /usr/libexec/no-python - priority 404 slave unversioned-python: (null) slave unversioned-python-man: /usr/share/man/man1/unversioned-python.1.gz /usr/bin/python3 - priority 300 slave unversioned-python: /usr/bin/python3 slave unversioned-python-man: /usr/share/man/man1/python3.1.gz /usr/bin/python2 - priority 200 slave unversioned-python: /usr/bin/python2 slave unversioned-python-man: /usr/share/man/man1/python2.1.gz Current `best' version is /usr/libexec/no-python.
Explanation
- Status
- Auto
- Current
- /usr/libexec/no-python
- /usr/libexec/no-python – priority 404
- /usr/libexec/no-python
- Other
- /usr/bin/python3
- /usr/bin/python3 – priority 300
- /usr/bin/python2
- /usr/bin/python2 – priority 200
- /usr/bin/python3
Configure Python Version
Outline
- alternatives
alternatives
Outline
- Configure Alternatives
- Configure alternatives for python
- GUI
- alternatives –config name
- Script
- update-alternatives –set name
- GUI
- Configure alternatives for python
GUI
alternatives –config name
Syntax
sudo alternatives --config python
Sample
sudo alternatives --config python
Output
Output Image
Output-Text
>sudo alternatives --config python [sudo] password for dadeniji: There are 3 programs which provide 'python'. Selection Command ----------------------------------------------- *+ 1 /usr/libexec/no-python 2 /usr/bin/python3 3 /usr/bin/python2 Enter to keep the current selection[+], or type selection number: 3 > >
Explanation
- Using GUI, we switched the current version of Python to version 2
Script
update-alternatives –set name
Syntax
sudo update-alternatives --set [name] [folder]
Sample
sudo update-alternatives --set python /usr/bin/python2
Output
Output Image
Output-Text
>sudo update-alternatives --set python /usr/bin/python2 [sudo] password for dadeniji: >
Explanation
- Using Script, we switched the current version of Python to version 2
Inquire for Current Version
Outline
- alternatives
alternatives
Outline
- List currently installed versions
- Update-alternatives –display
Syntax
update-alternatives --display [application]
Sample
update-alternatives --display python
Output
Output Image
Output-Text
>update-alternatives --display python python - status is manual. link currently points to /usr/bin/python2 /usr/libexec/no-python - priority 404 slave unversioned-python: (null) slave unversioned-python-man: /usr/share/man/man1/unversioned-python.1.gz /usr/bin/python3 - priority 300 slave unversioned-python: /usr/bin/python3 slave unversioned-python-man: /usr/share/man/man1/python3.1.gz /usr/bin/python2 - priority 200 slave unversioned-python: /usr/bin/python2 slave unversioned-python-man: /usr/share/man/man1/python2.1.gz Current `best' version is /usr/libexec/no-python.
Explanation
- Status
- Manual
- And, not automatic
- Current Link
- Link currently points to /usr/bin/python2
- Priority
- /usr/libexec/no-python – priority 404
- /usr/bin/python3 – priority 300
- /usr/bin/python2 – priority 200
Get Python Version
Outline
- python –version
python -version
Outline
- Invoke python
- Pass along argument –version
Syntax
python --version
Sample
python --version
Output
Output Image
Output-Text
Python 2.7.16
Explanation
- We have back the version number we switched to
- And, that version number is 2.7.16
Summary
“Alternatives” offers a viable option for gracefully switching between the various installed versions of an application.