前言

CentOS 6.8 安装 Python 2.7.13,因为软件版本上的需求所以考虑将 Python 升级至 2.7.14,加上生产环境还是以 RHEL 6 为主,互联网自动化运维平台大多数也推荐以Python 2.7.x + CentOS 6.x 来操作,选择 Python 2 还是 Python 3 也没有定论,找到适合的搭配即可。

Python 2.6 升级至 Python 2.7 的实践心得

更新历史

2018年04月12日 - 升级Python 2.7.14
2017年07月11日 - 完善Windows下Python的部署细节
2017年04月26日 - 初稿

阅读原文 - https://wsgzao.github.io/post/python-2-6-update-to-2-7/

扩展阅读

Python - https://www.python.org/
PyPI - https://pypi.python.org/pypi
pip - https://pip.pypa.io/en/stable/user_guide/#configuration


简单安装

Python 2.7.13以后版本可以正常执行编译安装,不必参考网上文章去修改其他地方(很抱歉我不知道Python2.7.x为什么不需要像Python3那样调整)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 针对Python3调整安装目录
./configure --prefix=/usr/local/python3.6
make && make install
# 先修改老的连接,执行
mv /usr/bin/python /usr/bin/python2.6
# 再建立新连接
ln -s /usr/local/python3.6/bin/python3.6 /usr/bin/python
# 解决升级后YUM无法使用
vim /usr/bin/yum

#!/usr/bin/python 修改为 #!/usr/bin/python2.6

# 测试是否修复
yum repolist

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
# 查看当前系统中的 Python 版本,返回 Python 2.6.6 为正常,rhel7以上版本为2.7.5
python --version

Python 2.6.6

# 检查 CentOS 版本,返回 CentOS release 6.8 (Final) 为正常
cat /etc/redhat-release

CentOS release 6.8 (Final)

# 安装所有的开发工具包
yum groupinstall -y "Development tools"
# 安装其它的必需包
yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel

# 下载、编译和安装 Python 2.7.13
#wget https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz
tar zxf Python-2.7.13.tgz
cd Python-2.7.13
./configure
make && make install

# 查看新的 Python 版本,返回 Python 2.7.13 为正常
python --version

Python 2.7.13

ansible离线安装实践

这里利用了一些内网离线安装ansible的小技巧

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Install Python
tar xf Python-2.7.13.tgz
cd Python-2.7.13
./configure
make
make install

# ImportError: No module named six.moves
tar xf six-1.10.0.tar.gz
cd six-1.10.0
python setup.py install
cd ..

# ImportError: No module named packaging.version
tar xf packaging-16.8.tar.gz
cd packaging-16.8
python setup.py install
cd ..
# ImportError: No module named pyparsing
tar xf pyparsing-2.2.0.tar.gz
cd pyparsing-2.2.0
python setup.py install
cd ..

# ImportError: No module named appdirs
tar xf appdirs-1.4.3.tar.gz
cd appdirs-1.4.3
python setup.py install
cd ..

# Install Setuptools
unzip setuptools-34.2.0.zip
cd setuptools-34.2.0
python setup.py install
cd ..

# Install pip
tar xf pip-9.0.1.tar.gz
cd pip-9.0.1
python setup.py install
cd ..

# pip 离线下载依赖包
pip install -d ~/tmp/ ansible
pip install -d ~/tmp/ ansible-cmdb

# pip 离线安装
pip install ansible --index-url=http://172.31.197.1:7000/simple/ --trusted-host=172.31.197.1
pip install ansible-cmdb --index-url=http://172.31.197.1:7000/simple/ --trusted-host=172.31.197.1

# 查看安装好的版本
[root@ansible-wangao ansible]# pip -V
pip 9.0.1 from /usr/local/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg (python 2.7)

[root@ansible-wangao ansible]# ansible --version
ansible 2.3.0.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides
python version = 2.7.13 (default, Apr 25 2017, 17:19:23) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]

