[root@dn3 hadoop]# cat install.py
#!/usr/bin/python
#coding=utf-8
import os
import sys
if os.getuid() == 0:
pass
else:
print('当前用户不是root用户,请用root用户运行')
sys.exit(1)
version = raw_input('请输入您想要安装的Python版本(2.7/3.6)')
if version == '2.7':
url = 'https://www.python.org/ftp/python/2.7.16/Python-2.7.16.tgz'
elif version == '3.6':
url = 'https://www.python.org/ftp/python/3.6.9/Python-3.6.9.tgz'
else:
print '您输入的版本有误,请输入2.7或3.6'
cmd = 'wget '+url
res = os.system(cmd)
if res != 0:
print '下载失败,请重新下载'
sys.exit(1)
if version == '2.7':
package_name = 'Python-2.7.16'
else:
package_name = 'Python-3.6.9'
cmd = 'tar xf '+package_name+'.tgz'
res = os.system(cmd)
if res != 0:
os.system('rm '+package_name+'.tgz')
print '解压包失败!请重新解压'
sys.exit(1)
cmd = 'cd '+package_name+' && ./configure --prefix=/usr/local/python && make && make install'
res = os.system(cmd)
if res != 0:
print '编译Python源码失败,请检查是否缺少依赖。'
sys.exit(1)