Centos7安装python3并与python2共存

@lianst  September 1, 2018

不出意外的话,Centos7默认安装的是Python2.7.5版本。因为目前做的项目和当前趋势都已经向Python3看齐了,所以咱也赶上时代的潮流,把Python版本升级到3。

因为Centos中yum命令是用的Python2,所以我们还不能彻底抛弃Python2。

查看当前版本

[root@django software]# python -V
Python 2.7.5
[root@django software]# which python
/usr/bin/python

安装依赖

[root@django software]# yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make

备份当前版本

mv /usr/bin/python /usr/bin/python2.7.5
[root@django software]# mv /usr/bin/python /usr/bin/python2.7.5                
[root@django software]# ls /usr/bin/python
python2           python2.7         python2.7.5       python2.7-config  python2-config    python-config 

安装Python3

  1. Download Python3安装包
[root@django software]# wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz
  1. 编译安装
[root@django software]# tar -xvJf Python-3.6.2.tar.xz 
[root@django software]# cd Python-3.6.2
[root@django Python-3.6.2]# ./configure prefix=/usr/local/python3
[root@django Python-3.6.2]# make && make install

fi
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-9.0.1 setuptools-28.8.0
[root@django Python-3.6.2]# ln -s /usr/local/python3/bin/python3 /usr/bin/python
[root@django Python-3.6.2]# python -V
Python 3.6.2

配置yum Python版本

Python3安装好之后,你会发现你的yum命令无法使用。此刻不用着急,因为yum使用的是python2版本,我们需要小小的修改2个文件一下就可以了。

  • 修改yum命令主文件
[root@django Python-3.6.2]# which yum
/usr/bin/yum
[root@django Python-3.6.2]# vim /usr/bin/yum # yum命令主文件
  1 #!/usr/bin/python2.7.5
  2 import sys
  3 try:
  4     import yum
  5 except ImportError:
  • 修改库文件

单单修改yum命令主文件之后,在使用yum的时候还是会报错的,还需要修改一个库文件。

Total download size: 190 k
Installed size: 411 k
Downloading packages:
  File "/usr/libexec/urlgrabber-ext-down", line 28
    except OSError, e:
                  ^
SyntaxError: invalid syntax
  File "/usr/libexec/urlgrabber-ext-down", line 28
    except OSError, e:
                  ^
SyntaxError: invalid syntax

修改库文件

[root@django Python-3.6.2]# vim /usr/libexec/urlgrabber-ext-down
 1 #! /usr/bin/python2.7.5
  2 #  A very simple external downloader
  3 #  Copyright 2011-2012 Zdenek Pavlas
  4 

2个文件都修改完成之后,我们可以尝试使用yum来安装一个软件来测试是否修改成功。

[root@django Python-3.6.2]# yum -y install iftop
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ossm.utm.my
 * epel: mirrors.up.pt
 * extras: centos.mirror.myduniahost.com
 * updates: ossm.utm.my
Resolving Dependencies
--> Running transaction check
---> Package iftop.x86_64 0:1.0-0.14.pre4.el7 will be installed
--> Processing Dependency: libpcap.so.1()(64bit) for package: iftop-1.0-0.14.pre4.el7.x86_64
--> Running transaction check
---> Package libpcap.x86_64 14:1.5.3-11.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================================================================================
 Package                              Arch                                Version                                           Repository                         Size
====================================================================================================================================================================
Installing:
 iftop                                x86_64                              1.0-0.14.pre4.el7                                 epel                               52 k
Installing for dependencies:
 libpcap                              x86_64                              14:1.5.3-11.el7                                   base                              138 k

Transaction Summary
====================================================================================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 190 k
Installed size: 411 k
Downloading packages:
(1/2): iftop-1.0-0.14.pre4.el7.x86_64.rpm                                                                                                    |  52 kB  00:00:00     
(2/2): libpcap-1.5.3-11.el7.x86_64.rpm                                                                                                       | 138 kB  00:00:01     
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                92 kB/s | 190 kB  00:00:02     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 14:libpcap-1.5.3-11.el7.x86_64                                                                                                                   1/2 
  Installing : iftop-1.0-0.14.pre4.el7.x86_64                                                                                                                   2/2 
  Verifying  : 14:libpcap-1.5.3-11.el7.x86_64                                                                                                                   1/2 
  Verifying  : iftop-1.0-0.14.pre4.el7.x86_64                                                                                                                   2/2 

Installed:
  iftop.x86_64 0:1.0-0.14.pre4.el7                                                                                                                                  

Dependency Installed:
  libpcap.x86_64 14:1.5.3-11.el7                                                                                                                                    

Complete!

添加新评论