"""
Centos7 自动编译安装Python3.7.3
命令:
python37
pip37
"""
import os
# 可执行的平台
os_platform = ["Centos7"]
def color_red(context):
"""红色"""
return '\033[1;31;40m{}\033[0m'.format(context)
def color_green(context):
"""绿色"""
return '\033[1;32;40m{}\033[0m'.format(context)
def os_install_dependence(os_name="Centos7"):
if "Centos7" in os_platform:
print(color_green("正在安装依赖包"))
res = os.system("yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel zlib* openssl-devel -y")
return res
# 如果执行成功,会返回0
else:
msg = color_red("脚本在不支持 {} 中Python的安装".format(os_name))
print(msg)
return -1
def os_install_python():
print(color_green("正在解压Python.tgz..."))
package_name = "Python-3.7.3"
cmd_unzip = 'tar xf ' + package_name+ '.tgz'
res_unzip = os.system(cmd_unzip)
if res_unzip != 0:
print("Python-3.7.3.tgz 解压失败")
return -1
cmd_python = 'cd '+ package_name+ ' && ./configure --prefix=/usr/local/python37 && make && make install'
print(color_green("正在编译安装python3.7.3..."))
res_python = os.system(cmd_python)
# 注释掉,这里和预期不符
#if cmd_python != 0:
# print("编译安装失败,请检查是否缺少依赖")
# return -1
def check_isinstall():
print(color_green("正在检查环境..."))
return os.path.exists("/usr/local/python37")
def create_link():
print(color_green("正在创建软链接..."))
cmd = "ln -s /usr/local/python37/bin/python3 /usr/bin/python37"
os.system(cmd)
cmd = "ln -s /usr/local/python37/bin/pip3 /usr/bin/pip37"
os.system(cmd)
def clear_trash():
print(color_green("正在清理残余垃圾..."))
cmd = "rm -rf ./Python-3.7.3"
os.system(cmd)
def main():
if check_isinstall():
print("/usr/local/python37 已存在")
return
else:
res_dep = os_install_dependence()
res_python= os_install_python()
create_link()
clear_trash()
if __name__ == "__main__":
main()
脚本安装Python3.7.3
猜你喜欢
转载自blog.csdn.net/sunt2018/article/details/90752918
今日推荐
周排行