4

Since our application works with python2, we needed to move to RHEL 8

After we installed python2 on RHEL 8 machine, we see the following:

rpm -qa | grep python2
python2-pip-9.0.3-19.module+el8.6.0+13001+ad200bd9.noarch
python2-setuptools-wheel-39.0.1-13.module+el8.4.0+9442+27d0e81c.noarch
python2-pip-wheel-9.0.3-19.module+el8.6.0+13001+ad200bd9.noarch
python2-2.7.18-11.module+el8.7.0+15681+7a92afba.x86_64
python2-libs-2.7.18-11.module+el8.7.0+15681+7a92afba.x86_64
python2-setuptools-39.0.1-13.module+el8.4.0+9442+27d0e81c.noarch

but when we try to use import yum, we then get an error about 'No module named yum' with the following output:

python
Python 2.7.18 (default, Jun 17 2022, 07:56:00)
[GCC 8.5.0 20210514 (Red Hat 8.5.0-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import yum
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named yum

The installed rpm for yum are:

rpm -qa | grep yum
yum-4.4.2-11.el8.noarch
yum-utils-4.0.18-4.el8.noarch

and yum appears as:

more /usr/bin/yum
#!/usr/bin/python
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)

and from pip2 list we get this output:

pip2 list
pip (9.0.3)
setuptools (39.0.1)

So why are we getting the error about No module named yum?

https://www.getpagespeed.com/solutions/python-scripts-running-on-rocky-linux-8-can-not-import-yum

https://access.redhat.com/solutions/4289441

1 Answer 1

3

After looking at a StackO question a simple fix might be making a change in /usr/bin/yum. Change the first line !#/usr/bin/python to !#/usr/bin/python2.7

There is also a more recent, but more complex RHEL customer portal solution from April 25, 2023.

Resolutions

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(s)

  • 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
2
  • in /usr/bin/yum , we have the first line as #!/usr/libexec/platform-python , so its not as "!#/usr/bin/python" , what we can do about this?
    – King David
    Commented Jul 21, 2023 at 7:18
  • @KingDavid darn, go figure it’s slightly different for the presumable easy fix. You could try updating it to what I posted. You could also try tacking on 2.7 it python-platform, but I don’t know if that is valid or not. Did you attempt the other more involved resolutions? I’ll see if I can find anything regarding /usr/libexec/python-platform
    – DrZoo
    Commented Jul 21, 2023 at 13:36

You must log in to answer this question.

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