Python script: Linux automatic installation of python2.7 or 3.6

Posted by Gulsaes on Fri, 27 Mar 2020 16:48:30 +0100

Python script: Linux automatic installation of python2.7 or 3.6

Preface

In the daily operation and maintenance process, in the deployment environment, it is often necessary to install or upgrade the python version. Here, a script is required for automatic installation and deployment.

The implementation idea of this script is to obtain the execution result of shell command through the python modules os and sys to install the python version automatically.

Specific script implementation

[root@install-python ~]# cat install_python.py 
#! /usr/bin/python2.7
#coding=utf-8

# When installing in Linux, pay attention to the installation dependency. For example, GCC, zlib devel, etc

import os
import sys

if os.getuid() == 0:
    pass
else :
    print "The current user is not root User, please use root Run by the user."
    sys.exit(1)

os.system("yum -y install gcc* zlib-devel openssl-devel")

version = raw_input("Please enter what you want to install python Version (2.7/3.6)")

if version == "2.7":
    url = "https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz"
elif version == "3.6":
    url = "https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz"
else :
    print "The currently entered version number is not standard or not supported. Please re-enter."
    sys.exit(2)

cmd = "wget " + url
res = os.system(cmd)
if res != 0 :
    print "File download failed. It may be due to network reasons. Please try again."
    sys.exit(3)

package_name = ""
if version == "2.7":
    package_name = "Python-2.7.14"
elif version == "3.6":
    package_name = "Python-3.6.4"
else :
    print "error."
    sys.exit(4)

cmd = "tar -xf " + package_name + ".tgz"
res = os.system(cmd)
if res != 0:
    os.system("rm " + package_name + ".tgz")
    print "Failed to decompress the source package. Please check the source package file."
    sys.exit(5)

# Note. / configure cannot be preceded by spaces
cmd = "cd " + package_name +  "&& ./configure --prefix=/usr/local/python --with-ssl && make && make install"
res = os.system(cmd)

if res !=0:
    print "Source package compilation and installation failed, please check again."
    sys.exit(6)

# After the installation, you need to adjust the soft connection file to use the newly installed python version as the default python version.
os.system("mv /usr/bin/python /usr/bin/python_old")
if version == "2.7":
    os.system("ln -s /usr/local/python/bin/python2.7 /usr/bin/python")
elif version == "3.6":
    os.system("ln -s /usr/local/python/bin/python3.6 /usr/bin/python")
else :
    print("Manual configuration required python environment variable")

print "If it is from python 2 Upgrade to python 3 For version, the following commands need to be modified and executed:"
print "sed -i '1c#!/usr/bin/python2' /usr/bin/yum"
print "sed -i '1c#!/usr/bin/python2' /usr/libexec/urlgrabber-ext-down"
[root@install-python ~]# 

The code in the screenshot overlaps, please note.

After upgrading the Python version, you need to pay attention to some changes in the configuration.

Topics: Python Linux zlib yum