Centos 7从python2.7.5升级到python2.7.13环境实战

Centos系统目前默认python环境版本号为2.7.5,在实际的开发、测试环境中我们可能需要2.7.13或者3.x的版本,但是Centos操作系统自带软件可能依赖python2.7.5版本,故原版本不能删除,我们只能python基础版本与高版本并存,以下为我实际的操作过程,有什么问题请指正😄😀

python版本的升级.png

第一步:查看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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容