简体   繁体   English

CentOS 7 上的 Python 3 上没有模块“yum”

[英]No module 'yum' on Python 3 on CentOS 7

I'm trying to use the yum Python package in CentOS 7.我正在尝试在 CentOS 7 中使用yum Python 包

In Python 2 : I can successfully import yum .Python 2 中:我可以成功import yum

In Python 3 : When I try to import yum , I encounter ModuleNotFoundError: No module named 'yum' .Python 3 中:当我尝试import yum ,我遇到ModuleNotFoundError: No module named 'yum'

The exact same issue occurs with the dnf Python package after I install dnf.安装 dnf 后, dnf Python 包出现完全相同的问题。

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.围绕这个问题的研究把我带到了 CentOS 8,其中dnf包在 Python 3 上工作。当试图在 CentOS 7 中找到python3-dnf包(或yum的等效包)时。尝试在 CentOS 7 上安装 CentOS 8 包只导致我与所需的包发生冲突和不匹配。

Also, trying to pip3 install yum does not find any package, and pip3 install dnf succeeds but importing gives the following warning:另外,尝试pip3 install yum没有找到任何包,并且pip3 install dnf成功但导入会给出以下警告:

/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.所以,目前我找不到一种方法来与yum / dnf与 Python 代码进行本地交互,在 CentOS 7 上运行 Python 3。

My computer runs Fedora, and I have no access to a CentOS installation, but I think the information below is correct for it.我的电脑运行 Fedora,我无法访问 CentOS 安装,但我认为下面的信息是正确的。

The yum and dnf modules you're using are not your everyday pip modules.您使用的yumdnf模块不是您日常使用的pip模块。 Instead, they are actually part of the dnf and yum rpms.相反,它们实际上是dnfyum rpm 的一部分。

You can check that this way:你可以这样检查:

Python2蟒蛇2

>>> 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蟒蛇3

>>> 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如果你检查yumdnf文件,你会发现它们都是python3脚本

$ 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.如果您详细查看yum ,您会发现它实际上是对dnf模块的调用。

So, the availability of the yum or dnf module for Python will depend on which version of the actual commands you have installed.因此,用于 Python 的yumdnf模块的可用性将取决于您安装的实际命令的版本。

For dnf , you can try to install python2-dnf or python3-dnf .对于dnf ,您可以尝试安装python2-dnfpython3-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 .我想你可以,因为它们在/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对于yum ,我的猜测是他们提供了 Python 2 库以实现向后兼容性,但他们可能没有费心将其移至 Python 3,因为它已被dnf淘汰

Again, this is all true for my version of Fedora.同样,这对于我的 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 ;你的 CentOS 版本可能会得到不同的结果和包名,但底线应该是一样的:Python2 lib only for yum install different dnf packages for different Python version modules.为不同的 Python 版本模块安装不同的dnf包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM