no module named yum 处理方法(sys.path问题处理)

导致异常的原因基本就是自己重新搞了个python,没办法,开发需要,没法避免。
现在网上提供的方法基本就2个
1重新安装对应的rpm包
2修改yum文件中的python路径(可能被指向新的而不是系统默认的)
方法很多百度就有一堆

以上两种都不行估计就是环境问题了,这也是我遇到的,可能对你有帮助。
查看当前python的搜索路径方法
命令行运行python进入交互模式

[root@localhost ~]# python
Python 2.6.6 (r266:84292, Apr  2 2022, 10:41:29) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', '/foo/python26/lib/python26.zip', '/foo/python26/lib/python2.6', '/foo/python26/lib/python2.6/plat-linux2', '/foo/python26/lib/python2.6/lib-tk', '/foo/python26/lib/python2.6/lib-old', '/foo/python26/lib/python2.6/lib-dynload', '/foo/python26/lib/python2.6/site-packages']
>>> quit
Use quit() or Ctrl-D (i.e. EOF) to exit
>>> 

明显搜索路径变成了/foo/python26/lib
正常应该是/usr/lib64/python2.6
你不管怎么重装都没有用
方法

mv /foo/python26 /foo/python26_bak

这时再看

[root@localhost ~]# python
Python 2.6.6 (r266:84292, Apr  2 2022, 10:41:29) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib/python2.6/site-packages']
>>> 

路径正确了,yum也可以正确运行了。
如果还不行,可以手动改yum脚本添加搜索路径

[root@dlocalhost ~]# vi /usr/bin/yum
#!/usr/bin/python
import sys
sys.path.append('/usr/lib64/python26.zip')
sys.path.append('/usr/lib64/python2.6')
sys.path.append('/usr/lib64/python2.6/plat-linux2')
sys.path.append('/usr/lib64/python2.6/lib-tk')
sys.path.append('/usr/lib64/python2.6/lib-old')
sys.path.append('/usr/lib64/python2.6/lib-dynload')
sys.path.append('/usr/lib64/python2.6/site-packages')
sys.path.append('/usr/lib/python2.6/site-packages')
try:
    import yum
except ImportError:
    print >> sys.stderr, """\

还有个方法可以改环境变量PYTHONPATH
不过这个效果是追加,有兴趣的可以试下

你可能感兴趣的:(运维,运维开发)