centos 6 安装python2.7和pip

时间:2021-11-17 01:54:06

由于scrapy1.0的发布,想在Linux上测试,所以安装了centos6.6;但是它的python版本为2.6,很多东西都不支持,所以需要升级为2.7;

首先是在window7上安装虚拟机,然后安装centos6.6系统,最后更新python版本,具体的更新过程如下(中间走的弯路就不说了,直接给出正确的):

1,更新gcc:

yum -y install gcc

提示 :

Loaded plugins: fastestmirror, refresh-packagekit, security
Existing lock /var/run/yum.pid: another copy is running as pid 7172.
Another app is currently holding the yum lock; waiting for it to exit...
果断删除yum.pid(以root用户):rm /var/run/yum.pid

2,为了安装setuptools,要需要安装zlib:

yum install zlib zlib-dev  

2.1,为在python编译时加入ssl模块,yum安装openssl和openssl-devel:

yum install openssl

yum install openssl-devel

2.2,为了防止出现 import zlib找不到的情况,复制文件:

cp /usr/lib64/python2.6/lib-dynload/zlibmodule.so /usr/local/lib/python2.7/lib-dynload

 

3,下载python2.7.10,可直接到python官网下载,并选择相应版本;解压后文件夹改名为python

 

4,进入python,编译安装:

./configure 
make all
make install
make clean
make distclean

5,把系统自带的2.6移除(依然会保留2.6版本:/usr/bin/python2.6)

rm -f /usr/bin/python  

6,把python执行软连接连接到2.7

ln -s /usr/local/bin/python2.7 /usr/bin/python  

7,编辑yum命令,把路径指明为2.6,因为yum必须基于2.6版本

vi /usr/bin/yum 

把文件头部的#!/usr/bin/python改成#!/usr/bin/python2.6 
保存退出,yum即可正常使用。如若有其他命令、软件不能正常使用,仿照yum配置文件的修改方法,修改其配置文件即可。 
至此,更新完毕。

=====================================以下为pip的安装部分=================

8,安装setuptools,下载setuptools-18.0.1.tar.gz,解压后,进入setuptools-18.0.1,执行:

python setup.py install

9,安装pip,下载pip-7.1.0.tar.gz,解压后进入pip-7.1.0,执行:

python setup.py install

10,测试pip,如果报错“pkg_resources.DistributionNotFound: The 'pip==7.1.2' distribution was not found and is required by the application”,此时需要修改pip可执行程序:

mv /usr/bin/pip /usr/bin/pip0
cp /usr/bin/pip2.7 /usr/bin/pip

 

附:MySQLdb模块的安装方法:

简略方法:

pip install MySQL-python

安装后测试:

[root@iZ251ed9ao2Z ~]# python
Python 2.7.11 (default, Oct 18 2016, 09:34:16)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>

如果正常,则ok,否则按照以下步骤安装:

1,到官网下载源代码:MySQL-python-1.2.3.tar.gz

2,编译安装:cd MySQL-python-1.2.3 && python setup.py install

3,若出现错误:_mysql.c:2444: error: ‘_mysql_ConnectionObject’ has no member named ‘open’ ,则安装以下包:

yum -y install mysql-devel libxml2 libxml2-dev libxslt* zlib gcc openssl

4,重新编译安装即可。