我要装B一波 CentOS7 安装python的python脚本
#/bin/python
#coding=utf8
import os,sys
def choiceVersion(version):
if version == "2.7":
url = "https://www.python.org/ftp/python/2.7.16/Python-2.7.16.tgz"
download(url)
elif version == "3.7":
url = "https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz"
download(url)
else:
print("输入的版本号有误,请输入2.7或3.7")
install(version)
def download(url):
try:
cmd = "wget "+url
os.system(cmd)
except:
print("下载源码包失败,请检查当前网络")
sys.exit(1)
def install(version):
if version == "2.7":
package_name = "Python-2.7.16"
else:
package_name = "Python-3.7.2"
try:
cmd = "tar -xf "+package_name+".tgz"
os.system(cmd)
except:
print("解压源码包失败,请重新运行脚本下载源码包")
os.system("rm -rf "+package_name+".tgz")
sys.exit(1)
path = os.getcwd()
try:
dependency = "yum groupinstall -y 'Development Tools' && yum install -y zlib-devel bzip2-devel openssl-devel readline-devel libffi-devel"
os.system(dependency)
cmd = "cd "+path+"/"+package_name+" && ./configure && make && make install"
os.system(cmd)
except:
print("Python源码安装失败,请检查是否缺少依赖库")
sys.exit(1)
environmentalVariable = "echo \"PATH=$PATH:/usr/local/bin\" >> /etc/profile"
os.system(environmentalVariable)
source = "source /etc/profile"
os.system(source)
if __name__ == "__main__":
if os.getuid() == 0:
pass
else:
print("当前用户不是root,请使用root执行脚本 ")
sys.exit(1)
version_float = input("请输入你想安装的python版本 2.7或者3.7 ")
version_string = str(version_float)
choiceVersion(version_string)