1. 安装python3

1.1 下载python3安装包及其依赖包(该步骤可忽略,步骤1.2 提供应用包链接

 ① 在python官网下载所需的python3,或者用外网centos机器的wget命令下载:

[root@ZWZF-CWY-LZY-12 ~]# cd /opt  #在opt目录下载python3
[root@ZWZF-CWY-LZY-12 opt]# wget --no-check-certificate https://www.python.org/ftp/python/3.9.7/Python-3.9.7.tgz

  ② 在外网centos7机器上,下载python3依赖安装包

  安装yum-plugin-downloadonly软件包
[root@ZWZF-CWY-LZY-12 opt]# yum install yum-plugin-downloadonly

   安装python3依赖包

#创建依赖包缓存目录 
[root@ZWZF-CWY-LZY-12 opt]# mkdir -p /opt/python3 
#下载python3依赖安装包 
[root@ZWZF-CWY-LZY-12 opt]# cd /opt/python3
[root@ZWZF-CWY-LZY-12 python3]# yum reinstall --downloadonly --downloaddir=/opt/python3 zlib-devel bzip2-devel openssl-devel ncurses-devel epel-release gcc gcc-c++ xz-devel readline-devel gdbm-devel sqlite-devel tk-devel db4-devel libpcap-devel libffi-devel

1.2 离线安装python3及其依赖安装包

  ① 将外网centos7机器上,/opt目录下的Python-3.9.7.tgz和python3依赖包文件拷贝到内网centos服务器/opt目录下

   python3安装包及其依赖包下载链接: https://pan.baidu.com/s/1wSrzIMRGqkxjx0mGRh9MmQ 提取码: sbiw 

  ② 在内网centos服务器上,离线安装python3依赖安装包

# 进入python3依赖包路径,执行如下命令:
rpm -Uvh ./*.rpm --nodeps --force

  ③ 编译安装python3安装包

cd /opt
tar -zxvf  Python-3.9.7.tar.xz  #解压安装包
mkdir /usr/local/python3  # 创建编译安装目录
cd Python-3.9.7      # 进入python的解压目录
./configure --prefix=/usr/local/python3
make && make install   # 编译安装

  ④ 配置python3的软链接

ln -sf /usr/local/python3/bin/python3.9 /usr/bin/python3
ln -sf /usr/local/python3/bin/pip3.9 /usr/bin/pip3 

1.3 验证是否安装成功

python3 -V
pip3 -V

正常输出版本号,表示安装成功!


Linux系统默认装有python2.7,如果删除后再安装,可按如下步骤操作

2. 安装python2.7

2.1 安装GCC包

yum install gcc openssl-devel bzip2-devel

2.2 wget 下载 python 2.7 并解压

cd /usr/src
wget https://www.python.org/ftp/python/2.7.15/Python-2.7.15.tgz
tar -zxvf Python-2.7.15.tgz

2.3 安装python2.7

cd Python-2.7.15
./configure --enable-optimizations
make altinstall

2.4 配置python2的软链接

ln -sf /usr/src/Python-2.7.15/python  /usr/bin/python

2.5 查看python版本

python -V