CentOS 7 中默认安装了 Python,版本比较低(2.7.5),为了使用新版 3.x,需要对旧版本进行升级。由于很多基本的命令、软件包都依赖旧版本,比如:yum。所以,在更新 Python 时,建议不要删除旧版本(新旧版本可以共存)。
一、查看当前Python版本
# python -V
[root@localhost ~]# python -V
Python 2.7.
二、下载新的Python包并安装
进入python官网(https://www.python.org),选择需要的版本。此处选择Python-3.6.3版本
# yum install gcc gcc-c++ -y
Updated:
gcc.x86_64 :4.8.-.el7_6. gcc-c++.x86_64 :4.8.-.el7_6. Dependency Updated:
cpp.x86_64 :4.8.-.el7_6. gcc-gfortran.x86_64 :4.8.-.el7_6. libgcc.x86_64 :4.8.-.el7_6.
libgfortran.x86_64 :4.8.-.el7_6. libgomp.x86_64 :4.8.-.el7_6. libquadmath.x86_64 :4.8.-.el7_6.
libquadmath-devel.x86_64 :4.8.-.el7_6. libstdc++.x86_64 :4.8.-.el7_6. libstdc++-devel.x86_64 :4.8.-.el7_6. Complete!
下载
# wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
# wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz
[root@localhost ~]# wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
---- ::-- https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
Resolving www.python.org (www.python.org)... 151.101.228.223, 2a04:4e42:1a::
Connecting to www.python.org (www.python.org)|151.101.228.223|:... connected.
HTTP request sent, awaiting response... OK
Length: (22M) [application/octet-stream]
Saving to: ‘Python-3.6..tgz’ %[===================================================================================================================>] ,, .7KB/s in 3m 23s -- :: ( KB/s) - ‘Python-3.6..tgz’ saved [/] [root@localhost ~]#
[root@localhost ~]# ls
anaconda-ks.cfg Python-3.6..tgz
[root@localhost ~]#
[root@localhost ~]# du -sh Python-3.6..tgz
22M Python-3.6..tgz
解压
# tar zxvf Python-3.6.3.tgz
[root@localhost ~]# ls
anaconda-ks.cfg Python-3.6. Python-3.6..tgz
[root@localhost ~]#
查看文件内容
[root@localhost ~]# cd Python-3.6.
[root@localhost Python-3.6.]#
[root@localhost Python-3.6.]# ls
aclocal.m4 config.sub configure.ac Grammar install-sh LICENSE Makefile.pre.in Modules Parser PCbuild pyconfig.h.in README.rst Tools
config.guess configure Doc Include Lib Mac Misc Objects PC Programs Python setup.py
[root@localhost Python-3.6.]#
[root@localhost Python-3.6.]# ll
total
-rw-r--r--. Oct aclocal.m4
-rwxr-xr-x. Oct config.guess
-rwxr-xr-x. Oct config.sub
-rwxr-xr-x. Oct configure
-rw-r--r--. Oct configure.ac
drwxr-xr-x. Oct Doc
drwxr-xr-x. Oct Grammar
drwxr-xr-x. Oct Include
-rwxr-xr-x. Oct install-sh
drwxr-xr-x. Oct Lib
-rw-r--r--. Oct LICENSE
drwxr-xr-x. Oct Mac
-rw-r--r--. Oct Makefile.pre.in
drwxr-xr-x. Oct Misc
drwxr-xr-x. Oct Modules
drwxr-xr-x. Oct Objects
drwxr-xr-x. Oct Parser
drwxr-xr-x. Oct PC
drwxr-xr-x. Oct PCbuild
drwxr-xr-x. Oct Programs
-rw-r--r--. Oct pyconfig.h.in
drwxr-xr-x. Oct Python
-rw-r--r--. Oct README.rst
-rw-r--r--. Oct setup.py
drwxr-xr-x. Oct Tools
# ./configure
[root@localhost Python-3.6.]# ./configure
………………
………………
…………
creating Modules/Setup
creating Modules/Setup.local
creating Makefile If you want a release build with all stable optimizations active (PGO, etc),
please run ./configure --enable-optimizations
编译安装
# make && make install
报了一个错误
zipimport.ZipImportError: can't decompress data; zlib not available
make: *** [install] Error
验证
[root@localhost ~]# python -V
Python 2.7.
[root@localhost ~]# python3 -V
Python 3.6.
设置3.6.3为默认版本
查看 Python 的路径,在 /usr/bin 下面。可以看到 python 链接的是 python 2.7,所以,执行 python 就相当于执行 python 2.7
# ll /usr/bin/ | grep python
[root@localhost ~]# ll /usr/bin/ | grep python
lrwxrwxrwx. root root Mar : python -> python2
lrwxrwxrwx. root root Mar : python2 -> python2.
-rwxr-xr-x. root root Nov python2.
[root@localhost ~]#
将原来 python 的软链接重命名:
# mv /usr/bin/python /usr/bin/python.bak
将 python 链接至 python3:
# ln -s /usr/local/bin/python3 /usr/bin/python
[root@localhost ~]# ll /usr/bin/ | grep python
lrwxrwxrwx. root root Mar : python -> python2
lrwxrwxrwx. root root Mar : python2 -> python2.
-rwxr-xr-x. root root Nov python2.
[root@localhost ~]#
[root@localhost ~]# mv /usr/bin/python /usr/bin/python.bak
[root@localhost ~]#
[root@localhost ~]# ll /usr/bin/ | grep python
lrwxrwxrwx. root root Mar : python2 -> python2.
-rwxr-xr-x. root root Nov python2.
lrwxrwxrwx. root root Mar : python.bak -> python2
[root@localhost ~]#
[root@localhost ~]# ln -s /usr/local/bin/python3 /usr/bin/python
[root@localhost ~]#
[root@localhost ~]# ll /usr/bin/ | grep python
lrwxrwxrwx. root root Apr : python -> /usr/local/bin/python3
lrwxrwxrwx. root root Mar : python2 -> python2.
-rwxr-xr-x. root root Nov python2.
lrwxrwxrwx. root root Mar : python.bak -> python2
[root@localhost ~]#
配置YUM
升级 Python 之后,由于将默认的 python 指向了 python3,yum 不能正常使用,需要编辑 yum 的配置文件,此时:
[root@localhost ~]# yum list
File "/usr/bin/yum", line
except KeyboardInterrupt, e:
^
SyntaxError: invalid syntax
修改/usr/bin/yum和/usr/libexec/urlgrabber-ext-down,将 #!/usr/bin/python 改为 #!/usr/bin/python2.7,保存退出即可。
# python -V
[root@localhost ~]# python -V
Python 3.6.
参考博客:
https://www.cnblogs.com/lwf-blog/p/8182975.html
https://www.cnblogs.com/fjping0606/p/9156344.html
https://www.cnblogs.com/simuhunluo/p/7704765.html