美文网首页
Centos 7从python2.7.5升级到python2.7

Centos 7从python2.7.5升级到python2.7

作者: 旭娃 | 来源:发表于2018-12-12 14:00 被阅读0次

原文:https://www.jianshu.com/p/fad3942fc0ed

第一步:查看Centos版本及python版本

Centos版本

[root@amio ~]# cat /etc/centos-releaseCentOS Linux release7.3.1611(Core)

python版本

[root@amio ~]# python -VPython2.7.5[root@amio usr]# ll -l /usr/bin/python*lrwxrwxrwx1root root73月2922:44/usr/bin/python -> python2lrwxrwxrwx1root root93月2922:44/usr/bin/python2 -> python2.7-rwxr-xr-x1root root713611月600:29/usr/bin/python2.7-rwxr-xr-x1root root183511月600:29/usr/bin/python2.7-configlrwxrwxrwx1root root164月203:27/usr/bin/python2-config -> python2.7-configlrwxrwxrwx1root root144月203: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-0203: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 请求,正在等待回应...200OK长度:17076672(16M) [application/octet-stream]正在保存至: “Python-2.7.13.tgz”100%[==================================================================================>]17,076,67284.8MB/s 用时0.2s2017-04-0203:14:53(84.8MB/s) - 已保存 “Python-2.7.13.tgz” [17076672/17076672])[root@amio home]# ll总用量16680-rw-r--r--1root root1707667212月1720:21Python-2.7.13.tgz

第三步:解压、配置、编译、安装python2.7.13

解压

[root@amio home]#tar-zxvfPython-2.7.13.tgz# 解压命令[root@amio home]#ll# 解压后在当前目录生成Python-2.7.13的目录总用量 16684drwxr-xr-x17 1000 1000    4096 12月 17 20:05Python-2.7.13-rw-r--r--1rootroot17076672 12月 17 20:21Python-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# 发现版本还是原版本Python2.7.5

安装前后的python对比

[root@amio ~]# ll -l /usr/bin/python*  # 系统自带的lrwxrwxrwx 1 root root    7 3月  29 22:44 /usr/bin/python -> python2lrwxrwxrwx 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-configlrwxrwxrwx 1 root root  16 4月  2 03:27 /usr/bin/python2-config -> python2.7-configlrwxrwxrwx 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*lrwxrwxrwx1root root93月2922:44/usr/bin/python2 -> python2.7-rwxr-xr-x1root root713611月600:29/usr/bin/python2.7lrwxrwxrwx1root root73月2922:44/usr/bin/python2.7.5-> python2# 改为2.7.5-rwxr-xr-x1root root183511月600:29/usr/bin/python2.7-configlrwxrwxrwx1root root164月203:27/usr/bin/python2-config -> python2.7-configlrwxrwxrwx1root root144月203: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*lrwxrwxrwx1root root244月205:08 /usr/bin/python ->/usr/local/bin/python2.7# 新增的,并指向新安装的pythonlrwxrwxrwx1root root93月2922:44/usr/bin/python2 -> python2.7-rwxr-xr-x1root root713611月600:29/usr/bin/python2.7lrwxrwxrwx1root root73月2922:44/usr/bin/python2.7.5-> python2-rwxr-xr-x1root root183511月600:29/usr/bin/python2.7-configlrwxrwxrwx1root root164月203:27/usr/bin/python2-config -> python2.7-configlrwxrwxrwx1root root144月203:27/usr/bin/python-config -> python2-config

再次检查python版本

[root@amio ~]# python  #正常展示python2.7.13版本Python2.7.13(default, Apr22017,04:48:29)[GCC4.8.520150623(Red Hat4.8.5-11)] on linux2Type"help","copyright","credits"or"license"formore information.

若想访问老版本python(如2.7.5版本)

[root@amio ~]# python2.7.5Python2.7.5(default, Nov62016,00:28:07)[GCC4.8.520150623(Red Hat4.8.5-11)] on linux2Type"help","copyright","credits"or"license"formore information.

题外话:python2, python2.7访问的是2.7.5还是2.7.13呢

[root@amio ~]#python2.7#python2.7.13Python2.7.13(default,Apr2 2017, 04:48:29)[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]onlinux2Type"help", "copyright", "credits"or"license"formoreinformation.[root@amio ~]#python2#python2.7.5Python2.7.5(default,Nov6 2016, 00:28:07)[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]onlinux2Type"help", "copyright", "credits"or"license"formoreinformation.

番外:yum的设置(系统预装的yum引用的老版本python)

[root@amio ~]# yum -y install epel-releaseThere was a problem importing one of the Python modulesrequired to run yum. The error leading tothisproblem was:  Nomodulenamed yum  # 😭😭😭这时候报错了Please install a package which providesthismodule,orverify that themoduleis installed correctly.It's possible that the abovemoduledoesn't match thecurrent version of Python, which is:2.7.13(default, Apr22017,04:48:29)[GCC4.8.520150623(Red Hat4.8.5-11)]If you cannot solvethisproblem yourself, please go tothe yum faq at:  http://yum.baseurl.org/wiki/Faq[root@amio ~]# vi /usr/bin/yum首行的#!/usr/bin/python 改为 #!/usr/bin/python2.7改完之后继续安装,又报错😭ImportError: Nomodulenamed urlgrabber.grabber[root@amio ~]# vi /usr/libexec/urlgrabber-ext-down首行的#!/usr/bin/python 改为 #!/usr/bin/python2.7

作者:Amio_

链接:https://www.jianshu.com/p/fad3942fc0ed

來源:简书

简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

相关文章

网友评论

      本文标题:Centos 7从python2.7.5升级到python2.7

      本文链接:https://www.haomeiwen.com/subject/vxnuhqtx.html