首页 > 代码库 > centos安装Python2.7

centos安装Python2.7

<script type="text/javascript">//
‘); (window.slotbydup=window.slotbydup || []).push({ id: ‘1111918‘, container: s, size: ‘200,200‘, display: ‘inlay-fix‘ });})();// ]]></script>
 
<script charset="utf-8" type="text/javascript" src="http://pos.baidu.com/fcfm?di=1111918&dri=0&dis=0&dai=4&ps=398x10&dcb=BAIDU_SSP_define&dtm=SSP_JSONP&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1479511189685&ti=centos6.7%E5%AE%89%E8%A3%85python2.7.11_Python_%E7%AC%AC%E4%B8%83%E5%9F%8E%E5%B8%82&ari=2&dbv=2&drs=1&pcs=772x455&pss=1000x623&cfv=0&cpl=5&chi=1&cce=true&cec=UTF-8&tlm=1479511190&rw=472&ltu=http%3A%2F%2Fwww.th7.cn%2FProgram%2FPython%2F201602%2F767902.shtml&ltr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DpZHhXbQCYeZTEheq7F-x_vWH8W4nTn2GHhaJuDgB28WmV0juMwOu_3d2dzNfcHWFhexIAApLOiA6M2aEYa3Hd_%26wd%3D%26eqid%3Db28349c50003db2800000003582f8c8a&lcr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DpZHhXbQCYeZTEheq7F-x_vWH8W4nTn2GHhaJuDgB28WmV0juMwOu_3d2dzNfcHWFhexIAApLOiA6M2aEYa3Hd_%26wd%3D%26eqid%3Db28349c50003db2800000003582f8c8a&ecd=1&psr=1366x768&par=1366x728&pis=-1x-1&ccd=24&cja=false&cmi=7&col=zh-CN&cdo=-1&tcn=1479511190"></script><script type="text/javascript">// </script><script type="text/javascript" src="http://baidu.th7.cn/cpro/ui/c.js"></script>
 

 

 

 

 

 

 

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

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

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

# python -VPython 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 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版本

# shsh-4.1# python -VPython 2.7.11sh-4.1# exitexit# pythonPython 2.6.6 (r266:84292, Jul 23 2015, 15:22:56) [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>>

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

# pythonPython 2.7.11 (default, Feb 24 2016, 09:50:38) [GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2Type "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 pythonpython: /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 方向键不能用问题, 情况如下:

# pythonPython 2.7.11 (default, Feb 24 2016, 09:50:38) [GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2Type "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!!!

centos安装Python2.7