我要装B一波 CentOS7 安装python的python脚本

来源:1-2 python运维-自动化运维脚本编写

低调远行

2019-03-05 15:40

#/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)


写回答 关注

2回答

  • qq_萧大宝_hDFGL0
    2020-04-04 22:39:29

    用Python语言写出来的脚本,去安装python,先有蛋后有鸡吗

  • qq_那么一丢丢_0
    2019-03-12 15:15:56

    大佬,帮你加了个缩进

    #/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.5':
            url = "https://www.python.org/ftp/python/2.7.16/Python-3.5.2.tgz"
            download(url)
        else:
            print("输入的版本号有误,请输入'2.7'或者'3.5'")
            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.5.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 'Devolopment Tools' && yum install -y zlib-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 /detc/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)


    qq_那么一...

    3.5和3.7傻傻分不清楚~烦请忽略

    2019-03-12 15:21:19

    共 1 条回复 >

python自动化运维篇

如何把运维的工作流程化、标准化、自动化已经是迫在眉睫的问题。

53346 学习 · 44 问题

查看课程

相似问题