centos6.7安装python2.7.11

1. 查看本机系统及python版本

# cat /etc/redhat-release 
CentOS release 6.7 (Final)

查看CentOS release 6.7 (Final)自带的python版本

# python -V
Python 2.6.6

注意, 我们不要破坏系统的Python环境, 因为几个关键的实用应用程序依赖于系统默认的Python 2.6.6, 如果破坏了系统的Python环境就会发生很多难以预见的错误,导致要重装系统。

2. yum安装开发工具库集及一些额外包包

# yum groupinstall "Development tools"
# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

3. 下载并解压python2.7.11源码包

# wget -c --no-check-certificate https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tar.xz
# tar xf Python-2.7.11.tar.xz
# cd Python-2.7.11

4. 编译并安装python2.7.11

# ./configure --prefix=/usr/local
# make && make altinstall

其中, ./configure --prefix=是指定安装路径, 按照官方文档,如果要将多个Python运行时安装在同一目录, 则使用make altinstall而不是make install

(注意: 不指定prefix,则可执行文件默认放在/usr /local/bin,库文件默认放在/usr/local/lib,配置文件默认放在/usr/local/etc。其它的资源文件放在/usr /local/share。你要卸载这个程序,要么在原来的make目录下用一次make uninstall(前提是make文件指定过uninstall),要么去上述目录里面把相关的文件一个个手工删掉。
指定prefix,直接删掉一个文件夹就够了)

5. 为python2.7执行文件创建软链接

# ln -s /usr/local/bin/python2.7 /usr/local/bin/python

/usr/local/bin/python指向/usr/local/bin/python2.7

6. 检测python版本

# sh
sh-4.1# python -V
Python 2.7.11
sh-4.1# exit
exit
# python
Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

我们发现, 在sh下检测到Python的版本是2.7.11, 但终端下仍然是默认的2.6.6, 这是因为要重启终端才生效, 我们重启终端后:

# python
Python 2.7.11 (default, Feb 24 2016, 09:50:38) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

python版本默认是我们新安装的Python 2.7.11, 这是因为在环境变量PATH中/usr/local/bin/usr/bin前面,其 优先级高:

# echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/java/jdk1.8.0_60/bin:/root/bin

或看看当前使用的是哪个python:

# which python
/usr/local/bin/python

查看python路径:

# whereis python
python: /usr/bin/python2.6 /usr/bin/python /usr/lib/python2.6 /usr/lib64/python2.6 /usr/local/bin/python /usr/local/bin/python2.7-config /usr/local/bin/python2.7 /usr/local/lib/python2.7 /usr/include/python2.6 /usr/share/man/man1/python.1.gz

7. 解决Delete Backspace 方向键不能用问题

若发现, 在python 命令行下, Delete Backspace 方向键不能用问题, 情况如下:

# python
Python 2.7.11 (default, Feb 24 2016, 09:50:38) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> ex^[[D^[[D^[[C^[[C^[[C^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^[[3~^[[3~^[[3~^C

这是因为在编译安装python前, 没有安装readline-devel所致。
解决方法是先安装readline-devel, 再回到源码包路径 下, 重新执行命令安装, 即可解决, 如下:

# yum install readline-devel
# cd Python-2.7.11
# make && make altinstall

到此, 我们完成了centos6.7下python2.7.11的安装!
OK, Enjoy it!!!

你可能感兴趣的:(Linux,python,centos)