[root@ansible-wangao ansible]# ansible-cmdb --version
ansible-cmdb v1.20

# 更换豆瓣pip源
vim $HOME/.pip/pip.conf

[global]
index-url = https://pypi.doubanio.com/simple/
[install]

trusted-host=pypi.doubanio.com

Windows 安装 Python 2.7.x

Windows下安装扩展包可能没有想象中那么顺利,遇到安装不上的包推荐去下面这个网址看看

http://www.lfd.uci.edu/~gohlke/pythonlibs/

一般情况先安装编译库Microsoft Visual C++ Compiler for Python 2.7
https://www.microsoft.com/en-us/download/details.aspx?id=44266

下载Python,如 2.7.13
https://www.python.org/downloads/
https://www.python.org/ftp/python/2.7.13/python-2.7.13.msi

安装时注意勾选环境变量
Add python.exe to Path

如果忘记勾选也没关系,可以自行在PATH添加环境变量
右键计算机【属性】->【高级系统设置】->【环境变量】->【系统变量】->【PATH】
D:\Python27;D:\Python27\Scripts;

安装python2.7.13默认自带pip版本为9.0.1
PS C:\Users\Administrator> pip -V
pip 9.0.1 from c:\python27\lib\site-packages (python 2.7)

安装Python后升级pip
python -m pip install –upgrade pip

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 更换pypi国内源,运行以下python代码会自动建立pip.ini
import os

ini="""[global]
index-url = https://pypi.doubanio.com/simple/
[install]
trusted-host=pypi.doubanio.com
"""
pippath=os.environ["USERPROFILE"]+"\\pip\\"

if not os.path.exists(pippath):
os.mkdir(pippath)

with open(pippath+"pip.ini","w+") as f:
f.write(ini)

Python 2.7.14

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Install Packages
yum install gcc zlib zlib-devel openssl-devel -y

# Install Python
tar xf Python-2.7.14.tgz
cd Python-2.7.14
./configure
make
make install
cd ..

# ImportError: No module named six.moves
tar xf six-1.11.0.tar.gz
cd six-1.11.0
python setup.py install
cd ..

# ImportError: No module named packaging.version
tar xf packaging-17.1.tar.gz
cd packaging-17.1
python setup.py install
cd ..

# ImportError: No module named pyparsing
tar xf pyparsing-2.2.0.tar.gz
cd pyparsing-2.2.0
python setup.py install
cd ..

# ImportError: No module named appdirs
tar xf appdirs-1.4.3.tar.gz
cd appdirs-1.4.3
python setup.py install
cd ..

# Install Setuptools
unzip setuptools-38.5.2.zip
cd setuptools-38.5.2
python setup.py install
cd ..

# Install pip
tar xf pip-9.0.1.tar.gz
cd pip-9.0.1
python setup.py install
cd ..

# pip 离线下载
# pip install --download DIR -r requirements.txt
pip install -d ~/ansible/ ansible

# pip 离线安装
# pip install --no-index --find-links=DIR -r requirements.txt
pip install --no-index --find-links=pip-ansible-2.3.3.0/ -r requirements.txt

# pip 离线安装pipenv
pip install --no-index --find-links=pip-pipenv/ pipenv

# 使用pipenv创建虚拟环境
mkdir win_ansible
cd win_ansible
pipenv shell
pip install --no-index --find-links=pip-ansible-2.4.3.0/ -r requirements.txt

# 退出虚拟环境
exit

# RuntimeError: Compression requires the (missing) zlib module
yum install zlib zlib-devel -y
cd Python-2.7.14
make
make install

# pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
yum install openssl-devel -y
cd Python-2.7.14
make
make install

文章目录
  1. 1. 前言
  2. 2. 更新历史
  3. 3. 简单安装
  4. 4. ansible离线安装实践
  5. 5. Windows 安装 Python 2.7.x
  6. 6. Python 2.7.14