Upgraded Python, and now I can't run "yum upgrade"

12,083

Solution 1

To fix this problem, you should reinstall Python 2.7 shipped with CentOS 7.

(See below as for why you should reinstall, and how you should manage newer versions of software in a distro such as CentOS.)

Reinstalling older Python from RPM

Since yum is not working, you'll have to do most of it manually, by downloading the packages and reinstalling them using rpm directly.

For example, you can find a Python 2.7 RPM here (for CentOS 7 on x86_64):

http://mirror.centos.org/centos/7/os/x86_64/Packages/python-2.7.5-76.el7.x86_64.rpm

You can fetch it using wget or curl.

Then, change directory to the one where you downloaded the package to, and install it using:

$ sudo rpm -Fvh --oldpackage python-*.rpm

(The rpm -F command, -F for "freshen", will update packages that were already installed. Using --oldpackage will allow you to reinstall the same version, or even install an older version, which is possible since you might have gotten package updates that are more recent than version you downloaded.)

It is possible that this is not the only package you'll need, I suggest you look at all the packages starting with python-*, since many subpackages (python-libs is one obvious case, but there might be others) might have been overwritten as well. You can download them from the same location (Warning: long directory listing here, it might take a while to take the whole list.)

You might need additional options such as --force and --nodeps, but the idea is that you manage to reinstall the package and overwrite the Python package back to the 2.7 (even if a slightly older versions, with not all the updates) from CetnOS.

To list all packages matching python-*, you can use:

$ rpm -qa 'python-*'

You can also verify whether files from a specific package have been overwritten with:

$ rpm -V python-libs

This will list files in the package which have had at least one attribute modified. The attribute listed as 5 is the MD5 checksum of the file, that means the contents of that file have been altered.

Once you have enough packages restored, try to run yum again, it might end up upgrading packages again.

Why reinstall Python 2.7 if I don't care about it?

An operating system such as CentOS and its upstream RHEL (Red Hat Enterprise Linux) is built for stability and reliability. Sure, that means sometimes package versions will be old, but it means everything is tested to work together.

As you've seen, Python is pretty central to CentOS/RHEL and many other packages depend on it. Well, even the package management software does. So overwriting the Python version will surely break the OS as you've seen it.

If you want a distribution that ships with latest Python, then CentOS/RHEL is not what you want. Try Fedora (closest to CentOS/RHEL but released every 6 months), Arch Linux (rolling releases) or one of the others that have releases often (every 6 months, typically) or are rolling release distros (newer packages rolling in all the time.)

How to get Python 3.7 on CentOS 7?

But if you really want/need CentOS (and there are many good reasons for it) and you want Python 3.7 (or a more recent version of about any other piece of software), look into installing it in a way that does not interfere with the version shipped by the OS, leaving that version intact.

In the specific example of Python 3.7, install it under /usr/local, /opt/python-3.7 or a similar directory. Set your user's $PATH to pick that version of Python before the system's one. Make sure you don't tweak the /usr/bin/python symlink, instead use a python3 one for the newer version.

The same advice can be used for most other software you would like to install in parallel with the system deployed version in a CentOS or similar Linux distribution.

Solution 2

NOTE: In case someone still needs it.

Not MINE link at the end

If this is what you see on yum install <package-name>

(base) [root@localhost rstudio]# yum install shiny-server-1.5.9.923-x86_64.rpm 
  File "/usr/bin/yum", line 30
    except KeyboardInterrupt, e:
                            ^
SyntaxError: invalid syntax

Cause Analysis Because yum supports python2 by default, when you upgrade to python3, you get an error. If you can enter python2 by building python2

(base) [root@localhost rstudio]# python2
Python 2.7.5 (default, Jul 13 2018, 13:06:57) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

Then you can modify the yum code python to python to implement.

lets solve it....

vi /usr/bin/yum

Change #!/usr/bin/python on the first line to #!/usr/bin/python2.

