离线安装python

0x00 背景

工作中会遇到服务器没有网络的情况,也需要使用python,这个时候提前打包并写好安装脚本就显得尤为重要。

0x01 实践

编写install.sh

function compile_py36(){
rm -fr /usr/local/python36
mkdir /usr/local/python36
tar -xvf Python-3.6.8.tar.xz -C /usr/local/python36/
cd /usr/local/python36/Python-3.6.8
./configure --with-ssl --prefix=/usr/local/python36 
make && make install
ln -s /usr/local/python36/bin/python3 /usr/bin/python3.6
ln -s /usr/local/python36/bin/python3 /usr/bin/python3
}

RED="echo -en \\E[4;31m"
GREEN="echo -en \\E[7;32m"
RESET="echo -en \\E[0;39m"

dir=`pwd`
s=`python3.6 -V`; if [[ $s == '' ]]
then
	compile_py36
else
	s1=`python3.6 -m pip -V`; 
	if [[ $s1 == '' ]]
	then 
		$RED && echo "检测到系统自带python3不可用,即将卸载系统自带python3" && $RESET
		yum remove python36 -y
		compile_py36
	else $RED && echo "检测到系统有python3.6环境,先尝试使用系统自带的python3.6,如果不可用请先执行yum remove python36 -y卸载再重新执行本脚本" && $RESET
	fi
fi

cd $dir   ##  安装包上传的目录

rpm -ivh libtirpc-0.2.4-0.16.el7.x86_64.rpm
rpm -ivh python3-3.6.8-18.el7.x86_64.rpm python3-libs-3.6.8-18.el7.x86_64.rpm python3-setuptools-39.2.0-10.el7.noarch.rpm python3-pip-9.0.3-8.el7.noarch.rpm
rpm -ivh python3-devel-3.6.8-18.el7.x86_64.rpm

s2=`python3.6 -m pip`; if [[ $s2 == '' ]]
then
python3.6 -m ensurepip
else echo "已安装pip" 
fi

我喜欢把安装脚本和离线包打包到一个压缩包,并用另一个脚本insallpy36.sh启动

unzip -o install_py.zip
cd install_py
chmod 550 install.sh
./install.sh

 

 

你可能感兴趣的:(安全开发,python,linux,开发语言)