linux安装他版本python时遇到的问题及解决方法
程序员文章站
2022-06-04 14:33:37
...
因为学习需要以及centos6.9中的python版本过低,所以更新了下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.5)')
if version == '2.7':
url = 'https://www.python.org/ftp/python/2.7.12/Python-2.7.12.tgz'
elif version == '3.5':
url = 'https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz'
else:
print '您输入的版本号有误,请输入2.7或者3.5'
sys.exit(1)
cmd = 'wget ' +url
res = os.system(cmd)
if res !=0:
print'下载源码失败,请检查网络'
sys.exit(1)
if version =='2.7':
package_name = 'Python-2.7.12'
else:
package_name = 'Python-3.5.2'
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)
然后需要修改下/usr/bin/python,请根据自己安装目录位置修改
rm -f /usr/bin/python
ln -s /usr/local/python/bin/python /usr/bin/python
因为修改了/usr/bin/python,所以需要修改下yum
vim /usr/bin/yum
#!/usr/bin/python2.6
我们会发现pip也不能用了,先去下载pip和setup-tools
wget https://pypi.python.org/packages/c4/44/e6b8056b6c8f2bfd1445cc9990f478930d8e3459e9dbf5b8e2d2922d64d3/pip-9.0.3.tar.gz#md5=b15b33f9aad61f88d0f8c866d16c55d8
wget https://pypi.python.org/packages/72/c2/c09362ab29338413ab687b47dab03bab4a792e2bbb727a1eb5e0a88e3b86/setuptools-39.0.1.zip#md5=75310b72ca0ab4e673bf7679f69d7a62
tar xvf pip-9.0.3.tar.gz
unzip setuptools-39.0.1.zip
#先安装setup-tools
cd setup-tools-39.0.1
python setup.py install
会遇到错误Compression requires the (missing) zlib module
解决方法:
yum install zlib
yum install zlib-devel
wget http://rpmfind.net/linux/RPM/index.html
#下载成功后,进入python2.7的目录,重新执行
cd Python-2.7.12
make
make install
重新执行,安装setup-tools
cd setup-tools-39.0.1
python setup.py install
安装pip
cd pip-9.0.3
python setup.py install
rm /usr/bin/pip
ln -s /usr/local/python/bin/pip /usr/bin/pip