欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

No module named yum错误

程序员文章站 2022-05-29 07:53:13
...

今天在安装Docker的时候,根据官网的教程,在设置稳定存储库这一步,出现了错误:

yum-config-manager \
>     --add-repo \
>     https://download.docker.com/linux/centos/docker-ce.repo
Traceback (most recent call last):
  File "/usr/bin/yum-config-manager", line 6, in <module>
    import yum
ImportError: No module named yum

在导入yum时提示没有yum这个包,但是不可能没有啊

  • 分析
    yum是python写的,所以执行yum需要python
    查看系统安装的python,有多个版本
whereis python
python: /usr/bin/python /usr/bin/python2.6 /usr/bin/python2.6-config /usr/lib/python2.6 /usr/lib64/python2.6 /usr/local/bin/python2.7 /usr/local/bin/python /usr/local/bin/python2.7-config /usr/local/lib/python2.7 /usr/include/python2.6 /usr/share/man/man1/python.1.gz

输入python,查看使用的版本,python指向的是2.7版本

python
Python 2.7.14 (default, Jul 12 2018, 19:10:45) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

因为当前yum命令可以正常使用,查看yum文件(/usr/bin/yum),指定的是python2.6

  1 #!/usr/bin/python2.6
  2 import sys
  3 try:
  4     import yum
  5 except ImportError:
  6     print >> sys.stderr, """\
  7 There was a problem importing one of the Python modules
  8 required to run yum. The error leading to this problem was:
...

查看报错文件(/usr/bin/yum-config-manager),解释器指定的是python,因此是2.7版本,因为yum是需要python2.6版本的,所以会出问题

 #!/usr/bin/python -tt
  2 
  3 import os, os.path
  4 import sys
  5 import re
  6 import yum
  7 sys.path.insert(0,'/usr/share/yum-cli'
...

查看python的软链接,python2.6指向的是2.6解释器

 ll /usr/bin/ | grep python
-rwxr-xr-x    1 root root      20200 6月  19 2018 abrt-action-analyze-python
lrwxrwxrwx    1 root root         24 7月  12 2018 python -> /usr/local/bin/python2.7
lrwxrwxrwx    1 root root          6 7月  11 2018 python2 -> python
-rwxr-xr-x    2 root root       9032 8月  18 2016 python2.6
-rwxr-xr-x    2 root root       9032 8月  18 2016 python2.6.6
-rwxr-xr-x    1 root root       1418 8月  18 2016 python2.6-config

修改/usr/bin/yum-config-manager文件,将解释器版本指定为Python2.6

 #!/usr/bin/python2.6 -tt

再次执行设置稳定存储库的命令,执行成功

yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

转载于:https://www.jianshu.com/p/2f47c8ee037a