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

60,929

Solution 1

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.

Solution 2

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

Solution 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

Share:
60,929

Related videos on Youtube

itgeek
Author by

itgeek

Updated on September 18, 2022

Comments

  • itgeek
    itgeek over 1 year

    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.

    • Austin Hemmelgarn
      Austin Hemmelgarn over 5 years
      You almost certainly do not want to do this globally. It WILL break your system.
    • Cupcake Protocol
      Cupcake Protocol over 5 years
      I'd start by trying /usr/sbin/alternatives --config python
    • GracefulRestart
      GracefulRestart over 5 years
      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.
  • Russian Junior Ruby Developer
    Russian Junior Ruby Developer over 3 years
    will scl method survive reboot?