linux centos7服务中自带的python版本为python2.7,这里通过python自带的python的os.system()自动换切换成python3。代码如下:
#coding=utf-8
import os
import sys
if os.getuid() == 0:
pass
else:
print "需要切换用户到root"
sys.exit(1)
#开始安装需要的驱动
file_path = "/home/file"
cmd = "cd " + file_path
r = os.system(cmd)
if r != 0:
print "打开文件系统失败"
sys.exit(1)
os.system("unzip python.zip")
os.system("cd python")
os.system("rpm -Uvh ./*.rpm --nodeps --force")
#安装python
res = os.system("tar -zxvf Python-3.6.8.tgz")
if res != 0:
print "解压失败"
sys.exit(1)
res1 = os.chdir('/home/file/Python-3.6.8')
res_path = os.getcwd()
print "当前工作路径:%s" % res_path
os.system("./configure --prefix=/usr/local/bin/python3")
os.system("make && make install")
#安装结果检测
ra = os.system("/usr/local/bin/python3/bin/python3 -V")
if ra != 0:
print "安装失败请重新安装"
sys.exit(1)
#环境变量设置
os.system("mv /usr/bin/python /usr/bin/python_bak")
os.system("ln -s /usr/local/bin/python3/bin/python3 /usr/bin/python")
os.system("ln -s /usr/local/bin/python3/bin/pip3 /usr/bin/pip")
rb = os.system("python -v")
if ra == 0:
print "安装成功"