准备环境:CentOS6.9机器

 

1.查看机器默认的Python版本

[root@hlmcent69nma ~]# python -V
Python 2.6.6
[root@hlmcent69nma ~]# whereis python
python: /usr/bin/python /usr/bin/python2.6 /usr/lib/python2.6 /usr/lib64/python2.6 /usr/local/bin/python /usr/include/python2.6 /usr/share/man/man1/python.1.gz

 

2.安装gcc

[root@hlmcent69nma ~]# yum install gcc -y

 

3.下载最新的Python2.7.x安装包,解压并进入指定目录

[root@hlmcent69nma ~]# wget https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz
[root@hlmcent69nma ~]# ll Python-2.7.14.tgz 
-rw-r--r--. 1 root root 17176758 Sep 17 02:05 Python-2.7.14.tgz
[root@hlmcent69nma ~]# tar zxvf Python-2.7.14.tgz
[root@hlmcent69nma ~]# cd Python-2.7.14
[root@hlmcent69nma Python-2.7.14]# ll
total 1000
-rw-r--r--.  1 1000 1000  10914 Sep 17 01:38 aclocal.m4
-rwxr-xr-x.  1 1000 1000  44259 Sep 17 01:38 config.guess
-rwxr-xr-x.  1 1000 1000  36515 Sep 17 01:38 config.sub
-rwxr-xr-x.  1 1000 1000 442959 Sep 17 01:38 configure
-rw-r--r--.  1 1000 1000 141572 Sep 17 01:38 configure.ac
drwxr-xr-x. 22 1000 1000   4096 Sep 17 01:38 Demo
drwxr-xr-x. 18 1000 1000   4096 Sep 17 01:55 Doc
drwxr-xr-x.  2 1000 1000   4096 Sep 17 01:38 Grammar
drwxr-xr-x.  2 1000 1000   4096 Sep 17 01:38 Include
-rwxr-xr-x.  1 1000 1000   7122 Sep 17 01:38 install-sh
drwxr-xr-x. 47 1000 1000  12288 Sep 17 01:38 Lib
-rw-r--r--.  1 1000 1000  12757 Sep 17 01:38 LICENSE
drwxr-xr-x. 11 1000 1000   4096 Sep 17 01:38 Mac
-rw-r--r--.  1 1000 1000  48553 Sep 17 01:38 Makefile.pre.in
drwxr-xr-x.  5 1000 1000   4096 Sep 17 01:38 Misc
drwxr-xr-x.  9 1000 1000   4096 Sep 17 01:38 Modules
drwxr-xr-x.  3 1000 1000   4096 Sep 17 01:38 Objects
drwxr-xr-x.  2 1000 1000   4096 Sep 17 01:38 Parser
drwxr-xr-x.  9 1000 1000   4096 Sep 17 01:38 PC
drwxr-xr-x.  2 1000 1000   4096 Sep 17 01:38 PCbuild
-rw-r--r--.  1 1000 1000  35170 Sep 17 01:38 pyconfig.h.in
drwxr-xr-x.  2 1000 1000   4096 Sep 17 01:38 Python
-rw-r--r--.  1 1000 1000  55670 Sep 17 01:38 README
drwxr-xr-x.  5 1000 1000   4096 Sep 17 01:38 RISCOS
-rw-r--r--.  1 1000 1000  99231 Sep 17 01:38 setup.py
drwxr-xr-x. 23 1000 1000   4096 Sep 17 01:38 Tools

 

4.配置,编译,编译安装

[root@hlmcent69nma Python-2.7.14]# ./configure --prefix=/usr/local/python/python2.7
[root@hlmcent69nma Python-2.7.14]# make
[root@hlmcent69nma Python-2.7.14]# make install

备注:
执行 ./configure --prefix=/usr/local/python/python3 命令。./configure命令执行完毕之后创建一个文件creating Makefile,供下面的make命令使用 执行make install之后就会把程序安装到我们指定的目录中去。Configure是一个可执行脚本,它有很多选项,在待安装的源码路径下使用命令./configure –help输出详细的选项列表。其中--prefix选项是配置安装的路径,如果不配置该选项,安装后可执行文件默认放在/usr /local/bin,库文件默认放在/usr/local/lib,配置文件默认放在/usr/local/etc,其它的资源文件放在/usr /local/share,比较凌乱。如果配置--prefix,如:./configure --prefix=/usr/local/test可以把所有资源文件放在/usr/local/test的路径中,不会杂乱。用了—prefix选项的另一个好处是卸载软件或移植软件。当某个安装的软件不再需要时,只须简单的删除该安装目录,就可以把软件卸载得干干净净;移植软件只需拷贝整个目录到另外一个机器即可(相同的操作系统)。当然要卸载程序,也可以在原来的make目录下用一次make uninstall,但前提是make文件指定过uninstall。

 

5.查看系统的Python版本,还是2.6.x

[root@hlmcent69nma ~]# python -V
Python 2.6.6

 

6.查看新安装的Python版本,当前系统的Python版本,并将系统指向的Python从2.6.x修改为2.7.x,再次查看当前系统的Python版本,已经变更为2.7.x

[root@hlmcent69nma ~]# /usr/local/python/python2.7/bin/python2.7 -V
Python 2.7.14
[root@hlmcent69nma ~]# /usr/bin/python -V
Python 2.6.6
[root@hlmcent69nma ~]# mv /usr/bin/python /usr/bin/python.bak
[root@hlmcent69nma ~]# ln -s /usr/local/python/python2.7/bin/python2.7 /usr/bin/python
[root@hlmcent69nma ~]# python -V
Python 2.7.14

 

7.但是发现yum命令无法使用了,由于yum是基于python2.6才能正常工作,需单独将yum指向python2.6版本

[root@hlmcent69nma ~]# yum list
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.14 (default, Nov 30 2017, 11:54:55) 
[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

[root@hlmcent69nma ~]# vim /usr/bin/yum

 

参考链接:

https://www.cnblogs.com/perallina/p/5253329.html

https://www.cnblogs.com/lclq/archive/2016/06/27/5620196.html

 

posted on 2017-12-01 20:40  铭的随记  阅读(6440)  评论(0编辑  收藏  举报