Centos系统目前默认python环境版本号为2.7.5,在实际的开发、测试环境中我们可能需要2.7.13或者3.x的版本,但是Centos操作系统自带软件可能依赖python2.7.5版本,故原版本不能删除,我们只能python基础版本与高版本并存,以下为我实际的操作过程,有什么问题请指正😄😀
第一步:查看Centos版本及python版本
-
Centos版本
[root@amio ~]# cat /etc/centos-release
CentOS Linux release 7.3.1611 (Core)
-
python版本
[root@amio ~]# python -V
Python 2.7.5
[root@amio usr]# ll -l /usr/bin/python*
lrwxrwxrwx 1 root root 7 3月 29 22:44 /usr/bin/python -> python2
lrwxrwxrwx 1 root root 9 3月 29 22:44 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 7136 11月 6 00:29 /usr/bin/python2.7
-rwxr-xr-x 1 root root 1835 11月 6 00:29 /usr/bin/python2.7-config
lrwxrwxrwx 1 root root 16 4月 2 03:27 /usr/bin/python2-config -> python2.7-config
lrwxrwxrwx 1 root root 14 4月 2 03:27 /usr/bin/python-config -> python2-config
第二步:从官网下载python对应版本的包(以2.7.13版本为例)
[root@amio ~]# cd /home/ # 存放下载包的路径
[root@amio home]# wget https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz # wget 后接python官网对应的链接
--2017-04-02 03:14:53-- https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz
正在解析主机 www.python.org (www.python.org)... xxx.xxx.xxx.xxx, 2a04:4e42:11::223
正在连接 www.python.org (www.python.org)|xxx.xxx.xxx.xxx|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:17076672 (16M) [application/octet-stream]
正在保存至: “Python-2.7.13.tgz”
100%[==================================================================================>] 17,076,672 84.8MB/s 用时 0.2s
2017-04-02 03:14:53 (84.8 MB/s) - 已保存 “Python-2.7.13.tgz” [17076672/17076672])
[root@amio home]# ll
总用量 16680
-rw-r--r-- 1 root root 17076672 12月 17 20:21 Python-2.7.13.tgz
第三步:解压、配置、编译、安装python2.7.13
-
解压
[root@amio home]# tar -zxvf Python-2.7.13.tgz # 解压命令
[root@amio home]# ll # 解压后在当前目录生成Python-2.7.13的目录
总用量 16684
drwxr-xr-x 17 1000 1000 4096 12月 17 20:05 Python-2.7.13
-rw-r--r-- 1 root root 17076672 12月 17 20:21 Python-2.7.13.tgz
-
安装gcc(在编译时会依赖)
[root@amio home]# yum install gcc* openssl openssl-devel ncurses-devel.x86_64 bzip2-devel sqlite-devel python-devel zlib
-
配置、编译、安装
[root@amio home]# cd Python-2.7.13
[root@amio Python-2.7.13]# (sudo) ./configure --prefix=/usr/local # [配置]指定可执行文件、库文件、配置文件、资源文件的安装路径。若没有权限加sudo
[root@amio Python-2.7.13]# (sudo) make # 编译
[root@amio Python-2.7.13]# make altinstall # 不要使用make install,否则会覆盖系统自带python
第四步:安装后环境检查
-
python安装后的版本
[root@amio ~]# python -V # 发现版本还是原版本
Python 2.7.5
-
安装前后的python对比
[root@amio ~]# ll -l /usr/bin/python* # 系统自带的
lrwxrwxrwx 1 root root 7 3月 29 22:44 /usr/bin/python -> python2
lrwxrwxrwx 1 root root 9 3月 29 22:44 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 7136 11月 6 00:29 /usr/bin/python2.7
-rwxr-xr-x 1 root root 1835 11月 6 00:29 /usr/bin/python2.7-config
lrwxrwxrwx 1 root root 16 4月 2 03:27 /usr/bin/python2-config -> python2.7-config
lrwxrwxrwx 1 root root 14 4月 2 03:27 /usr/bin/python-config -> python2-config
[root@amio ~]# ll -l /usr/local/bin/python* # 手工安装的
-rwxr-xr-x 1 root root 8257136 4月 2 04:48 /usr/local/bin/python2.7
-rwxr-xr-x 1 root root 1687 4月 2 04:49 /usr/local/bin/python2.7-config
-
备份旧版本,连接新版本
[root@amio ~]# mv /usr/bin/python /usr/bin/python2.7.5
[root@amio ~]# ll -l /usr/bin/python*
lrwxrwxrwx 1 root root 9 3月 29 22:44 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 7136 11月 6 00:29 /usr/bin/python2.7
lrwxrwxrwx 1 root root 7 3月 29 22:44 /usr/bin/python2.7.5 -> python2 # 改为2.7.5
-rwxr-xr-x 1 root root 1835 11月 6 00:29 /usr/bin/python2.7-config
lrwxrwxrwx 1 root root 16 4月 2 03:27 /usr/bin/python2-config -> python2.7-config
lrwxrwxrwx 1 root root 14 4月 2 03:27 /usr/bin/python-config -> python2-config
[root@amio ~]# ln -s /usr/local/bin/python2.7 /usr/bin/python # 增加连接
[root@amio ~]# ll -l /usr/bin/python*
lrwxrwxrwx 1 root root 24 4月 2 05:08 /usr/bin/python -> /usr/local/bin/python2.7 # 新增的,并指向新安装的python
lrwxrwxrwx 1 root root 9 3月 29 22:44 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 7136 11月 6 00:29 /usr/bin/python2.7
lrwxrwxrwx 1 root root 7 3月 29 22:44 /usr/bin/python2.7.5 -> python2
-rwxr-xr-x 1 root root 1835 11月 6 00:29 /usr/bin/python2.7-config
lrwxrwxrwx 1 root root 16 4月 2 03:27 /usr/bin/python2-config -> python2.7-config
lrwxrwxrwx 1 root root 14 4月 2 03:27 /usr/bin/python-config -> python2-config
-
再次检查python版本
[root@amio ~]# python #正常展示python2.7.13版本
Python 2.7.13 (default, Apr 2 2017, 04:48:29)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
-
若想访问老版本python(如2.7.5版本)
[root@amio ~]# python2.7.5
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
-
题外话:python2, python2.7访问的是2.7.5还是2.7.13呢
[root@amio ~]# python2.7 #python2.7.13
Python 2.7.13 (default, Apr 2 2017, 04:48:29)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
[root@amio ~]# python2 #python2.7.5
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
番外:yum的设置(系统预装的yum引用的老版本python)
[root@amio ~]# yum -y install epel-release
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:
No module named yum # 😭😭😭这时候报错了
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:
2.7.13 (default, Apr 2 2017, 04:48:29)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
If you cannot solve this problem yourself, please go to
the yum faq at:
http://yum.baseurl.org/wiki/Faq
[root@amio ~]# vi /usr/bin/yum
首行的#!/usr/bin/python 改为 #!/usr/bin/python2.7
改完之后继续安装,又报错😭
ImportError: No module named urlgrabber.grabber
[root@amio ~]# vi /usr/libexec/urlgrabber-ext-down
首行的#!/usr/bin/python 改为 #!/usr/bin/python2.7