No module 'yum' on Python 3 on CentOS 7
Asked Answered
R

1

5

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.

Reynoso answered 9/12, 2019 at 16:28 Comment(0)
L
9

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.

Louisalouisburg answered 12/12, 2019 at 3:35 Comment(9)
Thank you for your detailed answer, but unfortunately this doesn't solve the issue in CentOS 7.Reynoso
@OrB, I think my answer is actually "sorry, you can't". yum/dnf are installed as system packages, so you have to work with what the system offers, and apparently on CentOS it only offers Python2 interfaces. Why do you need specifically Python 3 bindings? what are you trying to accomplish? And, also, how far are you willing to hack it to get it through? You might be able to get through by crafting a virtualenv with proper requirements and adding the files from the python3-dnf package to it, for example. But do you want to go there?Louisalouisburg
I specifically want Python 3 binding since Python 2 is going to retire in 15 days. It seems that you are right saying it's simply impossible, so currently I'm going to work around it by writing my code for Python 2 :(Reynoso
@Louisalouisburg I need Python 3 because Ansible is known to be very slow on systems with large number of file pointers when used on Python 2. But I can't use python 3 with Ansible to install packages on my CentOS 7 system with Yum and not Dnf. That's quite a puzzle I got.Golf
@Gherman, why not? How are you trying to do that? Ansible has its own package modules, it shouldn't worry about Python 2 or 3 or which libs are availableLouisalouisburg
@Louisalouisburg It shouldn't? It does... I just tried to use standard Ansible yum module and it won't work with Python 3.Golf
@Gherman, your issue is a bit off-topic for this question, I think you should open it on the DevOps SE site. In any case, I think you can either use Ansible's dnf module (and some logic to decide which to use on the playbook), or, much better, use the generic package module, which should pick the right command automatically. docs.ansible.com/ansible/latest/modules/package_module.htmlLouisalouisburg
@Louisalouisburg You asked "Why do you need specifically Python 3 bindings?". I gave you an example. Generally it's not uncommon to want a specific version of a platform/runtime for various reasons. Thanks.Golf
@Gherman, ok, it makes sense. I just want to double check you tried Python 3 + Ansible + the package module. I'm not sure whether Ansible uses the libs or command calls, so it might be worth the try, as you only mention the yum module. And again, if that does not work, it would be a valid question for DevOpsLouisalouisburg

© 2022 - 2024 — McMap. All rights reserved.