5

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?

5 Answers 5

15

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

7

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.

8
  • 3
    EPEL has add-on versions of python. I know they definitely have 3.4, maybe 3.6. Unsure about 3.7. Jun 12, 2019 at 22:54
  • Hi, When I ran "sudo rpm -ivh --oldpackage python-.rpm" I got the error, "error: File not found by glob: python-.rpm"
    – Dave
    Jun 12, 2019 at 23:57
  • @Dave did you download the RPM file from that URL? The glob is simply referring to that file...
    – filbranden
    Jun 13, 2019 at 0:12
  • 3
    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. Jun 13, 2019 at 4:32
  • 1
    Another safe approach is to use software collections for CentOS; SCL currently provides Python 3.5.1. Jun 13, 2019 at 4:35
1

After upgrading python, I edited /bin/yum and /usr/libexec/urlgrabber-ext-down and changed /usr/bin/python to /usr/bin/python2.7 and yum works perfectly. Of course, future upgrades of python will likely break this so keep a record of what you did.

1

I ran into this exact same situation, and the answer for me, was simply to use alternatives.

The previous answers assumed that Python2 is no longer installed anywhere on the person's system. With a modern version of Linux, that strikes me as very unlikely. It is more probable that there are versions of both Python2 and Python3 installed on any given server. By installing Python3, the person has simply hidden the previous installation of Python2.

So, which do you use? For stable operating system utilities, you need Python2. For development, you probably want to use Python3. That is to say, you need both! This is why they wrote alternatives.

The documentation can be very confusing. So, for RedHat/CentOS/Amazon variants here is a quick overview, "alternatives". Here is another overview "update-alternatives" that covers the Debian derivatives.

The commands that I provide below solved the 'yum' update problem, by re-setting the system default version of Python back to being Python2. For my development environment, I typically use an alias ...

alias python=/usr/bin/python3

Here are the system commands that I used to get my yum working again.

sudo alternatives --display python
sudo alternatives --remove python /usr/bin/python2.7 
sudo alternatives --install /usr/bin/python python /usr/bin/python3 10
sudo alternatives --install /usr/bin/python python /usr/bin/python2 20
alternatives --display python
sudo alternatives --auto python
sudo yum update -y

The integer values at the end of the line are the priority for that particular alternative. The 20 at the end of the python2 line, when compared with the 10 at the end of the python3 line, tell the system that it should default to using python2.

0

The cleanest solution is prolly A. Rick's alternatives thing, but the quickest one is to temporarily replace the /usr/bin/python symlink (pointing to /usr/bin/python3 or to /usr/bin/python3.6 or so; do remember it) with a symlink pointing to /usr/bin/python2 or to /usr/bin/python2.7. After this, run all your yum commands, then switch back to what you had before. If this is on a server running various processes and users, best to first switch to administrative mode.

You must log in to answer this question.

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