python升级
背景
在安装之前,系统自带的python执行环境如下:
[root@ansibleserver bin]# pwd /usr/bin [root@ansibleserver bin]# ls -l python* -rwxr-xr-x 2 root root 9176 Sep 12 2011 python lrwxrwxrwx. 1 root root 6 Dec 16 14:37 python2 -> python -rwxr-xr-x 2 root root 9176 Sep 12 2011 python2.6 -rwxr-xr-x 1 root root 1418 Sep 12 2011 python2.6-config lrwxrwxrwx 1 root root 16 Dec 17 00:57 python-config -> python2.6-config |
1、下载python的安装包
python的安装包下载地址如下:
https://www.python.org/ftp/python/
在其中可以看到各种压缩包,截图如下:
--在其中可以看到两种格式的后缀,一种是正常的压缩包,而已asc结尾的表示为密钥文件,在下载过来,可以用来验证压缩包的正确性和安全性,使用的命令为:
gpg --verify 【sigfile】 【signed-files】
2、解压安装
上传到服务器之后,进行解压,如下:
# tar -zxvf Python-2.7.9.tgz |
进行编译:
# ./configure --prefix=/usr/local/ |
安装:(在make的时候jobs参数,表示使用处理器的个数来同时运行,可以使用man make查看)
# make --jobs=`grep processor /proc/cpuinfo |wc -l` |
# make install |
3、备份旧版本的python
将旧版本的进行更名备份:
# cd /usr/bin # mv python python2.6.6 |
将新版本的python创建链接:
ln -s /usr/local/bin/python |
可以看到新版本和旧版本同时存在,如下图所示:
4、验证
直接输入python,查看python的版本:
# python Python 2.7.9 (default, Dec 22 2015, 02:04:23) [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> |
可以看到版本已经升级,如果需要使用旧版本,如下所示:
# python2.6 Python 2.6.6 (r266:84292, Sep 12 2011, 14:03:14) [GCC 4.4.5 20110214 (Red Hat 4.4.5-6)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> |
5、安装setuptools模块
https://pypi.python.org/packages/source/s/setuptools/setuptools-7.0.tar.gz # tar xvzf setuptools-7.0.tar.gz # cd setuptools-7.0 # python setup.py install
|
6、问题
6.1 YUM无法使用
在升级之后,yum会出现问题如下:
解决方法如下:
修改文件/usr/bin/yum,在修改之前内容如下:
#!/usr/bin/python |
修改之后的内容如下:
#!/usr/bin/python2.6 |
主要就是使用老版本的python,从而解决此问题
6.2 python setup.py install无法使用
在安装模块的时候,使用pythonsetup.py install的时候,报错如下:
Traceback (most recent call last): File "/usr/bin/ansible", line 4, in <module> import pkg_resources ImportError: No module named pkg_resources |
这个主要是要进行重新安装相关模块,首先必须安装setuptools
6.3 无法安装setuptools模块
升级python 安装 setuptools报错Compression requires the (missing) zlib module,具体报错内容如下:
creating 'dist/setuptools-0.6c11-py2.7.egg' and adding 'build/bdist.linux-x86_64/egg' to it Traceback (most recent call last): File "setup.py", line 94, in <module> scripts = scripts, File "/usr/local/lib/python2.7/distutils/core.py", line 152, in setup dist.run_commands() File "/usr/local/lib/python2.7/distutils/dist.py", line 953, in run_commands self.run_command(cmd) File "/usr/local/lib/python2.7/distutils/dist.py", line 972, in run_command cmd_obj.run() File "/opt/oracle/sor/install/setuptools-0.6c11/setuptools/command/install.py", line 76, in run self.do_egg_install() File "/opt/oracle/sor/install/setuptools-0.6c11/setuptools/command/install.py", line 96, in do_egg_install self.run_command('bdist_egg') File "/usr/local/lib/python2.7/distutils/cmd.py", line 326, in run_command self.distribution.run_command(command) File "/usr/local/lib/python2.7/distutils/dist.py", line 972, in run_command cmd_obj.run() File "/opt/oracle/sor/install/setuptools-0.6c11/setuptools/command/bdist_egg.py", line 236, in run dry_run=self.dry_run, mode=self.gen_header()) File "/opt/oracle/sor/install/setuptools-0.6c11/setuptools/command/bdist_egg.py", line 527, in make_zipfile z = zipfile.ZipFile(zip_filename, mode, compression=compression) File "/usr/local/lib/python2.7/zipfile.py", line 681, in __init__ "Compression requires the (missing) zlib module" RuntimeError: Compression requires the (missing) zlib module |
解决方法:
安装zlib-devel包即可,如下:
[root@ansibleserver Python-2.7.9]# rpm -qa |grep zlib zlib-1.2.3-27.el6.x86_64 [root@ansibleserver Python-2.7.9]# yum -y install zlib-devel |
验证是否安装完成:
[root@ansibleserver Python-2.7.9]# rpm -qa |grep zlib zlib-1.2.3-27.el6.x86_64 zlib-devel-1.2.3-27.el6.x86_64 |
在安装完包之后,必须重新进行编译安装python,否则依旧无法使用