Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No module 'yum' on Python 3 on CentOS 7

I'm trying to use the yum Python package in CentOS 7.

In Python 2: I can successfully import yum.

In Python 3: When I try to import yum, I encounter ModuleNotFoundError: No module named 'yum'.

The exact same issue occurs with the dnf Python package after I install dnf.

Researching around the issue took me to CentOS 8, where dnf package does work on Python 3. When trying to find python3-dnf package (or the equivalent one for yum) in CentOS 7. Trying to install CentOS 8 packages on CentOS 7 only led me to conflicts and mismatches with required packages.

Also, trying to pip3 install yum does not find any package, and pip3 install dnf succeeds but importing gives the following warning:

/usr/local/lib/python3.6/site-packages/dnf.py:15: UserWarning: The DNF Python API is not currently available via PyPI.

Please install it with your distro package manager (typically called
'python2-dnf' or 'python3-dnf'), and ensure that any virtual environments
needing the API are configured to be able to see the system site packages
directory.

  warnings.warn(warning_msg)

So, currently I can't find a way to interact natively with yum/dnf with Python code, running Python 3 on CentOS 7.

like image 968
Or B Avatar asked Jan 25 '23 12:01

Or B


1 Answers

My computer runs Fedora, and I have no access to a CentOS installation, but I think the information below is correct for it.

The yum and dnf modules you're using are not your everyday pip modules. Instead, they are actually part of the dnf and yum rpms.

You can check that this way:

Python2

>>> import yum
>>> help(yum)
(...)
FILE
    /usr/lib/python2.7/site-packages/yum/__init__.py

$ dnf -C repoquery --file /usr/lib/python2.7/site-packages/yum/__init__.py
yum-0:3.4.3-518.fc29.noarch

Python3

>>> import dnf
>>> help(dnf)
(...)
FILE
    /usr/lib/python3.7/site-packages/dnf/__init__.py

$ dnf -C repoquery --file     /usr/lib/python3.7/site-packages/dnf/__init__.py
python3-dnf-0:4.0.4-1.fc29.noarch
python3-dnf-0:4.2.5-5.fc29.noarch

If you check the yum and dnf files, you'll see that they are both python3 scripts

$ head -1 /usr/bin/yum /usr/bin/dnf
==> /usr/bin/yum <==
#!/usr/bin/python3

==> /usr/bin/dnf <==
#!/usr/bin/python3

If you look at yum in detail, you'll see that it is actually a call to the dnf module.

So, the availability of the yum or dnf module for Python will depend on which version of the actual commands you have installed.

For dnf, you can try to install python2-dnf or python3-dnf. I'm not sure that you can have them both at the same time, though. I guess you can, since they have different names in /usr/bin.

For yum, my guess is that they provide the Python 2 library for backward compatibility, but they have probably not bothered moving it to Python 3, given it is obsoleted by dnf

Again, this is all true for my version of Fedora. Your version of CentOS may get different results and package names, but the bottom line should be the same: Python2 lib only for yum; install different dnf packages for different Python version modules.

like image 72
caxcaxcoatl Avatar answered Jan 28 '23 03:01

caxcaxcoatl