在CentOS7中安装python3.7.0

1.下载及解压
python3.7的安装包可从官网下载上传到主机,也可以用wget直接下载。

[[email protected] ~]# cd /usr/local/src/
[[email protected] src]# wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
[[email protected] src]# tar xvJf Python-3.7.0.tar.xz
[[email protected] src]# mv Python-3.7.0 /usr/local/python-3.7
[[email protected] src]# cd /usr/local/python-3.7/
2.安装
安装之前,首先安装python依赖的包;

[[email protected] python-3.7]# yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel gcc-c++
配置编辑参数;

[[email protected] python-3.7]# ./configure –prefix=/usr/local/sbin/python-3.7
然后编译和安装:“make”、“make install”;

安装完成后,若出现以下两行,说明安装成功;

Installing collected packages: setuptools, pip
Successfully installed pip-10.0.1 setuptools-39.0.1
也可以用“echo $?”查看,结果为0说明安装成功;

然后就可以使用python3了。

[[email protected] python-3.7]# /usr/local/sbin/python-3.7/bin/python3
Python 3.7.0 (default, Jul 21 2018, 13:45:48)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
>>>
可以看到,虽然成功安装了python3.7,但是执行起来命令特别长,可以把python命令指向到到python3。

首先查看python命令的绝对路径,可以看到它是通过软链接执行到python2.7的。

[[email protected] ~]# python -V #查看python的默认版本
Python 2.7.5
[[email protected] ~]# which python #查看python命令的绝对路径
/usr/bin/python
[[email protected] ~]# ll /usr/bin/ |grep python
lrwxrwxrwx. 1 root root 7 7月 15 19:58 python -> python2
lrwxrwxrwx. 1 root root 9 7月 15 19:58 python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 8月 4 2017 python2.7
那我们也可以做一个软链接指向到python3命令。

[[email protected] ~]# rm -rf /usr/bin/python #删除原有的软链接文件
[[email protected] ~]# ln -s /usr/local/sbin/python-3.7/bin/python3 /usr/bin/python
[[email protected] ~]# ll /usr/bin/ |grep python
lrwxrwxrwx. 1 root root 38 7月 21 14:18 python -> /usr/local/sbin/python-3.7/bin/python3
lrwxrwxrwx. 1 root root 9 7月 15 19:58 python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 8月 4 2017 python2.7
现在python的默认版本就是python 3.7.0了。

[[email protected] ~]# python -V
Python 3.7.0
[[email protected] ~]# python
Python 3.7.0 (default, Jul 21 2018, 13:45:48)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
>>>

修改完python的默认版本后,会存在一个问题,无法执行yum命令,修改yum命令文件,将头文件改为python2.7即可。

[[email protected] ~]# vim /usr/bin/yum
#!/usr/bin/python2.7 #将头文件改为python2.7
同样的,将pip也指向到python3.7。

[[email protected] ~]# ln -s /usr/local/sbin/python-3.7/bin/pip3 /usr/bin/pip
[[email protected] ~]# pip –version
pip 10.0.1 from /usr/local/sbin/python-3.7/lib/python3.7/site-packages/pip (python 3.7)

LEAVE A REPLY

loading