8

How to change python default version from 2.7 to 3.7 on RHEL 7.

Installed python on RHEL7 virtual machine by following this https://tecadmin.net/install-python-3-7-on-centos/

When I check python version it still prints old version.

3
  • 7
    You almost certainly do not want to do this globally. It WILL break your system. Sep 12, 2018 at 19:32
  • I'd start by trying /usr/sbin/alternatives --config python Sep 12, 2018 at 20:58
  • 3
    It would not be advised to replace the base python version as it will break your RHEL system (all base software expecting Python 2.7 will cease to work, such as yum), but to instead keep the base version for the system to use and install the newer version in a way that does not affect the base version. RedHat has an option to install python3 this way using what they call Software Collections. Sep 12, 2018 at 21:20

3 Answers 3

9

RHEL 7 and its derivatives depend on Python 2 at a very deep level. If you outright replace Python 2 with 3, you'll break several of the OS's core tools.

Even if that were not the case, your question is based on an incorrect premise, being that completely replacing Python 2 with Python 3 is a good idea in the first place. Instead, install both side-by-side.

How? It starts with the fact that one should always call Python 3 as python3, since that insulates you from the major version compatibility problem. Scripts that assume Python 2 will call it as python in shebang lines and such, so there is not in fact a conflict between the old version and your newer version's python3 binary and everything that depends on it, as long as your binary Python packages are built properly.

That “if” can bite you: some packagers have created Python 3 packages with a /usr/bin/python or similar, which creates a conflict. These packages are ignoring the standard advice, which allows both to be installed in parallel.

As for getting a non-conflicting Python 3 package for RHEL 7, that’s well covered in another answer on Stack Overflow.

6

I would recommend the alternatives solution

My commands wold be.

#!/bin/bash

alternatives --list | grep -i python
alternatives --install /usr/bin/python python /usr/bin/python2.7 1
alternatives --install /usr/bin/python python /usr/bin/python3.6 2
alternatives --install /usr/bin/python python /usr/local/bin/python3.7 3
alternatives --config python
2
  • what about python-config, pydoc, pygettext, and other dependencies? Would you just install alternatives for them as well?
    – DrBeco
    Aug 31, 2019 at 15:38
  • This works great except that it broke my yum and I now need to switch python versions back in order to run yum. Is there a python3 version of yum?
    – joidegn
    Jan 28, 2021 at 16:21
3

On RHEL7 with the standard official installation (using SCL repo), the switch into python3 environment for the current shell is via "scl enable" command (note: in my case I have python 3.6):

scl enable rh-python36 "/bin/bash"

I've figured out, that if you want to set some user to have his environment hard configured for Python3, just do this (note: replace "[some_user]" by username of that user):

user=[some_user]; userhome=$(eval echo ~$user); echo ". /opt/rh/rh-python36/enable" >> ${userhome}/.bashrc

.bashrc is loaded for both, login and nonlogin shells so this should do for both, scripts ran from command line as well as scripts ran by some cron jobs etc. All it does is it sets needed environment variables to point to the Python3 instead of the system-default as primary locations, it's as simple as that.

More information on SCL you can find here - highly recommend to read to understand the concept: https://developers.redhat.com/products/softwarecollections/overview

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .