问答详情
源自:1-2 python运维-自动化运维脚本编写

我要装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)


提问者:低调远行 2019-03-05 15:40

个回答

  • 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)