4

I'm just using the free version of RHEL in my homelab, so do not have support with Red Hat. Same fix for CentOS should apply here...

I shouldn't have even tried to upgrade Python3, but hindsight is (as always) 20/20. This is what I'm getting when I try to use YUM:

[root@RHEL7 ~]# yum update
-bash: /usr/bin/yum: /usr/bin/python: bad interpreter: No such file or directory
[root@RHEL7 ~]# yum
-bash: /usr/bin/yum: /usr/bin/python: bad interpreter: No such file or directory

From reading about this error it seems I am going to have to completely reinstall Python2 and/or 3, but am wondering if there is any other fix. Both Python2 and Python3 actually still work fine (at least in the REPL):

[root@RHEL7 ~]# python3
Python 3.6.8 (default, Aug 13 2020, 07:46:32)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
[root@RHEL7 ~]# python2
Python 2.7.5 (default, Aug 13 2020, 02:51:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

This is what I get when I try to set Python2 as default (nothing returned, issue persists):

[root@RHEL7 bin]# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
[root@RHEL7 bin]#
[root@RHEL7 bin]# yum
-bash: /usr/bin/yum: /usr/bin/python: bad interpreter: No such file or directory

Oddly enough Python2 already seems to be aliased to python but when I try just running python nothing happens...? The symlink is definitely already present:

[root@RHEL7 bin]# pwd
/usr/bin
[root@RHEL7 bin]# python
-bash: python: command not found
[root@RHEL7 bin]# ln -s python2 python
ln: failed to create symbolic link ‘python’: File exists
[root@RHEL7 bin]# ls -l python
lrwxrwxrwx 1 root root 24 Jul  9 11:30 python -> /etc/alternatives/python
KidACrimson
  • 123
  • 2
  • 10

2 Answers2

8

yum can't find /usr/bin/python. It should look like this:

[root@centos7 ~]# ls -l /usr/bin/python
lrwxrwxrwx. 1 root root 7 Jul  9 11:08 /usr/bin/python -> python2
[root@centos7 ~]#

If python2 itself is still present but the symlink itself is missing, reinstate using:

[root@centos7 ~]# cd /usr/bin
[root@centos7 bin]# ln -s python2 python
[root@centos7 bin]# ls -l python
lrwxrwxrwx. 1 root root 7 Jul  9 11:10 python -> python2
[root@centos7 bin]#

Note the solution by Romeo will likely solve it, but it will leave it looking a little different from the original symlink. For the purposes of your homelab, this might not matter a jot.

[root@centos7 ~]# update-alternatives --install /usr/bin/python python /usr/bin/                                                                                                                        python2.7 1
[root@centos7 ~]# ls -l /usr/bin/python
lrwxrwxrwx. 1 root root 24 Jul  9 11:11 /usr/bin/python -> /etc/alternatives/python
[root@centos7 ~]# ls -l /etc/alternatives/python
lrwxrwxrwx. 1 root root 18 Jul  9 11:11 /etc/alternatives/python -> /usr/bin/python2.7
[root@centos7 ~]#
steve
  • 21,582
  • 5
  • 48
  • 75
  • 1
    Thanks, @Steve! Weirdly enough, I already seem to have the symlink present: `[root@RHEL7 bin]# ln -s python2 python ln: failed to create symbolic link ‘python’: File exists` – KidACrimson Jul 09 '22 at 15:27
  • 1
    This worked! I had to remove the bad symlink first: `lrwxrwxrwx 1 root root 24 Jul 9 11:30 python -> /etc/alternatives/python` – KidACrimson Jul 09 '22 at 15:37
  • 1
    Thanks a ton, @Steve !! :D – KidACrimson Jul 09 '22 at 15:37
5

Seems like you lost python "executable" which is used by yum. So what you need is to recreate it:

update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1

This will set python 2.7 (default in RHEL 7) to be preferred version.

Artem S. Tashkinov
  • 26,392
  • 4
  • 33
  • 64
Romeo Ninov
  • 16,541
  • 5
  • 32
  • 44
  • 1
    I know it’s a very popular approach, but Python binaries *aren’t* managed using alternatives in RHEL (or Fedora, or Debian and derivatives). – Stephen Kitt Jul 09 '22 at 10:59
  • Are you sure, @StephenKitt, I see a lot of pages about this: https://linuxconfig.org/how-to-change-from-default-to-alternative-python-version-on-debian-linux https://unix.stackexchange.com/questions/468620/how-to-change-python-default-version-from-2-7-to-3-7-on-rhel-7 https://stackoverflow.com/questions/45542690/how-to-set-python3-5-2-as-default-python-version-on-centos – Romeo Ninov Jul 09 '22 at 11:02
  • Yes, I’m sure; I know there are lots of pages recommending the use of alternatives for this, which is why I said it’s popular. If you look at your first link in detail, it even says that alternatives *aren’t* used by default, and explains how to set alternatives up from scratch. – Stephen Kitt Jul 09 '22 at 12:32
  • Thank you, @RomeoNinov. I tried the command but it returns nothing and the issue is still persisting: `[root@RHEL7 bin]# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 [root@RHEL7 bin]# yum -bash: /usr/bin/yum: /usr/bin/python: bad interpreter: No such file or directory` – KidACrimson Jul 09 '22 at 15:30
  • 1
    @KidACrimson, check if in `/usr/bin` you have created `python` file as softlink to 2.7 python – Romeo Ninov Jul 09 '22 at 15:35
  • Thanks, just updated the original post. I see this: `lrwxrwxrwx 1 root root 24 Jul 9 11:30 python -> /etc/alternatives/python` – KidACrimson Jul 09 '22 at 15:35
  • And `ls -l /etc/alternatives/python`? – Romeo Ninov Jul 09 '22 at 15:41