Centos——升级Python2.7及安装pip

时间:2021-07-26 09:21:56

CentOS升级Python2.7及安装pip

1) 升级Python2.7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
python -V  # 查看版本:Python 2.6.6
mkdir -p ~/Env/python; cd ~/Env/python  # 创建个目录
wget --no-check-certificate https://www.python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz
tar Jxvf Python-2.7.6.tar.xz
cd Python-2.7.6
./configure --prefix=/usr/local/py-2.7.6  # "./configure -h"查看帮助
make  # 报模块缺失时,有需要的安装后重make
# issue: INFO: Can't locate Tcl/Tk libs and/or headers
#        Python build finished, but the necessary bits to build these modules were not found:
#        ...
# 注1:zlib必要,之后安装setuptools要用。见"安装setuptools"
# 注2:openssl必要,之后pip要用。见"安装pip"
# 注3:bzip2也最好加,执行"yum install bzip2 bzip2-devel -y"
make install
# 建立软链接,默认指向Python2.7
mv /usr/bin/python /usr/bin/python2.6.6
ln -s /usr/local/py-2.7.6/bin/python2.7 /usr/bin/python
python -V  # Python 2.7.6
# yum不兼容Python2.7,需要指定为原版本
vi /usr/bin/yum
# 将"!/usr/bin/python"改为"!/usr/bin/python2.6.6"
# ibus也不兼容Python2.7,需要修改
# 将"exec python"改为"exec python2.6.6"
ll /usr/bin | grep python  # 确认下python2.6.6
vi /usr/bin/ibus-setup  # 修改
vi /usr/libexec/ibus-ui-gtk  # 修改
reboot  # 可能需要重启

2) 准备基础环境

2.1) 安装setuptools

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
cd ~/Env/python
wget http://pypi.douban.com/packages/source/s/setuptools/setuptools-3.3.tar.gz  # 豆瓣源
tar zxvf setuptools-3.3.tar.gz
cd setuptools-3.3
python setup.py build
sudo python setup.py install
# 或者用ez_setup.py,同样改为豆瓣源
# 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/
# 缺少zlib,安装setuptools时出错。
# issue: RuntimeError: Compression requires the (missing) zlib module
yum install zlib zlib-devel -y
# 重make Python2.7再安装
cd ../Python-2.7.6
make  # 这时才注意先前make时缺了好多模块
make install
# 环境变量
vi /etc/profile
# 添加如下内容:
# PY_HOME=/usr/local/py-2.7.6
# export PATH=$PY_HOME/bin:$PATH
source /etc/profile  # 当前终端生效,reboot后才会完全生效
echo $PATH

2.2) 安装pip

1
2
3
4
5
6
7
8
9
10
11
12
13
14
easy_install -i http://pypi.douban.com/simple pip
# 缺少ssl模块,pip使用时出错
# issue: ImportError: cannot import name HTTPSHandler
yum install openssl openssl-devel -y
make  # Python-2.7.6目录
make install
# 配置pip为豆瓣源
mkdir ~/.pip  # ls或ll -a 查看隐藏内容
vi ~/.pip/pip.conf
# 添加如下内容:
# [global]
# index-url = http://pypi.douban.com/simple

2.3) 安装virtualenv

1
2
3
su
pip install virtualenv
pip install virtualenvwrapper

virtualenv用于创建隔离的Python运行环境,依赖不同库和版本时避免混乱。

virtualenvwrappervirtualenv上的扩展,提供更方便的命令。

2.4) 其他

Document

Network

Web framework