How to fix — bash: /usr/bin/python: Too many levels of symbolic links

Question:

I wanted to make python3 my default on rhel so I followed the following at How to set Python3.5.2 as default Python version on CentOS?

sudo ln -fs /usr/bin/python3 /usr/bin/python

It changed the default to 3.6.8

root@rhel:~# python -V
Python 3.6.8

Then I tried yum install python-pip:

root@rhel:~# yum install python-pip
  File "/usr/bin/yum", line 30
    except KeyboardInterrupt, e:
                        ^
SyntaxError: invalid syntax

This happened when I tried a few other commands. I tried reverting the changes by

root@rhel:~# sudo ln -fs /usr/bin/python /usr/bin/python

But am running into

root@rhel:~# python -V
bash: /usr/bin/python: Too many levels of symbolic links

I guess from what Im reading in places I need to break the sym links. The following is whats in my /usr/bin/

enter image description here

ls -l /usr/bin | grep python

lrwxrwxrwx    1 root root          15 Oct 21 14:12 python -> /usr/bin/python
lrwxrwxrwx    1 root root          14 Aug  8 05:53 python-config -> python2-config
lrwxrwxrwx    1 root root           9 Aug  8 05:51 python2 -> python2.7
lrwxrwxrwx    1 root root          16 Aug  8 05:53 python2-config -> python2.7-config
-rwxr-xr-x    1 root root        7144 Jun 11 10:34 python2.7
-rwxr-xr-x    1 root root        1835 Jun 11 10:34 python2.7-config
lrwxrwxrwx    1 root root           9 Aug  8 05:51 python3 -> python3.6
lrwxrwxrwx    1 root root          16 Aug  8 05:53 python3-config -> python3.6-config
lrwxrwxrwx    1 root root          20 Aug  8 05:53 python3-debug -> /usr/bin/python3.6dm
-rwxr-xr-x    2 root root       11336 Jun 11 15:17 python3.6
lrwxrwxrwx    1 root root          17 Aug  8 05:53 python3.6-config -> python3.6m-config
-rwxr-xr-x    1 root root       11336 Jun 11 15:17 python3.6dm
-rwxr-xr-x    1 root root         175 Jun 11 15:16 python3.6dm-config
-rwxr-xr-x    1 root root        3396 Jun 11 14:54 python3.6dm-x86_64-config
-rwxr-xr-x    2 root root       11336 Jun 11 15:17 python3.6m
-rwxr-xr-x    1 root root         173 Jun 11 15:16 python3.6m-config
-rwxr-xr-x    1 root root        3403 Jun 11 14:54 python3.6m-x86_64-config
Asked By: sectechguy

||

Answers:

this line of the result

lrwxrwxrwx    1 root root          15 Oct 21 14:12 python -> /usr/bin/python

tells us that the symbolic link python points to itself which is giving you the “Too many levels” error.

you can remove the link via

rm python

or reset the link to python3 using

ln -fs /usr/bin/python3 /usr/bin/python

(probably need sudo for both of these)

The yum install error is a separate issue, though

Answered By: LampToast

ln -fs /usr/bin/python /usr/bin/python creates a recursive symlink – i.e. it points to itself.

yum seems to be written with Python 2 syntax, so revert that symlink back:

sudo ln -fs python2 /usr/bin/python

Then find a way to make Python 3 the default which doesn’t break your package manager. E.g. an alias

See PEP 394 for the rationale of keeping Python 2 as default.

P.s. I’m not familiar with RHEL but all this matches with my experience with Ubuntu.

Answered By: wjandrea

I exit root account (crtl+D) and it works.

Answered By: joanna

I ran into a similar issue with output when running yum following resolution of symlink for /usr/bin/python. (following error received):

File "/usr/bin/yum", line 30
    except KeyboardInterrupt, e:
                        ^
SyntaxError: invalid syntax

The solution LampToast and wjandrea provided assisted in resolving my symlink issue. Although I noticed I had to link python2 vs python3.x – once I did this yum began working without errors. Also, if you happen to make use of package alternatives symlink to their location may need to be altered. I simply removed mine all together as I did not need them within my environment.

Steps in order as follows:

  1. return python packages within /usr/bin – to include symlink(s)

root@rhel:~# sudo ls -l /usr/bin | grep python

  1. remove python from /usr/bin

root@rhel:~# sudo rm python

  1. apply symlink specific to python2 to /usr/bin/python

root@rhel:~# sudo ln -fs /usr/bin/python2 /usr/bin/python

  1. verify python version – verifies issue resolution of package not being recognized

root@rhel:~# sudo python -V

  1. install yum-utils to verify additional missing packages

root@rhel:~# sudo yum -y install yum-utils.noarch yum-verify

  1. identify missing binaries – performed this check to verify no-known-issues were present. (Usually the case with the issues you are receiving)

root@rhel:~# sudo yum verify --verify-filenames='*bin/*'

  1. the output from previous command will display the missing binaries, simply reinstall using following command

root@rhel:~# sudo yum -y reinstall package_name

Note: You are able to perform step #6 over again to ensure no-known-issues are present.

I was going to recommend starting a new thread in case someone required assistance with further troubleshooting. However, I decided to modify my answer and share the knowledge I just gained through experiencing this issue myself.

Hope this is alright as this is my very first response on StackOverflow!

Cheers!

Answered By: ILiKeLaMpS
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.