下载centos7 with python3 docker镜像无法使用yum源的原因以及solution

本文分析了在CentOS 7 with Python 3 Docker镜像中无法使用yum源的问题,并提供了详细的解决方案。主要原因是镜像默认Python版本为Python 3,而yum使用的却是Python 2。通过调整解释器路径指向正确的Python版本即可解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下载centos7 with python3 docker镜像无法使用yum源的原因以及solution

可能大多数人都不知道yum的可执行程序使用什么语言写的,请看:

[lockey@7-o-1 ~]# cat /usr/bin/yum
#!/usr/bin/python
import sys
try:
    import yum
except ImportError:
    print >> sys.stderr, """\
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:

   %s

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:
%s

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

""" % (sys.exc_value, sys.version)
    sys.exit(1)

sys.path.insert(0, '/usr/share/yum-cli')
try:
    import yummain
    yummain.user_main(sys.argv[1:], exit_code=True)
except KeyboardInterrupt, e:
    print >> sys.stderr, "\n\nExiting on user cancel."
    sys.exit(1)

很显然,从解释器我们就可以看出使用python写的,看了code部分更是确信无疑,那么错误出在了哪里呢?

分析上边的代码是python2写的,但是镜像的默认python版本是python3,原因很清楚,解释器调用错误,我们只需要将解释器指向正确的位置即可
# whereis python
python: /usr/bin/python /usr/bin/python3.6m /usr/bin/python3.6 /usr/bin/python3.6m-config /usr/bin/python3.6-config /usr/bin/python3.6m-x86_64-config /usr/bin/python2.7 /usr/lib/python3.6 /usr/lib/python2.7 /usr/lib64/python3.6 /usr/lib64/python2.7 /etc/python /usr/include/python3.6m /usr/include/python2.7

将上边解释器改成:

#!/usr/bin/python2.7

这一步过了之后,运行可能还会报错因为要运行的python程序不止这一个地方,所以对于出错的地方要逐个修改解释器路径

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值