Site icon CentLinux

Effectively Use Alternatives command for Python

Share on Social Media

In this Linux tutorial, you will learn how to use Alternatives command to set Python Versions in Linux. #centlinux #linux #python

Problem Statement:

We have two versions of Python Programming Language are already installed on our Rocky Linux 9 server.

Python 3.9 is by default installed on all Red Hat Enterprise Linux 9 operating systems including Minimal installations.

Install Python on Linux:

We have installed Python 3.10 on this machine, and can also install the same by following our previous article: How to install Python 3.10 on Rocky Linux 9

Verify the existance of Python versions by executing following commands.

# python3.9 -V
Python 3.9.14

# python3.10 -V
Python 3.10.4

Alternatives Command Usage:

Linux operating system provides alternatives command to set an active link to a specific command, among all the installed versions of that command.

Use the following command to set an active version of Python.

# alternatives --config python

If the above command returns no output, check whether python is available in commands list known to alternatives.

# alternatives --list
libnssckbi.so.x86_64    auto    /usr/lib64/pkcs11/p11-kit-trust.so
soelim                  auto    /usr/bin/soelim.groff
iptables                auto    /usr/sbin/iptables-nft
ebtables                auto    /usr/sbin/ebtables-nft
arptables               auto    /usr/sbin/arptables-nft
cifs-idmap-plugin       auto    /usr/lib64/cifs-utils/cifs_idmap_sss.so
man                     auto    /usr/bin/man.man-db
ld                      auto    /usr/bin/ld.bfd

You can see that, there is no python command in list. Therefore, you have to add Python in alternatives info base.

Check the location of Python executable files.

# whereis python3.9
python3.9: /usr/bin/python3.9 /usr/lib/python3.9 /usr/lib64/python3.9 /usr/include/python3.9 /usr/share/man/man1/python3.9.1.gz

# whereis python3.10
python3.10: /usr/local/bin/python3.10 /usr/local/lib/python3.10

Note down the location of Python executables from above output and then execute following commands to define Python alternatives.

# alternatives --install /usr/bin/python python /usr/bin/python3.9 1
# alternatives --install /usr/bin/python python /usr/local/bin/python3.10 2

You can now execute following command to set an active version of Python command.

# alternatives --config python

There are 2 programs which provide 'python'.

  Selection    Command
-----------------------------------------------
   1           /usr/bin/python3.9
*+ 2           /usr/local/bin/python3.10

Enter to keep the current selection[+], or type selection number:

Currently, Python3.10 is set as active Python command.

You can verify it by executing following command at Linux bash prompt.

# python -V
Python 3.10.4

Now, change the active version of Python command to 3.9 as follows.

# alternatives --config python

There are 2 programs which provide 'python'.

  Selection    Command
-----------------------------------------------
   1           /usr/bin/python3.9
*+ 2           /usr/local/bin/python3.10

Enter to keep the current selection[+], or type selection number: 1

Check the version of Python command again.

# python -V
Python 3.9.14

Video of Alternatives Command Example:

Conclusion:

In this Linux tutorial, you will learn how to use Alternatives command to set Python Versions in Linux.

Exit mobile version