Why yum/subscription-manager/sosreport fail on system with "No module named..." error ?

Solution Verified - Updated -

Environment

  • Red Hat Enterprise Linux (RHEL)

Issue

  • Performing yum update results in the error No module named yum
  • While running sosreport fails with No module named os
  • When running subscription-manager commands fail with No module named version

Resolution

  • There are multiple possible causes for this issue. Usually this issue is a result of the python path being incorrectly set. The python path sys.path is dynamically built during python initialization using several methods, so depending on the system in question the fix may be one of the following:

Resolution 1
* Unset the PYTHONHOME variable:

# unset PYTHONHOME
  • For permanent change remove entry from root's .bashrc or .bash_profile if present.

  • Reinstall python package by running following command:

# rpm -Uvh --replacefiles --replacepkgs python-<version>.rpm

Resolution 2
* If there is no PYTHONHOME variable set, check to ensure there is no third-party python located underneath the improper /lib/ location instead of /lib64/.
* Check the python path as well as ldd to see which files are being loaded:

# python -c "import sys; print(sys.path)"
['', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib/python2.7/site-packages']

# ldd /usr/bin/python
    linux-vdso.so.1 =>  (0x00007ffd46b3b000)
    libpython2.7.so.1.0 => /lib64/libpython2.7.so.1.0 (0x00007efe38aaf000)  <-----take note of this file location
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007efe38893000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00007efe3868f000)
    libutil.so.1 => /lib64/libutil.so.1 (0x00007efe3848c000)
    libm.so.6 => /lib64/libm.so.6 (0x00007efe3818a000)
    libc.so.6 => /lib64/libc.so.6 (0x00007efe37dbc000)
    /lib64/ld-linux-x86-64.so.2 (0x00007efe38e7b000)

  • The above output indicates what is expected. If you see /usr/lib/python2.7 being loaded instead of /usr/lib64/python2.7 you should check the following:
# ls -l /lib/libpython2.7.so.1.0
# rpm -qf /lib/libpython2.7.so.1.0
  • If you find that there is a file at /lib/libpython2.7.so.1.0 and it is not owned by any package, you should move that file aside and see if the issue remains:
# mv /lib/libpython2.7.so.1.0 /tmp/
  • The prioritized python can also be in /root/.local, thus please ensure that there is no python under this path.

Root Cause

  • PYTHONHOME variable was set as environment variable on system.
  • Python libraries/files are modified which can be observed from output of rpm -Va command.
  • Third party python modules are installed on system which found in output of ldd /usr/bin/python command.
  • The package rpm-python* is not installed.
  • There is a third-party /lib/libpython2.7.so.1.0 that is being loaded instead of the proper system location of /lib64/libpython2.7.so.1.0. This causes the python module search path defined by sys.path to be incorrectly set, resulting in the system not finding the installed python modules

Diagnostic Steps

# sosreport 

'import site' failed; use -v for traceback 
Traceback (most recent call last): 
  File "/usr/sbin/sosreport", line 29, in ? 
    import os 
ImportError: No module named os 

# yum -d10 update
'import site' failed; use -v for traceback 
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.4.3 (#1, Jul 16 2009, 06:20:46) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] 

If you cannot solve this problem yourself, please go to 
the yum faq at: 
  http://wiki.linux.duke.edu/YumFaq  

Collect the following details from system

# rpm -qa --queryformat "%{NAME}-%{VERSION}-%{RELEASE}-%{ARCH}\n" | sort > /tmp/rpm-list
# ldd /usr/bin/python

Strace command output
# strace -fttTvyyo /tmp/strace.out -s 4096 yum update 

# which python
# env > /tmp/env.out 
# rpm -qf `which yum`
# rpm -Va > /tmp/rpm_va

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments