一般centos系统自带的Python是2.6.6版本,由于版本太老有些好用的模块及功能需要Python2.7才支持。简单写一下如何升级Python2.6到Python2.7并能同步使用pip,以及通过pip安装Django。踩过不少坑,总结了下,按下面步骤逐步安装不然会有报错。
1、安装openssl openssl-devel如果没安装后面用pip安装软件包的时候可能会提示错误“pip is configured with locations that require TLS/SSL, however the ssl module in Python is not avail”
yum -y openssl openssl-devel
2、安装Python2.7
wget https://www.python.org/ftp/python/2.7.10/Python-2.7.10.tgz
tar -zxvf Python-2.7.10.tgz
cd Python-2.7.10
vim Modules/Setup.dist
将以下四行前面的注释(#)去掉,然后保存退出。该步骤配合步骤1安装ssl模块
SSL=/usr/local/ssl
_ssl _ssl.c
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl
-L$(SSL)/lib -lssl -lcrypto
./configure --prefix=/usr/local/python27
make&&make install
mv /usr/bin/python /usr/bin/python2.6.6
ln -s /usr/local/Python2.7/bin/python2.7 /usr/bin/python
vim /usr/bin/yum 修改第一行python为python2.6,保证yum能继续使用
3、安装setuptools
yum -y install zlib zlib-devel
cd Python-2.7.10
make&&make install (重新编译python)
下载setuptools:wget https://pypi.python.org/packages/a9/23/720c7558ba6ad3e0f5ad01e0d6ea2288b486da32f053c73e259f7c392042/setuptools-36.0.1.zip#md5=430eb106788183eefe9f444a300007f0
unzip setuptools-36.0.1.zip
cd setuptools-2.0
python setup.py install
4、安装pip
下载pip:wget https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz#md5=35f01da33009719497f01a4ba69d63c9
tar -zxvf pip-9.0.1.tar.gz
cd pip-9.0.1
python setup.py install
ln -s /usr/local/python27/bin/pip /usr/bin/pip
5、安装MySQLdb(用Python操作数据库的一个模块)
yum install -y MySQL-python #先安装2.6版本的MySQLdb
把2.6包下的MySQLdb相关文件copy到2.7的包目录下
cp -r /usr/lib64/python2.6/site-packages/MySQLdb /usr/local/python27/lib/python2.7/site-packages/
cp -r /usr/lib64/python2.6/site-packages/_mysql* /usr/local/python27/lib/python2.7/site-packages/
6、安装Django
pip install Django