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

yipeiwu_com6年前Python基础

前言

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

简单安装

因为Python 2.7.13以后版本会自动完善yum配置,所以不必参考以前的网上文章去修改其他地方

# 查看当前系统中的 Python 版本,返回 Python 2.6.6 为正常
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的小技巧。

# 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

下载Python,如 2.7.13

https://www.python.org/downlo...

https://www.python.org/ftp/py...

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

安装python2.7.13默认自带pip版本为9.0.1

PS C:UsersAdministrator> pip -V

pip 9.0.1 from c:python27libsite-packages (python 2.7)

安装Python后升级pippython -m pip install --upgrade pip

# 更换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设计】。

相关文章

用python结合jieba和wordcloud实现词云效果

用python结合jieba和wordcloud实现词云效果

0x00 前言 突然想做一个漏洞词云,看看哪些漏洞比较高频,如果某些厂商有漏洞公开(比如ly),也好针对性挖掘。就选x云吧(镜像站 http://wy.hxsec.com/bugs.ph...

python使用装饰器作日志处理的方法

装饰器这东西我看了一会儿才明白,在函数外面套了一层函数,感觉和java里的aop功能很像;写了2个装饰器日志的例子, 第一个是不带参数的装饰器用法示例,功能相当于给函数包了层异常处理,第...

python getpass实现密文实例详解

python getpass实现密文实例详解

getpass模块的使用: 在python中实现密码密文需要导入getpass模块,在python中要使用内置模块的话,需要使用import进行导入,比如import getpass...

Windows下Eclipse+PyDev配置Python+PyQt4开发环境

Windows下Eclipse+PyDev配置Python+PyQt4开发环境

本文为大家分享了Windows下配置Python PyQt4开发环境的详细步骤,供大家参考,具体内容如下 1. 下载相关软件 Eclipse下载地址:http://www.eclipse...

Python使用pip安装报错:is not a supported wheel on this platform的解决方法

Python使用pip安装报错:is not a supported wheel on this platform的解决方法

本文讲述了Python使用pip安装报错:is not a supported wheel on this platform的解决方法。分享给大家供大家参考,具体如下: 可能的原因1:安...