CentOS升级Python2.7及安装pip


  1. ==编译安装python== > Centos6自带的python版本过低,yum无法安装2.7版本的python,需要自行编译。
yum install zlib zlib-devel -y #安装setuptools需要,编译python前安装,否则需要重新编译.

yum install openssl openssl-devel -y #安装pip需要.

yum install bzip2 bzip2-devel -y

wget  --no-check-certificate https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz

tar -zxvf Python-2.7.8.tgz

cd Python-2.7.8

./configure --prefix=/usr/local/py-2.7.8

make all

make install

make distclean

mv /usr/bin/python /usr/bin/python.bak

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

# yum不兼容Python2.7,需要指定为原版本

vi /usr/bin/yum

# 将"#!/usr/bin/python"改为"#!/usr/bin/python.bak"
  1. ==安装setuptools==
wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py

python ez_setup.py --download-base http://pypi.douban.com/packages/source/s/setuptools/

# 环境变量

vi /etc/profile

# 添加如下内容:

PY_HOME=/usr/local/py-2.7.8

export PATH=$PY_HOME/bin:$PATH

source /etc/profile  # 当前终端生效,reboot后才会完全生效

echo $PATH

  1. ==安装pip==
easy_install pip -i http://pypi.douban.com/simple 

# 配置pip为豆瓣源

mkdir ~/.pip  # ls或ll -a 查看隐藏内容

vi ~/.pip/pip.conf

# 添加如下内容:

[global]

index-url = https://pypi.douban.com/simple
  1. ==安装模块==
pip install MySQL-python

安装mysql-python报错:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 65: ordinal not in range(128)
解决:
yum install mysql-devel.x86_64 python-devel

如果pip的方式安装不了可以直接在网站下载源码安装:

https://pypi.python.org/pypi/MySQL-python
python setup.py build
python setup.py install

测试:

>>>import MySQLdb