CentOS系统默认安装了python2.7.5
在安装python 3之前,可以先查看当前的python版本

[root@ianshuai ~]# python
Python 2.7.5 (default, Nov 16 2020, 22:23:17) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
安装python3
一、解决依赖关系

在 CentOS 7 中安装 Python 3.7.6之前,请确保系统中已经有了所有必要的依赖包否则会报错:

[root@ianshuai ~]# yum -y groupinstall development
[root@ianshuai ~]# yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
二、wget下载安装包(也可以在外部下载好,上传)
# 创建一个名为Python3的空文件,也可以不创建,在进行编译安装的时候会自动创建
[root@ianshuai ~]# mkdir /usr/local/python3
[root@ianshuai python3]# cd /usr/local/python3
[root@ianshuai python3]# wget https://www.python.org/ftp/python/3.7.6/Python-3.7.6.tgz
三、安装Python 3.7.6

3.1 解压

[root@ianshuai python3]# tar -xzvf Python-3.7.6.tgz
[root@ianshuai python3]# cd Python-3.7.6

3.2 执行编译安装

[root@ianshuai Python-3.7.6]# ./configure --prefix=/usr/local/python3
[root@ianshuai Python-3.7.6]# make && make install

3.3 创建软连接:

[root@ianshuai Python-3.7.6]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[root@ianshuai Python-3.7.6]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
四、添加到PATH环境变量
[root@ianshuai  Python-3.7.6]# cd /etc/profile.d
[root@ianshuai  profile.d]# echo 'export PATH=$PATH:/usr/local/python3/bin/' > python3.sh
五、验证安装是否成功
[root@ianshuai profile.d]# python3
Python 3.7.6 (default, Sep  2 2021, 09:36:18) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

成功了,完美散花

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