我把系统的Python版本从3.9.9升级到3.11.4后系统的dnf命令和yum命令都不能用了

我把系统的Python版本从3.9.9升级到3.11.4后系统的dnf命令和yum命令都不能用了,一用就提示如下:
Traceback (most recent call last):
File “/usr/bin/dnf”, line 61, in
from dnf.cli import main
ModuleNotFoundError: No module named ‘dnf’
因为系统安装了其他应用服务,不能重装系统,该怎么解决呢?

试试
PYTHONPATH=/usr/lib/python3.9/site-packages:$PYTHONPATH sudo dnf …

感谢~!
我刚试了一下,不得行,还是报同样的错,

dnf命令 (/usr/bin/dnf) 是一个可执行的python脚本,它执行中会调用 “dnf” 这个python模块。现在你碰到的问题是你的python命令(python 3.11)找不到这个模块。这个模块是由python3-dnf这个包提供的。不知道你是如何“把系统的python3.9升级到python3.11”的,是不是把python3-dnf这个包卸载了。这个包把“dnf”这个python模块装在了/usr/lib/python3.xx/site-packages/dnf。你可以看看它还在不在。

谢谢大神~! 刚看了一下,确实没有dnf这个模块了,我是通过命令升级的,我安装WEB服务其实不升级也可以,就是手贱输了个升级命令,然后就啥都干不了了,我看了一下之前的python版本还在,之前的Python版本的模块包里面有dnf这个模块,我可以把默认使用的Python版本设置成之前的版本吗?或者安装一下这个dnf模块,但是我怕还会有其他模块没有安装,最好是看看能不能使用之前的python版本,

[root@localhost ~]# ls /usr/lib/python3.xx/site-packages/
ls: 无法访问 ‘/usr/lib/python3.xx/site-packages/’: No such file or directory
[root@localhost ~]# ls /usr/lib/
binfmt.d firewalld kbd modules-load.d python3.10 sysusers.d
cpp firmware kdump motd python3.11 tmpfiles.d
debug fontconfig kernel motd.d rpm tuned
dkms games locale NetworkManager sysctl.d udev
dracut gcc modprobe.d pam.d sysimage
environment.d grub modules polkit-1 systemd
[root@localhost ~]# ls /usr/lib/python3.11
site-packages
[root@localhost ~]# ls /usr/lib/python3.11/site-packages
configobj-5.0.6-py3.11.egg-info python_dateutil-2.8.2-py3.11.egg-info
configobj.py python_linux_procfs-0.7.0-py3.11.egg-info
dateutil pyudev
decorator-5.1.1-py3.11.egg-info pyudev-0.24.1-py3.11.egg-info
decorator.py setuptools
distro setuptools-68.0.0.dist-info
distro-1.8.0.dist-info six-1.16.0.dist-info
_distutils_hack six.py
distutils-precedence.pth sssd
nftables SSSDConfig
nftables-0.1-py3.11.egg-info SSSDConfig-2.9.1-py3.11.egg-info
pkg_resources validate.py
procfs _version.py
pycache
[root@localhost ~]# ls /usr/lib/python3.10/site-packages
dnf gi pygtkcompat tuned
dnf-plugins lvmdbusd pyparsing vdo
dnfpluginscore pip pyparsing-3.0.7.dist-info
firewall pip-22.3.1.dist-info pyudev
[root@localhost ~]#

如果你的旧版本是 python3.10 的话,可以先执行 ls -l $(which python3.10),查看一下 python3.10 的路径是什么,然后修改 /usr/bin/dnf 文件,将第一行的 /usr/bin/python3 替换成查到的路径,再试试

1 Like

所以你原来的python版本是3.10,不是3.9.9。/usr/lib/python3.10/site-packages/dnf还在。所以你可以试试:
$PYTHONPATH=/usr/lib/python3.10/site-packages:$PYTHONPATH dnf …
成不成功,就看dnf这个模块是不是兼容python3.11。如果不行,或者还可以试试
/usr/bin/python3.10 /usr/bin/dnf …

我更改了以后dnf命令可以用了,不会报错
[root@localhost ~]# dnf
usage: dnf [options] COMMAND

List of Main Commands:

alias List or create command aliases
autoremove remove all unneeded packages that were originally installed as dependencies
check check for problems in the packagedb
check-update check for available package upgrades
clean remove cached data
deplist [deprecated, use repoquery --deplist] List package’s d

但是我用pip安装python模块各种安装不上,一直是如下的提示:
[root@localhost ~]# pip3 install Flask
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by ‘ReadTimeoutError(“HTTPSConnectionPool(host=‘pypi.org’, port=443): Read timed out. (read timeout=15)”)’: /simple/flask/
ERROR: Could not find a version that satisfies the requirement PyPDF2 (from versions: none)
ERROR: No matching distribution found for PyPDF2

[root@localhost ~]# pip install PyPDF2
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by ‘ReadTimeoutError(“HTTPSConnectionPool(host=‘pypi.org’, port=443): Read timed out. (read timeout=15)”)’: /simple/pypdf2/
ERROR: Could not find a version that satisfies the requirement PyPDF2 (from versions: none)
ERROR: No matching distribution found for PyPDF2

尝试更新pip 提示如下
[root@localhost ~]# python3 -m pip install --upgrade pip
Requirement already satisfied: pip in /usr/lib/python3.10/site-packages (22.3.1)
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by ‘ReadTimeoutError(“HTTPSConnectionPool(host=‘pypi.org’, port=443): Read timed out. (read timeout=15)”)’: /simple/pip/
WARNING: Running pip as the ‘root’ user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: 12. Virtual Environments and Packages — Python 3.13.0 documentation

  1. ‘ReadTimeoutError(“HTTPSConnectionPool(host=‘pypi.org’, port=443): Read timed out.
    —> pypi.org是经常连不上的,很正常。多试几次。
    2)WARNING: Running pip as the ‘root’ user can result in broken permissions and conflicting behaviour with the system package manager
    —> 这个警告要重视。最好不要以root用户身份登录。