centos6 安装python2.7 并做软件兼容处理 及 MySQLdb模块安装

相关软件准备

https://www.python.org/ftp/python/2.7.15/Python-2.7.15.tgz

https://pypi.org/project/setuptools/

https://pypi.org/project/pip/pip-10.0.1.tar.gz

旧版本setuptools下载,建议下载最新的

http://distfiles.macports.org/py-setuptools/

将以上三个包wget到linux系统里,一般放到 /usr/local/src里

在centos6.6默认python版本为2.6,很多东西兼容不好,需要升级为2.7;

1,更新gcc:

yum -y install gcc gcc-c++ automake autoconf 

提示 :

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-devel

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. 下载并解压python, 编辑Modules安装文件

wget https://www.python.org/ftp/python/2.7.15/Python-2.7.15.tgz  -O /usr/local/src/Python-2.7.15.tgz
tar xf !$
cd !$
vim Modules/Setup.dist

修改内容如下

# Socket module helper for socket(2)
_socket socketmodule.c timemodule.c

# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
#SSL=/usr/local/ssl
_ssl _ssl.c 
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl 
-L$(SSL)/lib -lssl -lcrypto

4,进入python,编译安装:

./configure --prefix=/usr/local
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只支持2.6,如果不该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,重新编译安装即可。

如果pip install 报错为证书相关的问题,可以参靠下面的链接重新编译

http://www.cnblogs.com/yuechaotian/archive/2013/06/03/3115472.html

原文地址:https://www.cnblogs.com/demonxian3/p/9271747.html