【CentOS 7.3】Python3 安装

CentOS 7.3 默认安装了 Python2.7 环境:

[root@localhost ~]# python
Python 2.7.5 (default, Nov  6 2016, 00:28:07) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

现在主流是 Python3,接下来再装⼀个 Python3,打造⼀个共存的环境。

1、准备Python3安装包并解压

我这里下载的是 Python-3.8.3.tgz 安装包,并将其直接放在了 /root 目录下,执行如下命令解压:

[root@localhost ~]# tar zxvf Python-3.8.3.tgz

得到文件夹: Python-3.8.3

2、安装相关预备环境

执行如下命令:

[root@localhost ~]# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make

3、编译并安装

指定安装目录为 /usr/local/python3,执行如下目录:

[root@localhost ~]# cd Python-3.8.3/
[root@localhost Python-3.8.3]# ./configure prefix=/usr/local/python3
[root@localhost Python-3.8.3]# make && make install

安装完毕,⽣成 /usr/local/python3 目录

4、添加软链接

将刚刚安装⽣成的目录 /usr/local/python3 ⾥的 python3 可执行文件做⼀份软链接,链接到 /usr/bin 下,方便后续使用python3

[root@localhost ~]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[root@localhost ~]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

5、验证安装

执行命令 python3 ,即可查看 Python3 版本的安装结果

[root@localhost ~]# python3
Python 3.8.3 (default, May  4 2021, 19:48:48) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

执行命令 python ,依然还是 python2.7.5 环境

[root@localhost ~]# python
Python 2.7.5 (default, Nov  6 2016, 00:28:07) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

你可能感兴趣的:(环境安装,python,linux,centos)