前言
centos7 自带有 python,但是却是 python2 版本的 python,如果你想安装个python3怎么办呢?
如果直接删除python2的话,可能会引起其他的问题,因为有些东西是依赖python2的,最好的解决办法是python3和python2共存,新安装一个python3的环境。
python2
先找到系统的python安装在哪个目录,查看对应版本号和相关安装包
cd / 先回到根目录
whereis python 查看python所在目录/usr/bin
cd /usr/bin 切到python目录
ll python* 查看python开头的相关文件详情
[[email protected] /]# cd /
[[email protected] /]# whereis python
python: /usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz
[[email protected] /]# cd /usr/bin
[[email protected] bin]# ll python*
lrwxrwxrwx. 1 root root 7 Oct 15 2017 python -> python2
lrwxrwxrwx. 1 root root 9 Oct 15 2017 python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 Aug 4 2017 python2.7
[[email protected] bin]#
从查看的结果可以看到python指向的是python2 ,python2指向的是python2.7。那也就是在控制台输入python、python2、python2.7都是运行python2.7
如果我们安装一个python3.6的环境,让python指向3.6, python2指向python2.7, 那就可以完美的共存了
安装python3
安装python3.6之前,先备份python包,因为一会要替换这个文件
[[email protected] bin]# mv python python.bak
从python安装包的历史版本中https://www.python.org/ftp/python/,找到需要的安装包,比如我这里选3.6.8版本
从众多的包里面找到Python-3.6.8.tgz包,那么下载地址拼接下就是:https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
在Centos 7系统里面新建一个目录,用于存放下载的python3安装包,比如:/usr/local
[[email protected] bin]# mkdir /usr/local/python3
cd到 /usr/local/python3目录,用wget下载3.6.8安装包
[[email protected] python3]# wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
[[email protected] bin]# cd /usr/local/python3
[[email protected] python3]# wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
--2018-12-29 13:37:12-- https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
Resolving www.python.org (www.python.org)... 151.101.108.223, 2a04:4e42:1a::223
Connecting to www.python.org (www.python.org)|151.101.108.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 23010188 (22M) [application/octet-stream]
Saving to: ‘Python-3.6.8.tgz’
100%[===============================================================================>] 23,010,188 14.2MB/s in 1.5s
2018-12-29 13:37:14 (14.2 MB/s) - ‘Python-3.6.8.tgz’ saved [23010188/23010188]
[[email protected] python3]#
等待下载完成之后会在当前目录下出现一个tgz包,tar命令解压这个包到当前目录就可以
[[email protected] python3]# tar -xvf Python-3.6.8.tgz
[[email protected] python3]# ll
total 22476
drwxr-xr-x 17 501 501 4096 Dec 24 11:01 Python-3.6.8
-rw-r--r-- 1 root root 23010188 Dec 24 11:01 Python-3.6.8.tgz
解压完之后需要编译Python-3.6.8包下的文件,先cd过去执行完这句命令之后,把python的安装目录指定一下,这样的话,里面的一些bin目录、lib目录就都会存放在这个目录下面。
如果不指定这个安装目录的话,最后python的安装文件将分散到linux的默认目录,不在一块。我们指定安装目录,以后卸载的话直接删除目录就可以干净卸载了。
[[email protected] python3]# cd Python-3.6.3
[[email protected] python3]# ./configure --prefix=/usr/local/python3Dir
在当前目录/usr/local/python3/Python-3.6.8执行make,执行完之后,接着输入make install
[[email protected] python3]# make
[[email protected] python3]# make install
执行完毕之后,我们就可以切换到/usr/local/python3Dir目录下去查看,python3.6在bin目录
[[email protected] bin]# cd /usr/local/python3Dir
[[email protected] python3Dir]# ls
bin include lib share
[[email protected] python3Dir]# cd bin
[[email protected] bin]# ls
2to3 idle3 pydoc3 python3 python3.6-config python3.6m-config pyvenv
2to3-3.6 idle3.6 pydoc3.6 python3.6 python3.6m python3-config pyvenv-3.6
[[email protected] bin]#
添加软链接
由于系统默认的python是指向python2,前面已经删除备份过了,这里执行把新安装的python3.6指向给/usr/bin/pythonn就可以了
[[email protected] bin]# ln -s /usr/local/python3Dir/bin/python3 /usr/bin/python
[[email protected] bin]# python -V
Python 3.6.8
[[email protected] bin]# python
Python 3.6.8 (default, Dec 29 2018, 13:48:00)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[[email protected] bin]#
输入python -V可以看到版本号,这样系统默认的就是python3.6.8版本了,如果想用python2.7版本,直接输入python2就可以了
[[email protected] bin]# python2
Python 2.7.5 (default, Aug 4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
yum配置
yum是依赖于python2的,所以需要更改yum里面的python指向,vim打开 /usr/bin/yum
[[email protected] Python-3.6.8]# vim /usr/bin/yum
#! /usr/bin/python修改为#! /usr/bin/python2
打开后输入键盘上i键,进入编辑状态,把第一行#! /usr/bin/python修改为#! /usr/bin/python2
编辑完之后,按ESC返回,输入:wq保存退出
接着vim打开/usr/libexec/urlgrabber-ext-down
[[email protected] Python-3.6.8]# vim /usr/libexec/urlgrabber-ext-down
#! /usr/bin/python 修改为#! /usr/bin/python2
原文地址:https://www.cnblogs.com/yoyoketang/p/10195102.html