RedHat6/Centos6系统中更新python版本

CentOS 6中,Python的版本默认为2.6.6版本,而我们有很多工作可能需要更高的版本,而去Python官网的时候发现,Python的版本已经到3.3了,于是下载并升级了下CentOS中Python的版本。

1,准备工作
将系统中自带的python2.6.6更名一下,因为yum也使用了系统自带的python,所以最好也将yum里指定一下使用的python版本。另外,需要提前安装openssl和openssl-devel,以防止编译安装Python时无法加载SSL模块。

$ python -V    #查看当前版本

$ mv /usr/bin/python /usr/bin/python2.6.6

$ vim /usr/bin/yum
将第一行的/usr/bin/python修改为/usr/bin/python2.6.6

$ sudo apt install gcc g++ make libssl-dev python3-openssl
$ yum install gcc gcc-c++ make openssl openssl-devel

# 安装相关依赖. 需要在make之前做, 不然在运行get-pip.py时
# 会遇到zipimport.ZipImportError: can't decompress data; zlib not available的错误
$ sudo apt-get install zlib1g-dev
$ yum install zlib zlib-devel

2,下载并安装新版python

3.5.5版下载地址
$ wget https://www.python.org/ftp/python/3.5.5/Python-3.5.5.tgz

2.7.5版下载地址
$ wget http://www.python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2

$ tar jxvf Python-3.5.5.tgz
$ cd Python-3.5.5
$ ./configure --enable-optimizations    #这里的--enable-optimizations是可以省略的
$ make && make install

3,更新链接

$ ln -s /usr/local/bin/python3.5 /usr/bin/python
$ python -V

4,更新setuptools和pip
setuptools和pip都是python的管理套件,更新了python以后,你会发现这些套件极有可能出现如下的错误

ImportError: No module named pkg_resources

ImportError: No module named setuptools

pkg_resources.DistributionNotFound: The 'pip==7.1.0' distribution was not found and is required by the application

比如下面这种情况

[root@zetawiki ~]# pip
Traceback (most recent call last):
  File "/usr/bin/pip", line 5, in 
    from pkg_resources import load_entry_point
ImportError: No module named pkg_resources

[root@zetawiki ~]# easy_install
Traceback (most recent call last):
  File "/usr/bin/easy_install", line 5, in 
    from pkg_resources import load_entry_point
ImportError: No module named pkg_resources

[root@zetawiki ~]# python -c 'import pkg_resources'
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named pkg_resources

[root@zetawiki ~]# python -c 'import setuptools'
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named setuptools

解决办法

#2017.12.12更新了新的解决办法
#ez_setup.py is deprecated and when using it setuptools will be pinned to 33.1.1 since it's the last version that supports setuptools self upgrade/installation
#check https://github.com/pypa/setuptools/issues/581 for more info
#Please use pip to install setuptools
#wget https://bootstrap.pypa.io/ez_setup.py -O - | python

wget https://bootstrap.pypa.io/get-pip.py -O - | python

然后再次尝试,就不会现出现上面的错误了

[root@zetawiki ~]# easy_install --version
setuptools 38.2.4 from /usr/local/lib/python3.3/site-packages (Python 3.3)
[root@zetawiki ~]# pip -V
pip 9.0.1 from /usr/local/lib/python3.3/site-packages (python 3.3)

此时可能pip套件仍不能使用。比如极有可能出现了下面的错误:

[root@zetawiki ~]# pip
Traceback (most recent call last):
  File "/usr/bin/pip", line 5, in 
    from pkg_resources import load_entry_point
  File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 3141, in 
  File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 3127, in _call_aside
  File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 3154, in _initialize_master_working_set
  File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 640, in _build_master
  File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 941, in require
  File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 828, in resolve
pkg_resources.DistributionNotFound: The 'pip==7.1.0' distribution was not found and is required by the application

解决办法

$ yum remove python-pip
$ easy_install pip

然后再次测试,就不会再有问题了,如下所示

[root@zetawiki ~]# which pip
/usr/local/bin/pip

[root@zetawiki ~]# mv /usr/bin/pip /usr/bin/pip.bak
[root@zetawiki ~]# ln -s /usr/local/bin/pip /usr/bin/pip

[root@zetawiki ~]# pip --version
pip 8.1.2 from /usr/local/lib/python3.3/site-packages/pip-8.1.2-py3.3.egg (python 3.3)

参考资料:
CentOS中升级Python到3.2
리눅스 pip, easy install 정상화

RedHat6/Centos6系统中更新python版本》上有2条评论

  1. 建议不要升级3.3
    python 2.X 和python 3.X是两个不同的大版本,差异还是很大的
    建议升级到2.7

发表评论

电子邮件地址不会被公开。