SoFunction
Updated on 2025-03-10

Solutions to upgrade python causes Yum to crash

Problem description

In useyumWhen commanding, the following error occurs:

There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:

   No module named yum

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:
2.7.18 (default, Nov 11 2024, 12:55:22)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]

Cause of the problem

This problem is usually caused by one of several reasons:

  • Python version changesyumDepend on the Python version, while the default Python on the system may be changed to Python 3, or Python 2 is upgraded or replaced, resulting inyumThe required module cannot be found.

  • Missing dependency moduleyumThe Python modules that depend on (e.g.urlgrabber) may have been deleted by mistake, or these modules cannot be found due to environment configuration errors.

  • Symbol link error: In CentOS,yumUsed by default/usr/bin/pythonTo call Python. If the symbolic link points to the wrong version of Python (such as Python 3), it will causeyumNot working properly.

Solution

Solution 1: Fix Python Symbol Links

  • Check the current Python version symbolic link:

ls -l /usr/bin/python
  • if/usr/bin/pythonPointing to other versions (such as Python 3), you need to switch it back to Python 2.7.

  • Will/usr/bin/pythonPoint to the system's default Python 2.7:

Reasons and prerequisites for effective entry

  • yumRely on the Python version, so you need to ensure/usr/bin/pythonSymbol links point to Python 2.7. If the symbolic link points to Python 3,yumThe corresponding module will not be found.
  • The premise of this solution is that there is still Python 2.7 installed in the system and it has not been deleted. If Python 2.7 is deleted, you need to reinstall it first.

Scenario 2: Reinstall the Python module

  • InstallyumDepend on Python modules:

yum install python2 python-urlgrabber
  • This command reinstalls Python 2 and its required modules to ensureyumCan run normally.

  • ifyumIt cannot be used by itself, you can try to install it manually:

rpm -Uvh --replacepkgs /centos/7/os/x86_64/Packages/python-urlgrabber-<version>.rpm
  • Will<version>Replace with the appropriate version number.

Solution 3: Usednfreplaceyum

dnfyesyumThe subsequent version of   has better dependency management and performance optimization functions. ifyumCan't be repaired, you can consider installing and usingdnf

  • Installdnf

rpm -Uvh /centos/7/os/x86_64/Packages/dnf-<version>.rpm
  • Will<version>Replace with the appropriate version number.

  • usednfPerform package management operations:

dnf update

Summarize

The root cause of this problem is usually the system's Python environment changes, resulting inyumThe dependent module cannot be loaded correctly. The most common solution is to fix Python's symlinks to point to Python 2.7, or reinstall the necessary Python modules. For situations that cannot be repaired, you can also consider using itdnfAs an alternative.

This is the end of this article about the solution to the Yum crash by upgrading python. For more related content on upgrading python, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!