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

  把Python3的路徑加上,然后重載bash_profile這個文件

  接着修改bashrc這個文件

vim ~/.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即可。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM