如何在centos7中安装python3

在安装python3之前,我们先看一下当前的python版本

[root@asset-app01 /]# python --version
Python 2.7.5

查看一下python的安装路径

[root@asset-app01 /]# rpm -ql python
/usr/bin/pydoc
/usr/bin/python
/usr/bin/python2
/usr/bin/python2.7
/usr/libexec/platform-python
/usr/share/doc/python-2.7.5
/usr/share/doc/python-2.7.5/LICENSE
/usr/share/doc/python-2.7.5/README
/usr/share/man/man1/python.1.gz
/usr/share/man/man1/python2.1.gz
/usr/share/man/man1/python2.7.1.gz

可以看出python是安装在/usr/bin目录中,看上去好像有三个版本,使用 ll 命令再看一下

[root@asset-app01 /]# ll usr/bin | grep python
lrwxrwxrwx    1 root root         27 Jun 25  2019 futurize -> /usr/bin/python2.7-futurize
lrwxrwxrwx    1 root root         27 Jun 25  2019 futurize-2 -> /usr/bin/python2.7-futurize
lrwxrwxrwx    1 root root         27 Jun 25  2019 futurize-2.7 -> /usr/bin/python2.7-futurize
lrwxrwxrwx    1 root root         29 Jun 25  2019 pasteurize -> /usr/bin/python2.7-pasteurize
lrwxrwxrwx    1 root root         29 Jun 25  2019 pasteurize-2 -> /usr/bin/python2.7-pasteurize
lrwxrwxrwx    1 root root         29 Jun 25  2019 pasteurize-2.7 -> /usr/bin/python2.7-pasteurize
lrwxrwxrwx.   1 root root          7 May 16  2019 python -> python2
lrwxrwxrwx.   1 root root          9 May 16  2019 python2 -> python2.7
-rwxr-xr-x.   1 root root       7216 Apr  9  2019 python2.7
-rwxr-xr-x    1 root root        304 May  3  2019 python2.7-futurize
-rwxr-xr-x    1 root root        308 May  3  2019 python2.7-pasteurize

·可以看出其实只有一个版本python2.7, python和python2都是通过软连接的方式连接到python2.7

 

接下来我们安装python3

yum install python3 -y

这个时候查看python版本,发现还是python2.7的版本

[root@asset-app01 /]# python --version
Python 2.7.5

这是因为python脚本是指向python2.7的软连接,我们需要将python文件改成指向python3的软连接

[root@localhost vagrant]# ll /usr/bin | grep python
lrwxrwxrwx  1 root root         14 Feb  4 13:11 python -> /bin/python3.6
lrwxrwxrwx  1 root root          9 Feb  4 13:08 python2 -> python2.7
-rwxr-xr-x  1 root root       7216 Aug  7 09:52 python2.7
lrwxrwxrwx  1 root root          9 Feb  4 13:09 python3 -> python3.6
-rwxr-xr-x  2 root root      11408 Aug  8 02:29 python3.6
-rwxr-xr-x  2 root root      11408 Aug  8 02:29 python3.6m

将python文件改成指向python3的软连接(其实就是将新安装的python3作为默认的python编译器)

ln -snf /bin/python3.6 /bin/python

在查看python版本

[root@localhost vagrant]# python --version
Python 3.6.8

 

 

总结: 上面说了一大堆,其实安装python3很简单,执行下面两句即可

安装python3

yum install python3 -y

将python3作为默认的python编译器

ln -snf /bin/python3.6 /bin/python

OK,python3安装完成

 

需要注意的是,安装完python3,并将python3作为默认编译器后,一些原来需要python2编译器的脚本命令将会无法使用,例如yum

解决方法请看https://www.cnblogs.com/gaoBlog/p/12262716.html

 

你可能感兴趣的:(如何在centos7中安装python3)