#!/usr/bin/python2
import sys
try:
    import yum
except ImportError:
    print >> sys.stderr, """\
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:

problem solved!!

postscript

Found that yum no matter what software is installed, is an error, the type is as follows:

base) [root@localhost ~]# yum install yum-fastestmirror
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.huaweicloud.com
 * updates: mirror.jdcloud.com
  File "/usr/libexec/urlgrabber-ext-down", line 28
    except OSError, e:
                  ^
SyntaxError: invalid syntax
  File "/usr/libexec/urlgrabber-ext-down", line 28
    except OSError, e:
                  ^
SyntaxError: invalid syntax
  File "/usr/libexec/urlgrabber-ext-down", line 28
    except OSError, e:
                  ^
SyntaxError: invalid syntax
  File "/usr/libexec/urlgrabber-ext-down", line 28
    except OSError, e:
                  ^
SyntaxError: invalid syntax

Solution

1, enter the edit urlgrabber-ext-down
2, change python to python2
#vi /usr/libexec/urlgrabber-ext-down

#!/usr/bin/python >--Replace with -->#!/usr/bin/python2

P.S. Copied, almost to the word, from Solution

Share:
12,083

Related videos on Youtube

Dave
Author by

Dave

Updated on September 18, 2022

Comments

  • Dave
    Dave over 1 year

    I have upgraded my system (CentOS 7) to Python 3.7 and it seems that has broken a ton of things. In particular, I can't perform a yum upgrade ...

    [myuser@server ~]$ sudo yum upgrade
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * base: mirror.us-midwest-1.nexcess.net
     * epel: mirror.layeronline.com
     * extras: mirror.us-midwest-1.nexcess.net
     * updates: mirror.us-midwest-1.nexcess.net
      File "/usr/libexec/urlgrabber-ext-down", line 28
        except OSError, e:
                      ^
    SyntaxError: invalid syntax
      File "/usr/libexec/urlgrabber-ext-down", line 28
        except OSError, e:
                      ^
    SyntaxError: invalid syntax
      File "/usr/libexec/urlgrabber-ext-down", line 28
        except OSError, e:
                      ^
    SyntaxError: invalid syntax
      File "/usr/libexec/urlgrabber-ext-down", line 28
        except OSError, e:
                      ^
    SyntaxError: invalid syntax
      File "/usr/libexec/urlgrabber-ext-down", line 28
        except OSError, e:
                      ^
    SyntaxError: invalid syntax
    
    
    Exiting on user cancel
    

    Is there any way I can heal the pain here?

  • Aaron D. Marasco
    Aaron D. Marasco almost 5 years
    EPEL has add-on versions of python. I know they definitely have 3.4, maybe 3.6. Unsure about 3.7.
  • Dave
    Dave almost 5 years
    Hi, When I ran "sudo rpm -ivh --oldpackage python-.rpm" I got the error, "error: File not found by glob: python-.rpm"
  • filbranden
    filbranden almost 5 years
    @Dave did you download the RPM file from that URL? The glob is simply referring to that file...
  • filbranden
    filbranden almost 5 years
    Also, I think you might need rpm -Uvh instead (-U for updating an existing package...) I'll edit my answer to say that instead.
  • filbranden
    filbranden almost 5 years
    @Dave I updated the answer, please take a longer look.
  • filbranden
    filbranden almost 5 years
    @AaronD.Marasco Do the EPEL packages install to an alternative location, such that they can co-exist with the Python 2.7 shipped as part of CentOS 7?
  • Stephen Kitt
    Stephen Kitt almost 5 years
    The whole point of EPEL is to provide newer packages that can be safely installed on RHEL and CentOS, and the Python 3 packages are no exception; they install alongside the Python 2 packages without breaking them. EPEL provides Python 3.6.8 currently.
  • Stephen Kitt
    Stephen Kitt almost 5 years
    Another safe approach is to use software collections for CentOS; SCL currently provides Python 3.5.1.