"yum模块未找到" centos7

3

我的操作系统是CentOS Linux 7.4.1708版本。

首先,我安装了Anaconda的Python环境。然后我替换了默认的/usr/bin/python中的Python环境。

$ ll /usr/bin/python*
lrwxrwxrwx. 1 root root  7 Aug 15 03:40 /usr/bin/python -> python2
lrwxrwxrwx. 1 root root  9 Aug  9 22:10 /usr/bin/python3 -> python3.6
lrwxrwxrwx. 1 root root 29 Aug  9 22:10 /usr/bin/python2.7 -> /root/anaconda2/bin/python2.7
lrwxrwxrwx. 1 root root 29 Aug  9 21:59 /usr/bin/python3.6 -> /root/anaconda3/bin/python3.6
lrwxrwxrwx. 1 root root  9 Aug  8 23:49 /usr/bin/python2 -> python2.7


Python 2.7.15 |Anaconda, Inc.| (default, May  1 2018, 23:32:55)
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

所以我不能再使用yum了。

$ yum
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:

   No module named yum

Please install a package which provides this module, or
verify that the module is installed correctly.

It's possible that the above module doesn't match the
current version of Python, which is:
2.7.15 |Anaconda, Inc.| (default, May  1 2018, 23:32:55)
[GCC 7.2.0]

If you cannot solve this problem yourself, please go to
the yum faq at:
  http://yum.baseurl.org/wiki/Faq

我试图将 vi /usr/bin/yum 第一行修改为其他任何Python路径,但无效。

此外,我正在尝试像这样重新安装python*.rpm:

rpm -ivh python-tools-2.7.5-68.el7.x86_64.rpm python-2.7.5-68.el7.x86_64.rpm python-libs-2.7.5-68.el7.x86_64.rpm tkinter-2.7.5-68.el7.x86_64.rpm

我尝试重新安装yum*.rpm(今天我下载了很多*.rpm...),但是还是没有解决问题。有人可以帮助我吗?谢谢!


这是相关问题的解决方案: 进入链接描述 - Bambam Deo
这是解决方案:"No module named yum” centos7 - Bambam Deo
尝试使用 --force 选项重新安装 Python 包。 - Zalatik
@Zalatik 如何使用 --force 重新安装 Python 包? - Yi Levin
安装 Python 2.7.5-68.el7.x86_64.rpm 包并强制覆盖安装。 - Zalatik
是的,它有效,耶...谢谢你,我尝试了很多方法来修复它。现在它已经完成了,但我仍然不知道理论。 - Yi Levin
3个回答

3

最近,在CentOS7上使用Yum3.4.3和Python2.7.5时,我遇到了这个问题:

[root@centos64b build]# yum list 出现了一个导入运行yum所需的Python模块之一的问题。导致该问题的错误是:

没有名为yum的模块

请安装提供此模块的软件包,或验证模块是否正确安装。

以上模块可能与当前版本的Python不匹配,当前Python版本为: 2.7.5(默认值,Apr 11 2018,07:36:10)[GCC 4.8.5 20150623(Red Hat 4.8.5-28)]

如果您无法自行解决此问题,请转到yum faq: http://yum.baseurl.org/wiki/Faq

虽然我在遇到这个问题之前没有更新Python。最终发现sys.path中未设置python site-packages libpath,因此修复的方法是在/usr/bin/yum Python脚本中将site-package libpath附加到sys.path。然后yum正常工作。

[build@centos64b ~]$ more /usr/bin/yum
#!/usr/bin/python
import sys
sys.path.append('/usr/lib/python2.7/site-packages')
sys.path.append('/usr/lib64/python2.7/site-packages')

谢谢!你让我的一天变得更美好了!我还将这些行添加到/usr/libexec/urlgrabber-ext-down中。 - Val K

0

重新安装 python

rpm -ivh --force python-2.7.5-68.el7.x86_64.rpm

为什么它有效。一般来说,RPM 能够容忍其他软件包的文件。在您的情况下,它看到没有创建链接文件,因此在安装时跳过了它们。从man rpm中我们可以找到。

--force
    Same as using --replacepkgs, --replacefiles, and --oldpackage. 
--replacefiles
    Install the packages even if they replace files from other, already installed, packages. 

使用这些选项,rpm 不会关心旧文件是由其他人创建的。

P.S. 一些提示:永远不要删除 /usr/bin 中的更改文件。将您的链接放在 /bin 中更好。更好的方法是通过向您的 .bash_profile 添加类似以下内容来将您的 bin 目录添加到 $PATH 中:

$PATH=/root/anaconda2/bin/python2.7:$PATH

所以如果出现问题,只需要从.bash_profile中删除该行即可。 再说一遍:在处理系统文件时,一定要备份。


0

我在Superuser上找到了两个解决方案StackExchange

解决方案1

  • ln -s /etc/yum.conf /etc/yum/yum.conf

解决方案2

删除新安装的Python

  • rm /usr/bin/python

将Python链接到正确的版本(x.y)

  • ln -s /usr/bin/pythonx.y /usr/bin/python

但我找不到旧版Python了,所有的 /usr/bin/python* 都是我安装的,预设的pythonx.y已经被删除了。 - Yi Levin
无论哪个版本存在,都尝试使用ln命令进行链接。另外,要修复yum,您需要编辑/etc/yum/yum.conf文件。 - Bambam Deo
如何链接?请帮帮我, 我的python2.6是/usr/local/bin/python 我的python2.7是/usr/bin/python 但默认的python是python2.6。 而且,我不知道如何修复yum 内容在这里。[main] cachedir=/var/cache/yum/$basearch/$releasever keepcache=0 debuglevel=2 logfile=/var/log/yum.log exactarch=1 obsoletes=1 gpgcheck=1 plugins=1 installonly_limit=5 bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum distroverpkg=centos-release - Yi Levin
你能分享一下以下命令的输出结果吗?ls -l /usr/bin pyth*ls -l /usr/local/bin pyth* - Bambam Deo
很可能,以下两个命令可以解决你的问题:rm /usr/bin/python; ln -s /usr/local/bin/python /usr/bin/python - Bambam Deo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接