1、构建实验环境

1.1、实验机的准备

实验用虚拟机的安装请参见:

《VMware — CentOS6.x 实验机安装配置的简单说明》

链接如下:

http://www.jianshu.com/p/624441bf5e48

本次实验使用的系统版本为:

CentOS release 6.8 (Final)

  1. 实验网络规划:
IP 用途
192.168.135.42 python 开发环境
  1. 应用目录创建
]# mkdir -p /apps01/{src,local}

1.2、软件环境的准备

安装依赖软件

]# yum -y install gcc-c++ zlib-devel openssl-devel readline-devel sqlite sqlite-devel

1.3、获取 python 2.7 的安装包

下载 python 2.7.11

注:

写这篇文档的时候,2.7最新版本为11,可以通过该地址查看最新版本:
https://www.python.org/ftp/python/

]# wget https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tar.xz -P /apps01/src

2、python 2.7 的安装、配置

2.1、解压

]# cd /apps01/src
]# tar -xf Python-2.7.11.tar.xz
]# cd Python-2.7.11

2.2、安装

]# ./configure --prefix=/apps01/local/python27/ --enable-shared
]# make && make install

注:

在执行 make 之后,虽然有以下提示,但所列项目都可以忽略。

Python build finished, but the necessary bits to build these modules were not found:
_tkinter           bsddb185           dl              
imageop            sunaudiodev                        
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
  • _tkinter:a thin object-oriented layer on top of Tcl/Tk。如果不使用桌面程序可以忽略TKinter
  • bsddb185:old bsddb module。老的bsddb模块,可忽略
  • dl:Call C functions in shared objects. Python2.6 开始,已经弃用
  • imageop:Manipulate raw image data 已经弃用
  • sunaudiodev:Access to Sun audio hardware。这个是针对Sun平台的,CentOS下可以忽略

2.3、替换系统自带 python

查看 python 命令的位置

]# whereis python
python: /usr/bin/python2.6 /usr/bin/python /usr/lib/python2.6 /usr/lib64/python2.6 /usr/include/python2.6 /usr/share/man/man1/python.1.gz

备份 python 命令

]# mv /usr/bin/python /usr/bin/python.`date +%F@%T`.bak

创建软连接

]# ln -s /apps01/local/python27/bin/python /usr/bin/python

2.4、解决 python2.7 启动报错

报错:

python2.7: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory

解决:

]# find / -name libpython2.7.so.1.0
/usr/local/src/Python-2.7.11/libpython2.7.so.1.0
/usr/local/python27/lib/libpython2.7.so.1.0

]# vim /etc/ld.so.conf.d/python2_7.conf
/apps01/local/python27/lib/

]# ldconfig -v

2.5、解决 yum 启动报错

报错:

There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:

   No module named yum

Please install a package which provides this module, or
verify that the module is installed correctly.

It's possible that the above module doesn't match the
current version of Python, which is:
2.7.11 (default, Apr 20 2017, 09:45:30) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)]

If you cannot solve this problem yourself, please go to 
the yum faq at:
  http://yum.baseurl.org/wiki/Faq

解决:

]# whereis yum
yum: /usr/bin/yum /etc/yum.conf /etc/yum /usr/share/man/man8/yum.8.gz

]# vim /usr/bin/yum
#!/usr/bin/python --> /usr/bin/python2.6

附A:安装 python 过程中的报错

注1:

如果不安装 zlib-devel,等到安装 setuptools 的时候,会报如下错误

Extracting in /tmp/tmp5GRJiW
Traceback (most recent call last):
  File "ez_setup.py", line 391, in 
    sys.exit(main())
  File "ez_setup.py", line 388, in main
    return _install(archive, _build_install_args(options))
  File "ez_setup.py", line 50, in _install
    with archive_context(archive_filename):
  File "/usr/local/python27/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "ez_setup.py", line 102, in archive_context
    archive.extractall()
  File "/usr/local/python27/lib/python2.7/zipfile.py", line 1040, in extractall
    self.extract(zipinfo, path, pwd)
  File "/usr/local/python27/lib/python2.7/zipfile.py", line 1028, in extract
    return self._extract_member(member, path, pwd)
  File "/usr/local/python27/lib/python2.7/zipfile.py", line 1082, in _extract_member
    with self.open(member, pwd=pwd) as source, 
  File "/usr/local/python27/lib/python2.7/zipfile.py", line 1010, in open
    close_fileobj=should_close)
  File "/usr/local/python27/lib/python2.7/zipfile.py", line 526, in __init__
    self._decompressor = zlib.decompressobj(-15)
AttributeError: 'NoneType' object has no attribute 'decompressobj'

注2:

如果不安装 openssl-devel,等到安装 pip 的时候,会报如下错误

Searching for pip
Reading https://pypi.python.org/simple/pip/
Download error on https://pypi.python.org/simple/pip/: unknown url type: https -- Some packages may not be found!
Couldn't find index page for 'pip' (maybe misspelled?)
Scanning index of all packages (this may take a while)
Reading https://pypi.python.org/simple/
Download error on https://pypi.python.org/simple/: unknown url type: https -- Some packages may not be found!
No local packages or download links found for pip
error: Could not find suitable distribution for Requirement.parse('pip')

注3:

如果不安装 readline-devel,等到使用 ipython 时,会有如下告警:

WARNING: Readline  services not available or not loaded.
WARNING: The  auto-indent feature requires the readline library

文章来源于互联网,如有雷同请联系站长删除:python — CentOS6.x 安装 Python 2.7

发表评论