CentOS系统python默认版本由python2改为python3

CentOS中如果安装有yum,一般会有python2的某个版本。命令行键入python,出现的python2的环境:

[root@instance-hrnebyqu src]# python
Python 2.7.5 (default, Apr 11 2018, 07:36:10) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

  我们希望将python这个shell指令连接到python3的版本。这里首先装python3,然后将python连接到python3上。

  由于路径添加到了bash_profile文件中的PATH中了,因此环境变量不需要再改了。如果没有天津爱到哦环境变量则需要

  vim ~/.bash_profile
  source ~/.bash_profile

  把Python3的路径加上,然后重载bash_profile这个文件

  接着修改bashrc这个文件

vim ~/.bashrc
source ~/.bashrc

  将python2和python3 的路径都写上,并将python指定为python3

alias python2=/usr/bin/python
alias python3=/usr/local/python3/bin/python3
alias python=/usr/local/python3/bin/python3

  这样,命令行开python就是python3了。

[root@instance-hrnebyqu src]# python
Python 3.6.6 (default, Jul  4 2019, 12:00:29) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.  

  注意:

  在centos中,用于软件安装的yum指令是调用python昨晚命令解释器的,因此其默认版本为Python2,如果改成python3,会由于2和3的兼容性问题导致yum可能出现故障。因此需要特别注意。

  yum 的路径在

/usr/bin/yum

  可以看一下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:
复制代码

  可以看到,开头默认了解释器为/usr/bin/python。如果yum因为修改了python解释器出现bug,可以将这个改成/usr/bin/python2.x即可。

 

posted @ 2020-06-28 17:36  CloudNativeBJ  阅读(451)  评论(0编辑  收藏  举报