前言

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@VM-0-4-centos ~]# cd /
[root@VM-0-4-centos /]# whereis python
python: /usr/bin/python /usr/bin/python2.7-config /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@VM-0-4-centos /]# cd /usr/bin
[root@VM-0-4-centos bin]# ll python*
lrwxrwxrwx 1 root root    7 Jun 11 14:55 python -> python2
lrwxrwxrwx 1 root root    9 Jun 11 14:55 python2 -> python2.7
-rwxr-xr-x 1 root root 7144 Apr  2  2020 python2.7
-rwxr-xr-x 1 root root 1835 Apr  2  2020 python2.7-config
lrwxrwxrwx 1 root root   16 Jun 11 14:55 python2-config -> python2.7-config
lrwxrwxrwx 1 root root   28 Oct 11 18:30 python3 -> /root/python38/bin/python3.8
lrwxrwxrwx 1 root root   14 Jun 11 14:55 python-config -> python2-config
[root@VM-0-4-centos bin]#

我这时已经安装好python3,所以会显示出来

从查看的结果可以看到python指向的是python2 ,python2指向的是python2.7。那也就是在控制台输入python、python2、python2.7都是运行python2.7

如果我们安装一个python3.8的环境,让python3指向3.8, 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.8.6版本

请输入图片描述

请输入图片描述

在Centos 7系统里面新建一个目录,用于存放下载的python3安装包,比如: /root/python38

[root@VM-0-4-centos bin]# mkdir /root/python38

cd到 root/pyrhon36目录,用wget下载3.8.6安装包

[root@VM-0-4-centos bin]# wget [https://www.python.org/ftp/python/3.8.6/Python-3.8.6.tgz

[root@VM-0-4-centos bin]# cd /root/python38
[root@VM-0-4-centos python38]# wget https://www.python.org/ftp/python/3.8.6/Python-3.8.6.tgz
--2020-10-11 18:08:12--  https://www.python.org/ftp/python/3.8.6/Python-3.8.6.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.8.6.tgz’

100%[===============================================================================>] 23,010,188  14.2MB/s   in 1.5s

2020-10-11 18:10:14 (14.2 MB/s) - ‘Python-3.8.6.tgz’ saved [23010188/23010188]

[root@VM-0-4-centos python38]#

等待下载完成之后会在当前目录下出现一个tgz包,tar命令解压这个包到当前目录就可以

[root@VM-0-4-centos python38]# tar -xvf Python-3.8.6.tgz
[root@VM-0-4-centos python38]# ll
total 22476
drwxr-xr-x 17  501  501     4096 Dec 24 11:01 Python-3.8.6
-rw-r--r--  1 root root 23010188 Dec 24 11:01 Python-3.8.6.tgz

解压完之后需要编译Python-3.8.6包下的文件,先cd过去执行完这句命令之后,把python的安装目录指定一下,这样的话,里面的一些bin目录、lib目录就都会存放在这个目录下面。
如果不指定这个安装目录的话,最后python的安装文件将分散到linux的默认目录,不在一块。我们指定安装目录,以后卸载的话直接删除目录就可以干净卸载了。

[root@VM-0-4-centos python38]# cd Python-3.8.6
[root@VM-0-4-centos Python-3.8.6]# ./configure --prefix=/root/python38

在当前目录root/pyrhon38/Python-3.8.6执行make,执行完之后,接着输入make install

[root@VM-0-4-centos Python-3.8.6]## make

[root@VM-0-4-centos Python-3.8.6]## make install

耐心等待,时间会有点长。

添加软链接

由于系统默认的python是指向python2,这里执行把新安装的python3.8指向给/usr/bin/python3就可以了

[root@VM-0-4-centos bin]# ln -s  /root/python38/bin/python3.8 /usr/bin/python3

[root@VM-0-4-centos bin]# python3 -V
Python 3.8.6
[root@VM-0-4-centos bin]# python3
Python 3.8.6 (default, Oct 11 2020, 18:28:25)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> exit()
[root@VM-0-4-centos bin]#

