Yum commands fail with error “No module named yum” in RHEL 7

Yum commands fail with following error “No module named yum” in Red Hat Enterprise Linux 7 (RHEL 7). It is quite troublesome. There are some solution out there but none any of them work. After several trial and error, i finally get the yum back to alive again.

[[email protected]]# yum updateThere was a problem importing one of the Python modulesrequired to run yum. The error leading to this problem was:   No module named yumPlease install a package which provides this module, orverify that the module is installed correctly.It's possible that the above module doesn't match thecurrent version of Python, which is:2.7.16 (default, Apr  2 2019, 16:59:02)[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]If you cannot solve this problem yourself, please go tothe yum faq at:  http://yum.baseurl.org/wiki/Faq

The resolutions

This issue supposedly caused by the system’s default of python version provided by Red Hat has been changed or removed. The system’s default of python version is 2.7.5. So, you need to remove any existing python version 2. You can manually remove python installation or use below script. I have made this script to remove any python installation.

#!/bin/bashprefix='/usr/local/'pyver='2.7'rm -rf -I \    ${prefix}bin/python${pyver} \    ${prefix}bin/pip${pyver} \    ${prefix}bin/pydoc \    ${prefix}bin/include/python${pyver} \    ${prefix}lib/libpython${pyver}.a \    ${prefix}lib/python${pyver} \    ${prefix}bin/python${pyver} \    ${prefix}bin/pip${pyver} \    ${prefix}bin/include/python${pyver} \    ${prefix}lib/libpython${pyver}.a \    ${prefix}lib/python${pyver} \    ${prefix}lib/pkgconfig/python-${pyver}.pc \    ${prefix}lib/libpython${pyver}m.a \    ${prefix}bin/python${pyver}m \    ${prefix}bin/2to3-${pyver} \    ${prefix}bin/python${pyver}m-config \    ${prefix}bin/python${pyver}-config \    ${prefix}bin/idle${pyver} \    ${prefix}bin/pydoc${pyver} \    ${prefix}bin/pyvenv-${pyver} \    ${prefix}share/man/man1/python${pyver}.1 \    ${prefix}include/python${pyver}m    ${prefix}bin/pydoc ## WARN: skip if other pythons in local exist.

You only need to change the value of prefix and pyvar.

Steps to resolve

List all of the python installation folder.

# ls -al `which python`lrwxrwxrwx 1 root root 7 Apr  4 10:17 /usr/bin/python -> python2#

Change the value of pyvar and prefix.

Make a shell script, i.e.: delpy.sh and use script above. Change the file mode to executable. And run the script.

# touch delpy.sh && nano delpy.sh# chmod +x delpy.sh# ./delpy.sh

Repeat above steps until you don’t have any python installation left.

Download latest python 2.7.5 from and any its dependencies: https://rpmfind.net/linux/rpm2html/search.php?query=python

Install it with this command.

rpm -Uvh --force <rpm package name>

Download latest yum from and any its dependencies: https://rpmfind.net/linux/rpm2html/search.php?query=yum

https://rpmfind.net/linux/rpm2html/search.php?query=urlgrabber

Done.

If you have any question do not hesitate to leave a comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *