简介:
在Cetnos系统上安装Python
安装环境及版本:
系统环境:CentOS Linux release 7.5.1804 (Core)
Python版本:Python-3.6.3.tgz
一、预备
Centos7中默认是有python安装的,但是是2.7版本,如果2.7版本够用,就不需要安装了,此处需要安装Python3.6.3
[root@localhost ~]# cd /usr/bin/
[root@localhost bin]# ls python*
python python2 python2.7
通过快捷方式可以看出这几个文件之间是有依赖关系的
[root@localhost bin]# ls -al python*
lrwxrwxrwx. 1 root root 7 Apr 23 2019 python -> python2
lrwxrwxrwx. 1 root root 9 Apr 23 2019 python2 -> python2.7
-rwxr-xr-x. 1 root root 7216 Apr 11 2018 python2.7
安装版本Python3.6.3之前备份一下,只需要备份第一个即可
[root@localhost bin]# mv python python.bak
[root@localhost bin]# ls -al python*
lrwxrwxrwx. 1 root root 9 Apr 23 2019 python2 -> python2.7
-rwxr-xr-x. 1 root root 7216 Apr 11 2018 python2.7
lrwxrwxrwx. 1 root root 7 Apr 23 2019 python.bak -> python2
二、安装
2.1 创建目录并下载版本
注:下载前确保wget已经安装,如果没有安装的话:yum -y install wget
创建安装目录:mkdir /usr/local/python3
进入目录:cd /usr/local/python3/
下载安装包:wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
[root@localhost ~]# mkdir /usr/local/python3
[root@localhost ~]# cd /usr/local/python3/
[root@localhost python3]# wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
已发出 HTTP 请求,正在等待回应... 200 OK
长度:22673115 (22M) [application/octet-stream]
正在保存至: “Python-3.6.3.tgz”
100%[===========================================================================================>] 22,673,115 1.29MB/s 用时 35s
2020-07-05 16:32:07 (639 KB/s) - 已保存 “Python-3.6.3.tgz” [22673115/22673115])
[root@localhost python3]# ll
总用量 22144
-rw-r--r-- 1 root root 22673115 10月 3 2017 Python-3.6.3.tgz
2.2 解压缩
解压缩:tar -xvf Python-3.6.3.tgz
[root@localhost python3]# tar -xvf Python-3.6.3.tgz
Python-3.6.3/
Python-3.6.3/Doc/
Python-3.6.3/Doc/c-api/
...............
Python-3.6.3/Objects/dictnotes.txt
Python-3.6.3/Objects/typeslots.inc
2.3 编译安装
注:编译安装前确保gcc gcc-c++已经安装,如果没有安装的话:yum -y install gcc gcc-c++
进入解压缩目录:cd Python-3.6.3/
检测:./configure –prefix=/usr/local/python3Dir
编译:make
安装:make install
[root@localhost python3]# cd Python-3.6.3/
...
[root@localhost Python-3.6.3]# ./configure --prefix=/usr/local/python3Dir
...
creating Makefile
If you want a release build with all stable optimizations active (PGO, etc),
please run ./configure --enable-optimizations
[root@localhost Python-3.6.3]# make
...
# On Darwin, always use the python version of the script, the shell
# version doesn't use the compiler customizations that are provided
# in python (_osx_support.py).
if test `uname -s` = Darwin; then \
cp python-config.py python-config; \
fi
[root@localhost Python-3.6.3]# make install
...
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-9.0.1 setuptools-28.8.0
进入安装目录:/usr/local/python3Dir
[root@localhost Python-3.6.3]# cd /usr/local/python3Dir/
[root@localhost python3Dir]# ll
total 0
drwxr-xr-x 2 root root 295 Jul 1 23:54 bin
drwxr-xr-x 3 root root 24 Jul 1 23:54 include
drwxr-xr-x 4 root root 63 Jul 1 23:54 lib
drwxr-xr-x 3 root root 17 Jul 1 23:54 share
2.4 创建软连接
便于后续使用python命令时,指向的是python3,而不是之前的python2.7。
ln -s /usr/local/python3Dir/bin/python3 /usr/bin/python
三、验证
查看版本:python -V
进入命令行:python
[root@localhost python3Dir]# python -V
Python 3.6.3
[root@localhost python3Dir]# python
Python 3.6.3 (default, Jul 1 2020, 23:53:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
四、最后
由于centos的yum命令是需要python支持的,贸然进行版本更换,容易导致yum出错,需要保持yum依然用原来的2.7版本
vi /usr/bin/yum
#!/usr/bin/python改成#!/usr/bin/python2.7
[root@localhost python3Dir]# cat /usr/bin/yum
#!/usr/bin/python2.7
import sys
try:
......
有些时候yum安装还会报如下错,也是需要将/usr/libexec/urlgrabber-ext-down文件中的#!/usr/bin/python改成#!/usr/bin/python2.7
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
File "/usr/libexec/urlgrabber-ext-down", line 28
except OSError, e:
^
SyntaxError: invalid syntax