圈小蛙

升级Python以后,yum无法使用,提示ModuleNotFoundError: No module named ‘dnf’

编译安装升级Python

yum -y install libffi-devel tk-devel sqlite-devel
wget https://www.python.org/ftp/python/3.9.19/Python-3.9.19.tar.xz
tar -Jxvf Python-3.9.19.tar.xz
cd Python-3.9.19
./configure --prefix=/usr/local/python3 --with-openssl=/usr/local/openssl --enable-shared
make && make install
cp /usr/local/python3/lib/libpython3.9.so.1.0 /usr/lib64/
echo "/usr/local/python3/lib" >/etc/ld.so.conf.d/python3.conf
ldconfig
ln -s /usr/local/python3/bin/python3.9 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
pip3 install --upgrade pip

CentOS系统升级Python 3.9,编译安装完成后,yum、dnf命令无法使用,执行均报错,提示:

Traceback (most recent call last):
File "/usr/bin/yum", line 57, in
from dnf.cli import main
ModuleNotFoundError: No module named 'dnf'

猜测原因是,新安装的Python环境中没有依赖包。

尝试stackoverflow的解决方法

rpm -qa python3 #查看python3 rpm package

返回:python3-3.9.18-1.el9_3.1.x86_64

下载:python3-3.9.18-1.el9_3.1.x86_64.rpm(https://oraclelinux.pkgs.org/9/ol9-baseos-latest-x86_64/python3-3.9.18-1.el9_3.1.x86_64.rpm.html

在下载目录执行:

rpm -e --nodeps python3-3.9.18-1.el9_3.1.x86_64
rpm -i python3-3.9.18-1.el9_3.1.x86_64.rpm

没有作用,报错依旧。实在不行,检查当前Python环境中的依赖包目录有没有dnf,执行:

cd /usr/local/python3/lib/python3.9/site-packages/
ll dnf*

发现确实没有,在/usr目录查找dnf:

find /usr -name dnf

在这个目录:/usr/lib/python3.9/site-packages/dnf,复制到环境目录:

cp -r /usr/lib/python3.9/site-packages/dnf /usr/local/python3/lib/python3.9/site-packages/

果然解决,但提示缺别的依赖,相同的办法,比如libdnf:

cp -r /usr/lib64/python3.9/site-packages/libdnf /usr/local/python3/lib/python3.9/site-packages/

还缺好多依赖,比如gpg(提示ModuleNotFoundError: No module named 'gpgme'),把依赖补齐,下面是全部的依赖:

cp -r /usr/lib/python3.9/site-packages/dnf /usr/local/python3/lib/python3.9/site-packages/
cp -r /usr/lib64/python3.9/site-packages/libdnf /usr/local/python3/lib/python3.9/site-packages/
cp -r /usr/lib64/python3.9/site-packages/rpm /usr/local/python3/lib/python3.9/site-packages/
cp -r /usr/lib64/python3.9/site-packages/hawkey /usr/local/python3/lib/python3.9/site-packages/
cp -r /usr/lib64/python3.9/site-packages/libcomps /usr/local/python3/lib/python3.9/site-packages/
cp -r /usr/lib64/python3.9/site-packages/gpg /usr/local/python3/lib/python3.9/site-packages/
cp -r /usr/lib/python3.9/site-packages/dnfpluginscore /usr/local/python3/lib/python3.9/site-packages/
cp -r /usr/lib/python3.9/site-packages/dateutil /usr/local/python3/lib/python3.9/site-packages/
cp -r /usr/lib64/python3.9/site-packages/systemd /usr/local/python3/lib/python3.9/site-packages/
cp -r /usr/lib/python3.9/site-packages/dnf-plugins /usr/local/python3/lib/python3.9/site-packages/

至此,yum、dnf命令均恢复正常,也可以将两个目录都复制过去:

\cp -r /usr/lib/python3.9/site-packages/* /usr/local/python3/lib/python3.9/site-packages/
\cp -r /usr/lib64/python3.9/site-packages/* /usr/local/python3/lib/python3.9/site-packages/

不过提示:

Failed loading plugin "generate_completion_cache": No module named '_sqlite3'

执行:

yum -y install sqlite-devel

重新编译python,问题全部解决,成功恢复yum。

Exit mobile version