很早以前就想着要写博客,因为做了很多事情,总是记不住,能写下来,就当是日记了.
我这次想要在我的centos7里面安装python3.6,并且能与原来的2.7兼容.
1、查看原来的python版本
[[email protected] ~]# python
Python 2.7.5 (default, Nov 20 2015, 02:00:19)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
原本的版本是2.7.5,我现在准备再安装一个3.6的版本,而且安装后腰注意一个问题,因为centos的yum工具使用的python是2.7,需要注意
2、下载安装包,工欲善其事必先利其器,没有安装包,巧妇难为无米之炊嘛
python的安装包可以到python的官网地址里面去下载(https://www.python.org/ftp/python)
因为我的环境是windows上面的vm虚拟机里面的centos7,不想下载来本地再传进去.所以直接使用wget下载到centos里面去
(https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz)
[[email protected] ~]# wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
-bash: wget: 未找到命令
[[email protected] ~]# yum install wget
已加载插件:fastestmirror
base | 3.6 kB 00:00
......................
已安装:
wget.x86_64 0:1.14-15.el7_4.1
完毕!
[[email protected] ~]# wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
............................................
正在连接 www.python.org (www.python.org)|151.101.72.223|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:22673115 (22M) [application/octet-stream]
正在保存至: “Python-3.6.3.tgz”
0% [ ] 183,323 2.05KB/s 剩余 3h 17m
下载完成以后,解压包里面的文件
[[email protected] ~]# ls
anaconda-ks.cfg Python-3.6.3.tgz
[[email protected] ~]# tar -zxvf Python-3.6.3.tgz
Python-3.6.3/
Python-3.6.3/Doc/
................................
3、有了安装的包以后,我们再来解决一个linux系统安装软件必须的一个步骤,那就是安装一些必要的依赖包.
依赖这种东西,不好说,我只能是安装可能需要到的东西
[[email protected] ~]# yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
...............
已安装:
bzip2-devel.x86_64 0:1.0.6-13.el7 expat-devel.x86_64 0:2.1.0-10.el7_3
gdbm-devel.x86_64 0:1.10-8.el7 openssl-devel.x86_64 1:1.0.2k-8.el7
readline-devel.x86_64 0:6.2-10.el7 sqlite-devel.x86_64 0:3.7.17-8.el7
......................
完毕!
4、安装了依赖包,就可以开始编译安装python了
[[email protected] Python-3.6.3]# ./configure --prefix=/usr/local/python3;make;make install
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
..........................
checking for cl.exe... no
configure: error: in `/root/Python-3.6.3‘:
configure: error: no acceptable C compiler found in $PATH
See `config.log‘ for more details
make: *** 没有指明目标并且找不到 makefile。 停止。
make: *** 没有规则可以创建目标“install”。 停止。
[[email protected] Python-3.6.3]#
这个错误需要安装gcc来解决
[[email protected] Python-3.6.3]# yum install gcc
.....................................
已安装:
gcc.x86_64 0:4.8.5-16.el7
..................
完毕!
安装了gcc,就接着编译安装python
[[email protected] Python-3.6.3]# ./configure --prefix=/usr/local/python3;make;make install
......................
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-9.0.1 setuptools-28.8.0
这就安装完了.
5、修改原有python指向的2.7为3.5,并且修改yum配置
[[email protected] bin]# ll /usr/bin/py*
-rwxr-xr-x. 1 root root 78 11月 20 2015 /usr/bin/pydoc
lrwxrwxrwx. 1 root root 7 10月 30 19:00 /usr/bin/python -> python2
lrwxrwxrwx. 1 root root 9 10月 30 19:00 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 11月 20 2015 /usr/bin/python2.7
这里的/usr/bin/python -> python2是
原有的python使用的是/usr/bin/python2.7这个版本,我们这里就要修改成3.6的版本
首先是把原来的备份,要不然出了问题修复起来麻烦,备份后就可以将python3软连接到/usr/bin/目录下
lrwxrwxrwx. 1 root root 7 10月 30 19:00 /usr/bin/python -> python2
lrwxrwxrwx. 1 root root 9 10月 30 19:00 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 11月 20 2015 /usr/bin/python2.7
[[email protected] bin]# mv /usr/bin/python /usr/bin/python.bak
[[email protected] bin]# ll /usr/bin/py*
-rwxr-xr-x. 1 root root 78 11月 20 2015 /usr/bin/pydoc
lrwxrwxrwx. 1 root root 9 10月 30 19:00 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 11月 20 2015 /usr/bin/python2.7
lrwxrwxrwx. 1 root root 7 10月 30 19:00 /usr/bin/python.bak -> python2
[[email protected] bin]# ln -s /usr/local/python3/bin/python3 /usr/bin/python
然后测试一下当前环境使用的python是什么版本
[[email protected] bin]# python
Python 3.6.3 (default, Nov 9 2017, 00:02:58)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
我们看到的是新装的3.6版本了,接下来就去把yum的配置文件修改掉
[[email protected] bin]# vi /usr/bin/yum
#!/usr/bin/python
import sys
try:
import yum
except ImportError:
print >> sys.stderr, """\
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:
%s
上面的#!/usr/bin/python需要修改为#!/usr/bin/python2.7,修改后使用yum测试一下yum工具是否正常,修改后使用yum,发现有报错:
Downloading packages:
File "/usr/libexec/urlgrabber-ext-down", line 28
except OSError, e:
^
SyntaxError: invalid syntax
由于用户取消而退出
查看/usr/libexec/urlgrabber-ext-down文件,发现里面也有python参数,同样的修改之
[[email protected] bin]# cat /usr/libexec/urlgrabber-ext-down
#! /usr/bin/python
# A very simple external downloader
# Copyright 2011-2012 Zdenek Pavlas
上面的#!/usr/bin/python需要修改为#!/usr/bin/python2.7,修改后使用yum再测试,正常了
[[email protected] bin]# yum install zip
.......................................
已安装:
zip.x86_64 0:3.0-11.el7
完毕!
OK,查看系统里面的python2和3共存
[[email protected] bin]# python
Python 3.6.3 (default, Nov 9 2017, 00:02:58)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
[[email protected] bin]# python2.7
Python 2.7.5 (default, Nov 20 2015, 02:00:19)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
OK,我的centos7安装python3.6就算完成了