输入python3 -V可以看到版本号,这样系统默认的就是python3.8.6版本了,如果想用python2.7版本,直接输入python2就可以了

pip环境

python3安装完成之后,如果想用pip安装一些第三方包,系统会默认安装到python2的环境里面。
添加pip3的软链接,这样输入pip3 install 就能把安装包安装到python3的环境了。

[root@VM-0-4-centos bin]# ln -s /root/python38/bin/pip3  /usr/bin/pip3

[root@VM-0-4-centos bin]# pip3 -V

pip 20.2.1 from /root/python38/lib/python3.8/site-packages/pip (python 3.8)
[root@VM-0-4-centos bin]# pip2 -V
pip 20.1.1 from /usr/lib/python2.7/site-packages/pip (python 2.7)
[root@VM-0-4-centos bin]# ll pip*
-rwxr-xr-x 1 root root 219 Jun 11 14:56 pip
-rwxr-xr-x 1 root root 219 Jun 11 14:56 pip2
-rwxr-xr-x 1 root root 219 Jun 11 14:56 pip2.7
lrwxrwxrwx 1 root root  23 Oct 11 18:37 pip3 -> /root/python38/bin/pip3

[root@VM-0-4-centos bin]#

如果要安装python3的第三方包,那就输入pip3 install xx包名

[root@VM-0-4-centos bin]# pip3 install requests
Looking in indexes: https://mirrors.cloud.tencent.com/pypi/simple
Collecting requests
  Downloading https://mirrors.cloud.tencent.com/pypi/packages/45/1e/0c169c6a5381e241ba7404532c16a21d86ab872c9bed8bdcd4c423954103/requests-2.24.0-py2.py3-none-any.whl (61 kB)
     |████████████████████████████████| 61 kB 202 kB/s
Collecting idna<3,>=2.5
  Downloading https://mirrors.cloud.tencent.com/pypi/packages/a2/38/928ddce2273eaa564f6f50de919327bf3a00f091b5baba8dfa9460f3a8a8/idna-2.10-py2.py3-none-any.whl (58 kB)
     |████████████████████████████████| 58 kB 632 kB/s
Collecting certifi>=2017.4.17
  Downloading https://mirrors.cloud.tencent.com/pypi/packages/5e/c4/6c4fe722df5343c33226f0b4e0bb042e4dc13483228b4718baf286f86d87/certifi-2020.6.20-py2.py3-none-any.whl (156 kB)
     |████████████████████████████████| 156 kB 411 kB/s
Collecting chardet<4,>=3.0.2
  Downloading https://mirrors.cloud.tencent.com/pypi/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133 kB)
     |████████████████████████████████| 133 kB 407 kB/s
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1
  Downloading https://mirrors.cloud.tencent.com/pypi/packages/9f/f0/a391d1463ebb1b233795cabfc0ef38d3db4442339de68f847026199e69d7/urllib3-1.25.10-py2.py3-none-any.whl (127 kB)
     |████████████████████████████████| 127 kB 798 kB/s
Installing collected packages: idna, certifi, chardet, urllib3, requests
Successfully installed certifi-2020.6.20 chardet-3.0.4 idna-2.10 requests-2.24.0 urllib3-1.25.10
WARNING: You are using pip version 20.2.1; however, version 20.2.3 is available.
You should consider upgrading via the '/root/python38/bin/python3.8 -m pip install --upgrade pip' command.

[root@VM-0-4-centos bin]# python3
Python 3.8.6 (default, Oct 11 2020, 18:28:25)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> exit()
[root@VM-0-4-centos bin]#

我安装的第三库都在/root/python38/lib/python3.8/site-packages/文件夹下,跟python3在一起,不需要直接就可以整个文件夹删除

最后修改:2020 年 10 月 15 日
如果觉得我的文章对你有用,请随意赞赏