第二章 Centos7下 Python3脚本安装

Posted 陪你去流浪

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第二章 Centos7下 Python3脚本安装相关的知识,希望对你有一定的参考价值。

一、编写Python脚本

[root@jindada /shell/scripts]$ vim Ins-Python3.sh
##########################################################################
# Author:          jin.hui
# QQ:              974089352
# File Name:       Ins-Python3.sh
# Copyright (C):   2021 All rights reserved
# Created Time:    2021年06月21日 星期一 16时49分53秒
#########python_version################################################################
#!/usr/bin/bash
#1.定义变量
soft_dir=/data/software
python_version=Python-$1
python_data_dir=/usr/local/python-$1
python_version_package=Python-"$1".tgz
python_download_link=https://www.python.org/ftp/python/$1/Python-"$1".tgz
array=(zlib zlib-devel bzip2 bzip2-devel ncurses ncurses-devel readline readline-devel  openssl openssl-devel openssl-static xz lzma xz-devel sqlite sqlite-devel gdbm gdbm-devel tk tk-devel db4-devel libpcap-devel libffi-devel epel-release )
		
#2.安装依赖
source /etc/init.d/functions
if [ $# -ne 1 ];then
	echo "/bin/sh $0 python_version_number"
	exit 1
fi

for info in ${array[*]}
do
	yum -y install $info;
	if [ $? -eq 0 ];then
		echo "$i is installed"; 
	else
		yum -y install $i  &>/dev/null;
		action "$i is installing"  /usr/bin/true;
	fi     
done

#3.下载Pyhton3安装包
if [ -d $soft_dir ];then
	cd $soft_dir && [ -f $python_version_package ] && echo "$python_version_package is Exist" || wget $python_download_link
else
	echo "$soft_dir not exist" && mkdir $soft_dir -p && cd $soft_dir && [ -f $python_version_package ] && echo "$python_version_package is Exist" || wget $python_download_link
fi

#4.解压安装包
[ -d /root/$python_version ] && rm -rf /root/$python_version || cd $soft_dir ; tar -zxvf $python_version_package -C /root

#5.创建python程序目录
[ -d $python_data_dir ] &&  rm -rf $python_data_dir || mkdir $python_data_dir

#6.生成Makefile文件
cd /root/$python_version && mkdir bld && cd bld && ../configure --prefix=$python_data_dir

#7.编译安装
make && make install

#8.做软连接
[ -L /usr/local/python3 ] && rm -rf /usr/local/python3 ;ln -s $python_data_dir /usr/local/python3 || ln -s $python_data_dir /usr/local/python3

#9.配置环境变量
echo \'export PATH=/usr/local/python3/bin:$PATH\' > /etc/profile.d/python3.sh

source /etc/profile.d/python3.sh

#9.升级pip
pip3 install --upgrade pip
pip3_version=`pip3 -V |awk \'{print $1" "$2}\'`
[ $? -eq 0 ] && echo "$pip3_version is  Update Success" || echo "$pip3_version is  Update Failed"

#10.Python3安装完成    
cat << EOF
+-------------------------------------------------+
| `python3 -V` 已 经 安 装 完 毕 ,         |
| 请 尽 情 享 受 代 码 的 美 好 !                 |
+-------------------------------------------------+
EOF

sleep 5

mv ./Ins-Python3.sh /tmp

二、添加执行权限

[root@jindada /shell/scripts]$ chmod +x  Ins-Python3.sh

三、运行脚本

[root@jindada /shell/scripts]$ sh  Ins-Python3.sh 3.8.6

以上是关于第二章 Centos7下 Python3脚本安装的主要内容,如果未能解决你的问题,请参考以下文章

centos7安装python3.9脚本

centos7 安装python3.6 脚本

centos7 一键升级python3

centos7 下安装python3后发现自带的2.75没有覆盖,怎么使用新版本

离线环境升级python及安装模块

python3——centos7下安装