'Linux CentOS 7, how to set Python2.7 as default Python version?

I'm using a laptop running Linux CentOS 7. I installed Python2.7, and then I installed Anaconda, which installed Python3.5.2

I want my system to use Python2.7 as default, but if I type python from my terminal, it launches Python3.5.2 from Anaconda:

[davide@opennet-33-58 ~]$ python
Python 3.5.2 |Anaconda custom (64-bit)| (default, Jul  2 2016, 17:53:06) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

I tried to remove Python3 but it's still there...

How can I set Python2.7 as default Python version on my machine?

Thanks



Solution 1:[1]

For RHE/Centos. You will need to enable the correct repo if you don't have them.(For my case was the following)

subscription-manager repos --enable=rhel-6-server-optional-rpms

subscription-manager repos --enable=rhel-server-rhscl-6-rpms

Then you install

yum install scl-utils
yum install centos-release-scl-rh  (only for Centos)
yum install python27  (or any version you need to install)

Now the new python is installed,you have to enable it as default

scl enable python27 bash (with this command will be default until you logout,is good to test the changes)

To keep the changes you should create a script under /etc/profile.d/

#!/bin/bash
source scl_source enable python27

Solution 2:[2]

Maybe you would like to get familiar with alternatives

alternatives creates, removes, maintains and displays information about the symbolic links comprising the alternatives system. The alternatives system is a reimplementation of the Debian alternatives system.

Check out this thread where i quickly go over the basic commands to achieve what you are requesting and also have a look at alternatives manpages

Solution 3:[3]

Simplest way: just add an alias into your /home/.bashrc like:

alias python="/usr/bin/python3.5"

(I suppose CentOS have similar structure as Linux Mint)

But you probably should just use virtual env, here's a link to get you started. Solving issues like this one is the primary purpose of a virtual env.

Solution 4:[4]

If you want to set python2.7 as default Python for all the users add this line to /etc/profile.d/python_alias.sh (create the file if it does not exist):

alias python="/usr/bin/python2.7"

if you want to set python2.7 as default Python only for some users change the line above to:

case "$(whoami)" in
    <USER1>|<USER2>)
        alias python="/usr/bin/python2.7"
        ;;

Solution 5:[5]

Links python2.7 to python

sudo ln -fs /usr/bin/python2.7 /usr/bin/python

this is my example:

$ python
Python 3.5.2 (default, Sep 14 2017, 22:51:06) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

$ ls -l /usr/bin/python3.5
-rwxr-xr-x 2 root root 4456240 Sep 18  2017 /usr/bin/python3.5
$ ls -l /usr/bin/python
lrwxrwxrwx 1 root root 18 Dec  3 13:52 /usr/bin/python -> /usr/bin/python3.5
$ 
$ sudo ln -sf /usr/bin/python2.7 /usr/bin/python
$ ls -l /usr/bin/python
lrwxrwxrwx 1 root root 18 Dec  3 13:52 /usr/bin/python -> /usr/bin/python2.7
$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 OldFart
Solution 3 MatTheWhale
Solution 4 Lorenzo Garuti
Solution 5