Wednesday 1 February 2017

Yum with Python 3 : except KeyboardInterrupt, e: SyntaxError: invalid syntax

If your machine has Python 3 installed (or upgraded to Python3), then running yum will fail with below error 

[sbetha@host:pwd]$ yum
  File "/usr/bin/yum", line 30
    except KeyboardInterrupt, e:
                            ^
SyntaxError: invalid syntax

 

This is because, yum doesnt support Python 3 at the moment.
Check which all versions of Python are installed on your machine
$ ls -lrt  /usr/bin/python*
-rwxr-xr-x. 1 root root 1418 Jan 22  2014 /usr/bin/python2.6-config
-rwxr-xr-x  2 root root 9032 Jan 22  2014 /usr/bin/python266
-rwxr-xr-x  2 root root 9032 Jan 22  2014 /usr/bin/python2.6
lrwxrwxrwx. 1 root root    6 Apr 22  2015 /usr/bin/python2 -> python
lrwxrwxrwx. 1 root root   16 Apr 22  2015 /usr/bin/python-config -> python2.6-config
lrwxrwxrwx  1 root root   24 Aug 26 00:18 /usr/bin/python2.7 -> /root/py27/bin/python2.7
lrwxrwxrwx  1 root root   40 Jan 31 22:59 /usr/bin/python -> /scratch/sbetha/python_3.4.6/bin/python3
 

Edit /usr/bin/yum to include on line1 as 
$ cat /usr/bin/yum
#!/usr/bin/python2.6

 
Now yum will work as normal since it uses Python 2.6
$ yum --version
3.2.29
  Installed: rpm-4.8.0-37.el6.x86_64 at 2015-04-22 15:54
  Built    : None at 2013-10-14 13:39
  Committed: Panu Matilainen <pmatilai@redhat.com> at 2013-09-12

  Installed: yum-3.2.29-60.0.1.el6.noarch at 2015-04-22 15:54
  Built    : None at 2014-08-25 14:22
  Committed: Jacob Wang <jacob.wang@oracle.com> at 2014-08-21

  Installed: yum-plugin-aliases-1.1.30-30.0.1.el6.noarch at 2015-04-22 16:11
  Built    : None at 2014-10-15 23:40
  Committed: Curt Carter <curt.carter@oracle.com> at 2014-10-14

  Installed: yum-plugin-fastestmirror-1.1.30-30.0.1.el6.noarch at 2015-04-22 18:24
  Built    : None at 2014-10-15 23:40
  Committed: Curt Carter <curt.carter@oracle.com> at 2014-10-14

 

2 comments:

  1. > Just Update #!/usr/bin/python to #!/usr/bin/python2.7

    #!/usr/bin/python2.7
    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:

    %s

    Please install a package which provides this module, or
    verify that the module is installed correctly.

    It's possible that the above module doesn't match the
    current version of Python, which is:
    %s

    If you cannot solve this problem yourself, please go to
    the yum faq at:
    http://yum.baseurl.org/wiki/Faq

    """ % (sys.exc_value, sys.version)
    sys.exit(1)

    sys.path.insert(0, '/usr/share/yum-cli')
    try:
    import yummain
    yummain.user_main(sys.argv[1:], exit_code=True)
    except KeyboardInterrupt, e:
    print >> sys.stderr, "\n\nExiting on user cancel."
    sys.exit(1)

    ReplyDelete

Linux : Create a new user for the machine

Creating a new user in linux is sometimes needed, when you want to share the user access with anyone. SSH to the machine over the newly cre...