zoukankan      html  css  js  c++  java
  • 将CentOS系统python默认版本由python2改为python3

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

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

    [root@cp01-jiazhuang01 bin]# python
    Python 2.7.5 (default, Sep 23 2013, 17:17:55)
    [GCC 3.4.5 20051201 (Red Hat 3.4.5-2)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    

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

    首先,下载anaconda:https://repo.anaconda.com/archive/Anaconda3-2019.03-Linux-x86_64.sh

    解压后利用bash命令安装。

    在anaconda的路径中可以找到python3。

    /root/anaconda3/bin/python3
    

    由于anaconda3已经将路径添加到了bash_profile文件中的PATH中了,因此环境变量不需要再改了。如果是直接装的python3而没有用anaconda,那么需要在:

    vim ~/.bash_profile
    

    中把python3的路径加上。

    下面修改bashrc

    vim ~/.bashrc
    

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

    alias python2="/home/opt/python2.7.5/bin/python2.7"
    alias python3="/root/anaconda3/bin/python3.7"
    alias python=python3
    

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

    [root@cp01-jiazhuang01 bin]# python
    Python 3.7.3 (default, Mar 27 2019, 22:11:17)
    [GCC 7.3.0] :: Anaconda, Inc. on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    

    注意:

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

    yum 的路径在

    /usr/bin
    

    可以看一下yum文件

    vim /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, "
    
    Exiting on user cancel."
        sys.exit(1)
    

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

    2019-07-23 10:53:38

  • 相关阅读:
    silverlight 网站 iis发布
    使用PerfView诊断.Net GC的问题
    七种方法实现单例模式
    java多线程的学习之路(二)
    java多线程的学习之路(一)
    几种特殊的二叉树
    解析json字符串,把json字符串转换成Java对象
    KMP算法的理解
    常用 Git 命令清单
    Jquery.cookie.js 源码和使用方法
  • 原文地址:https://www.cnblogs.com/morikokyuro/p/13256680.html
Copyright © 2011-2022 走看看