Python环境安装脚本
2018-06-22 本文已影响0人
浪子彦卿L
根据选择安装3.6版本还是2.7版本
#coding=utf-8
import os
import sys
if os.getuid() == 0:
pass
else:
print("当前用户不是root用户, 请以root用户执行脚本")
sys.exit(1)
version = input("请输入你想安装的python版本(2.7/3.6): ")
if version == 2.7:
url = "https://www.python.org/ftp/python/2.7.12/Python-2.7.12.tgz"
elif version == 3.6:
#url = "https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz"
url = "https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz"
else:
print("您输入的版本号有误,请输入2.7或者3.5")
sys.exit(1)
# 安装相应的依赖包
cmd = "yum install zlib-devel openssl openssl-devel"
res = os.system(cmd)
if res != 0:
print("依赖包安装失败,请检查yum源")
sys.exit(1)
# 下载
cmd = "wget " + url
res = os.system(cmd)
if res != 0:
print("下载源码包失败,请检查网络")
sys.exit(1)
# 解压
if version == 2.7:
package_name = "Python-2.7.12"
else:
package_name = "Python-3.6.0"
cmd = "tar xf "+package_name+".tgz"
res = os.system(cmd)
if res != 0:
os.system("rm -r" + package_name+".tgz")
print("解压源码包失败,请重新运行这个脚本下载源码包")
sys.exit(1)
# 编译安装
cmd = "cd "+package_name+" && ./configure --prefix=/usr/local/python && make && make install "
res = os.system(cmd)
if res != 0:
print("编译python源码失败,请安装所需要的依赖库")
sys.exit(1)