Linux – CentOS /v8 – Avail Python/v2

Background

Let us get Python version 2 to be the default Python version on our machine.

Outline

  1. List installed Python Versions
  2. If not installed, Install Python version 2
  3. Review Installed Locations
  4. Set Python Version 2 as the default version of Python
  5. Confirm Current Version of Python

List Installed Python Versions

Outline

  1. alternatives
  2. compgen

alternatives

Outline

  1. List currently installed versions
    • Update-alternatives –display

Syntax


update-alternatives --display [name]

Sample


update-alternatives --display python

Output

Output Image

alternatives-update.display.python.01.20200520.0554AM

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

  1. Status
    • Auto
  2. Current
    • /usr/libexec/no-python
      • /usr/libexec/no-python – priority 404
  3. Other
    • /usr/bin/python3
      • /usr/bin/python3 – priority 300

compgen

Syntax


compgen -A command [cmd]

Sample


compgen -A command python

Output

Output Image

compgen.01.20200519.1135PM

Output-Text

>compgen -A command python
python3.6
python3.6m
python3

Explanation

  1. Python Version Installed
    • v3.6

Install Python Version -v2

Outline

  1. yum

yum

Outline

  1. Install Python v2 using yum
    • yum install python2

Syntax


yum install [package]

Sample


sudo yum install -y python2

Output

Output Image

yum.install.01.20200520.1209AM

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

  1. Python v2 installed

List Python Folders – Binary

Outline

  1. whereis
  2. ls

whereis

Outline

  1. Where are our python binaries

Syntax


whereis [app] 

Sample


>whereis -b -B /usr/bin -f python

Guide

  1. whereis
    • -b
      • Look for binaries

Output Image

whereis.01.20200520.0642AM

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

  1. Python is available via the following folders
    • v3
      • /usr/bin/python3.6m
      • /usr/bin/python3.6
    • v2
      • /usr/bin/python2

 

ls

Outline

  1. List folders that have python binaries

Syntax


ls /usr/bin 

Sample


ls -lrt /usr/bin/python* | grep ^l | grep -v "/alternatives"

Guide
  1. ls’
    • -lrt
      • Include Soft links identifiers
  2. grep
    • ^l
      1. Make sure l is the first character
    • /alternatives
      • skip entries that have alternatives in their name

Output

Output Image

ls.python.02.20200520.1229AM

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

  1. Python is available via these softlinks
    • v2
      • /usr/bin/python2
    • v3
      • /usr/bin/python3.6m
      • /usr/bin/python3.6

List Installed Python Versions – Take 2

Outline

  1. alternatives

alternatives

Outline

  1. List currently installed versions
    • Update-alternatives –display

Syntax


update-alternatives --display [name] 

Sample


update-alternatives --display python

Output

Output Image

alternatives-update.display.python.02.20200520.0608AM

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

  1. Status
    • Auto
  2. Current
    • /usr/libexec/no-python
      • /usr/libexec/no-python – priority 404
  3. Other
    • /usr/bin/python3
      • /usr/bin/python3 – priority 300
    • /usr/bin/python2
      • /usr/bin/python2 – priority 200

Configure Python Version

Outline

  1. alternatives

alternatives

Outline

  1. Configure Alternatives
    • Configure alternatives for python
      • GUI
        • alternatives –config name
      • Script
        • update-alternatives –set name

GUI

alternatives –config name
Syntax

sudo alternatives --config python

Sample

sudo alternatives --config python

Output

Output Image

alternatives-config.02.20200520.0533AM

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

  1. 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

update-alternatives.set.python.01.20200520.0622AM

Output-Text
>sudo update-alternatives --set python /usr/bin/python2
[sudo] password for dadeniji:
>

Explanation

  1. Using Script, we switched the current version of Python to version 2

 

Inquire for Current Version

Outline

  1. alternatives

alternatives

Outline

  1. List currently installed versions
    • Update-alternatives –display

Syntax


update-alternatives --display [application]

Sample


update-alternatives --display python

Output

Output Image

alternatives-update.display.01.20200520.0542AM

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

  1. Status
    • Manual
    • And, not automatic
  2. Current Link
    • Link currently points to /usr/bin/python2
  3. Priority
    • /usr/libexec/no-python – priority 404
    • /usr/bin/python3 – priority 300
    • /usr/bin/python2 – priority 200

Get Python Version

Outline

  1. python –version

python -version

Outline

  1. Invoke python
    • Pass along argument –version

Syntax


python --version

Sample


python --version

Output

Output Image

python.01.20200520.0100AM

Output-Text

Python 2.7.16

Explanation

  1. We have back the version number we switched to
  2. 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.

 

References

  1. geeksforgeeks.org
    • whereis command in Linux with Examples
      Link
  2. itsupportwale
    • Mohit Malviya
      • How to upgrade to python 3.7 on Ubuntu 18.10
        Link

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s