CentOS7下安装Python3和Python2并存
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS7下安装Python3和Python2并存相关的知识,希望对你有一定的参考价值。
(一)简述
刚安装的centos7.4版本默认是安装Python2.7,由于一些命令需要使用的原因,比如yum等,使用的是2.7.5版本的,由于现在很多库包括django都是使用Python3,
因此,有这样的需求:yum使用python2.7版本,django和其他的使用默认的python3版本。
(二)查看并备份python2相关信息
1,使用python -V 可以查看默认的版本信息。
[[email protected] ~]# python -V
Python 2.7.5
2,然后使用which python 查看下python的可执行文件的位置。
[[email protected] ~]# which python
/usr/bin/python
3,可执行文件在/usr/bin/目录下,查看有关python的文件,ll /usr/bin/python*。通过查看可以得知python默认指向的是python2.7
[[email protected] ~]# ll /usr/bin/python*
lrwxrwxrwx. 1 root root 7 Dec 21 15:23 /usr/bin/python -> python2
lrwxrwxrwx. 1 root root 9 Dec 21 15:23 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 Aug 4 2017 /usr/bin/python2.7
重要:通过查看可以得知python默认指向的是python2.7,由于还没有安装python3,可以先备份python文件,等python3安装好了后再创建软连接即可
4,备份默认python2.7版本
[[email protected] bin]# mv /usr/bin/python /usr/bin/python.bak
(三)安装配置python3
1,安装相关的包yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
[[email protected] bin]# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
Loaded plugins: fastestmirror
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
Loading mirror speeds from cached hostfile
Package zlib-devel-1.2.7-17.el7.x86_64 already installed and latest version
Package 1:openssl-devel-1.0.2k-8.el7.x86_64 already installed and latest version
Package ncurses-devel-5.9-14.20130511.el7_4.x86_64 already installed and latest version
Package sqlite-devel-3.7.17-8.el7.x86_64 already installed and latest version
Package gcc-4.8.5-16.el7_4.1.x86_64 already installed and latest version
Package 1:make-3.82-23.el7.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package bzip2-devel.x86_64 0:1.0.6-13.el7 will be installed
2,去python官网下载稳定版(https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz)
[[email protected] usr]# wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
--2018-03-08 00:05:54-- https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
Resolving www.python.org (www.python.org)... 151.101.228.223, 2a04:4e42:12::223
Connecting to www.python.org (www.python.org)|151.101.228.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 16992824 (16M) [application/octet-stream]
Saving to: ‘Python-3.6.4.tar.xz’
16% [==============> ] 2,781,624 49.4KB/s eta 4m 49s
3,安装python3
[[email protected] tmp]# tar xf Python-3.6.1.tar.xz
[[email protected] tmp]# cd Python-3.6.1
[[email protected] Python-3.6.1]# ./configure prefix=/usr/local/python3
[[email protected] Python-3.6.1]# make &&make install
4,在/usr/bin/下创建软连接
[[email protected] Python-3.6.1]# ln -s /usr/local/python3/bin/python3 /usr/bin/python
[[email protected] Python-3.6.1]# python -V
Python 3.6.1
[[email protected] Python-3.6.1]# python2 -V
Python 2.7.5
[[email protected] Python-3.6.1]# ll /usr/bin/python*
lrwxrwxrwx 1 root root 32 Mar 7 05:38 /usr/bin/python -> /usr/local/python3.6/bin/python3
lrwxrwxrwx. 1 root root 9 Jan 29 16:34 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 Aug 4 2017 /usr/bin/python2.7
lrwxrwxrwx. 1 root root 7 Jan 29 16:34 /usr/bin/python2.bak -> python2
5,配置正常使用yum,使yum使用python2.7版本,否则会报错的。
5.1,修改/usr/bin/yum文件把#! /usr/bin/python修改为#! /usr/bin/python2
[[email protected] Python-3.6.1]# vim /usr/bin/yum
#!/usr/bin/python2
5.2,修改/usr/libexec/urlgrabber-ext-down文件,把里面的#! /usr/bin/python 也要修改为#! /usr/bin/python2
[[email protected] Python-3.6.1]# vim /usr/libexec/urlgrabber-ext-down
#! /usr/bin/python2
6,如果使用pip的话,简历pip3的软连接。
[[email protected] Python-3.6.1]#ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
[[email protected] Python-3.6.1]# ll /usr/bin/pip3
lrwxrwxrwx 1 root root 29 Mar 7 05:41 /usr/bin/pip3 -> /usr/local/python3.6/bin/pip3
至此,一台服务器上默认的版本是python3 ,同时python2页存在了,可以正常使用yum来进行安装所需的软件了。
以上是关于CentOS7下安装Python3和Python2并存的主要内容,如果未能解决你的问题,请参考以下文章
Centos7下不删除python2.x的情况下安装python3.x
centos7下安装Python3.7(与python2共存)