首頁 > 軟體

CentOS7保留預設Python版本並安裝更新Python2和Python3共存

2020-06-16 16:58:14

CentOS 7 預設的Python版本是python2.7.5。因為yum依賴於預設的python版本的緣由,所以要先保留預設版本,並修改yum檔案頭部後,才能開始安裝更新python2和python3兩個最新版本。(python2和python3共存)

一. 保留預設python版本2.7.5,並修改yum頭部,保持yum對預設版本python2.7.5的依賴

1.su root  # 獲取root許可權

2.mv /usr/bin/python2.7 /usr/bin/python2.7.5  # 保留預設版本python為python2.7.5

 ln -s /usr/bin/python2.7.5 /usr/local/bin/python2.7.5  # 建立軟連線


3.ll /usr/bin/yum*  # 檢視/usr/bin/目錄下所有yum檔案(7個)頭部

4.vi /usr/bin/yum*  # 修改/usr/bin/目錄下所有yum檔案(7個)頭部

  #!/usr/bin/python —> #!/usr/bin/python2.7.5  

5.vi /usr/libexec/urlgrabber-ext-down  # 修改/usr/libexec/目錄下 urlgrabber-ext-down頭部

  #!/usr/bin/python —> #!/usr/bin/python2.7.5

二. 安裝最新版python2.7.13和python3.6.2(python2和python3共存,修改後預設版本為python3.6.2)

準備工作:

1. 安裝wget命令(可以線上下載安裝包的命令)

  yum -y install wget

2. 準備編譯環境

  yum groupinstall 'Development Tools'
  yum install zlib-devel bzip2-devel openssl-devel ncurses-devel
3. 下載並安裝最新版本的python2和python3的tgz壓縮檔案

  https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz
  https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz

開始安裝:

1. 進入下載目錄:

  cd /usr/local/src

2. 下載並安裝最新版本python2:
  wget https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz
  tar zxvf Python-2.7.13.tgz
  cd Python-2.7.13
  ./configure
  make all
  make install
  make clean
  make distclean
  rm -rf /usr/bin/python
  rm -rf /usr/bin/python2
  rm -rf /usr/bin/python2.7
  ln -s /usr/local/bin/python2.7 /usr/bin/python
  ln -s /usr/local/bin/python2.7 /usr/bin/python2
  ln -s /usr/local/bin/python2.7 /usr/bin/python2.7
  /usr/bin/python -V
  /usr/bin/python2 -V
  /usr/bin/python2.7 -V
  rm -rf /usr/local/bin/python
  rm -rf /usr/local/bin/python2
  ln -s /usr/local/bin/python2.7 /usr/local/bin/python
  ln -s /usr/local/bin/python2.7 /usr/local/bin/python2
  python -V
  python2 -V
  python2.7 -V

3. 下載並安裝最新版本python3:
  wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz
  tar zxvf Python-3.6.2.tgz
  cd Python-3.6.2
  ./configure
  make all
  make install
  make clean
  make distclean
  rm -rf /usr/bin/python
  rm -rf /usr/bin/python3
  rm -rf /usr/bin/python3.6
  ln -s /usr/local/bin/python3.6 /usr/bin/python
  ln -s /usr/local/bin/python3.6 /usr/bin/python3
  ln -s /usr/local/bin/python3.6 /usr/bin/python3.6
  /usr/bin/python -V
  /usr/bin/python3 -V
  /usr/bin/python3.6 -V
  rm -rf /usr/local/bin/python
  rm -rf /usr/local/bin/python3
  ln -s /usr/local/bin/python3.6 /usr/local/bin/python
  ln -s /usr/local/bin/python3.6 /usr/local/bin/python3
  python -V
  python3 -V
  python3.6 -V

Python 裝飾器詳解  http://www.linuxidc.com/Linux/2017-08/146373.htm

Python 使用期物處理並行  http://www.linuxidc.com/Linux/2017-08/146555.htm

Ubuntu 16.04下安裝Vim8並支援Python3  http://www.linuxidc.com/Linux/2017-09/147108.htm

CentOS 6.4下安裝Python3  http://www.linuxidc.com/Linux/2017-09/147156.htm

Python升級導致yum不可用 “ No module named yum”  http://www.linuxidc.com/Linux/2017-08/146518.htm

Python 網路框架Twisted基礎學習及詳細講解  http://www.linuxidc.com/Linux/2017-08/146314.htm

本文永久更新連結地址http://www.linuxidc.com/Linux/2017-12/149807.htm


IT145.com E-mail:sddin#qq.com