前言
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开头的相关文件详情
[root@yoyo /]# cd /
[root@yoyo /]# 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
[root@yoyo /]# cd /usr/bin
[root@yoyo 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
[root@yoyo bin]#
从查看的结果可以看到python指向的是python2 ,python2指向的是python2.7。那也就是在控制台输入python、python2、python2.7都是运行python2.7
如果我们安装一个python3.6的环境,让python3指向3.6, python2指向python2.7, 那就可以完美的共存了
yum安装依赖
先安装相关依赖
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel mysql-devel
安装python3
从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安装包,比如: /root/python36
[root@yoyo ~]# mkdir /root/python36
cd到 root/pyrhon36目录,用wget下载3.6.8安装包
[root@yoyo python36]# wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
[root@yoyo~]# cd /root/pyrhon36
[root@yoyo 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]
[root@yoyo python36]#
等待下载完成之后会在当前目录下出现一个tgz包,tar命令解压这个包到当前目录就可以
[root@yoyo python36]# tar -xvf Python-3.6.8.tgz
[root@yoyo python36]# 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的默认目录,不在一块。我们指定安装目录,以后卸载的话直接删除目录就可以干净卸载了。
[root@yoyo python36]# cd Python-3.6.8
[root@yoyo Python-3.6.8]# ./configure --prefix=/root/python36
在当前目录root/pyrhon36/Python-3.6.8执行make,执行完之后,接着输入make install
[root@yoyo Python-3.6.8]# make
[root@yoyo Python-3.6.8]# make install
耐心等待,喝杯咖啡就安装完成就行了,时间会有点长。
添加软链接
由于系统默认的python是指向python2,这里执行把新安装的python3.6指向给/usr/bin/python3就可以了
[root@yoyo bin]# ln -s /root/python36/bin/python3.6 /usr/bin/python3
[root@yoyo bin]# python3 -V
Python 3.6.8
[root@yoyo bin]# python3
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()
[root@yoyo bin]#
输入python3 -V可以看到版本号,这样系统默认的就是python3.6.8版本了,如果想用python2.7版本,直接输入python2就可以了
[root@yoyo 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()
pip环境
python3安装完成之后,如果想用pip安装一些第三方包,系统会默认安装到python2的环境里面。
添加pip3的软链接,这样输入pip3 install 就能把安装包安装到python3的环境了。
[root@yoyo bin]# ln -s /root/python36/bin/pip3 /usr/bin/pip3
[root@yoyo bin]# pip3 -V
pip 19.2.3 from /usr/local/python3/lib/python3.6/site-packages/pip (python 3.6)
[root@yoyo bin]# pip2 -V
pip 18.1 from /usr/lib/python2.7/site-packages/pip (python 2.7)
[root@yoyo bin]# ll pip*
lrwxrwxrwx 1 root root 27 Jan 2 16:47 pip3 -> /usr/local/python3/bin/pip3
-rwxr-xr-x 1 root root 216 Jan 2 11:05 pip2
-rwxr-xr-x 1 root root 216 Jan 2 11:05 pip2.7
-rwxr-xr-x 1 root root 232 Jan 2 16:53 pip.bak
如果要安装python3的第三方包,那就输入pip3 install xx包名
[root@yoyo bin]# pip3 install requests
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting requests
Downloading http://mirrors.aliyun.com/pypi/packages/7d/e3/20f3d364d6c8e5d2353c72a67778eb189176f08e873c9900e10c0287b84b/requests-2.21.0-py2.py3-none-any.whl (57kB)
100% |████████████████████████████████| 61kB 2.2MB/s
Requirement already satisfied: idna<2.9,>=2.5 in /usr/local/python3/lib/python3.6/site-packages (from requests) (2.8)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/python3/lib/python3.6/site-packages (from requests) (2018.11.29)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/local/python3/lib/python3.6/site-packages (from requests) (3.0.4)
Requirement already satisfied: urllib3<1.25,>=1.21.1 in /usr/local/python3/lib/python3.6/site-packages (from requests) (1.24.1)
Installing collected packages: requests
Successfully installed requests-2.21.0
[root@yoyo bin]# python
Python 3.6.8 (default, Jan 2 2019, 16:43:17)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> exit()
[root@yoyo bin]#
交流QQ群:779429633