CentOS7 installation of Python3 and Python2 coexist

Source: Internet
Author: User
Tags openssl

(a) brief description
The centos7.4 version installed by default is installed Python2.7, because some commands need to use the reason, such as Yum, use 2.7.5 version, because now many libraries including Django are using Python3,
So there's the need: Yum uses the python2.7 version, Django and others using the default Python3 version.

(ii) Review and backup of Python2 related information
1, use Python-v to view the default version information.

[[email protected] ~]# python -VPython 2.7.5

2, then use which Python to view the location of the next Python executable file.

[[email protected] ~]# which python/usr/bin/python

3, executable file in the/usr/bin/directory, view the file about Python, ll/usr/bin/python*. By looking at it, you know that Python is pointing to python2.7 by default.

[[email protected] ~]# ll /usr/bin/python*lrwxrwxrwx. 1 root root    7 Dec 21 15:23 /usr/bin/python -> python2lrwxrwxrwx. 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

Important: By viewing you can tell that Python is pointing to python2.7 by default, because you do not have Python3 installed, you can back up the Python file first, wait until Python3 is installed and then create a soft connection.
4, back up the default python2.7 version
[Email protected] bin]# Mv/usr/bin/python/usr/bin/python.bak

(iii) installation configuration Python3
1, install the relevant package 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 makeloaded plugins:fastestmirrorbase |                                                                                                           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 hostfilepackage zlib-devel-1.2.7-17.el7.x86_64 already installed an D Latest Versionpackage 1:openssl-devel-1.0.2k-8.el7.x86_64 already installed and latest Versionpackage Ncurses-devel-5.9-14.20130511.el7_4.x86_64 already installed and latest Versionpackage sqlite-devel-3.7.17-8.el7.x86 _64 already installed and latest Versionpackage gcc-4.8.5-16.el7_4.1.x86_64 already installed and latest Versionpackage 1:make-3.82-23.el7.x86_64 already installed and latest versionresolving dependencies--> Running Transaction Check---> Package bzip2-devel.x86_64 0:1.0.6-13.el7 'll be installed

2, go to Python official website download stable version (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.xzResolving www.python.org (www.python.org)... 151.101.228.223, 2a04:4e42:12::223Connecting to www.python.org (www.python.org)|151.101.228.223|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 16992824 (16M) [application/octet-stream]Saving to: ‘Python-3.6.4.tar.xz’16% [==============>                                                                                ] 2,781,624   49.4KB/s  eta 4m 49s

3, install 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, create a soft connection under/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 -VPython 3.6.1[[email protected] Python-3.6.1]# python2 -VPython 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/python3lrwxrwxrwx. 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.7lrwxrwxrwx. 1 root root    7 Jan 29 16:34 /usr/bin/python2.bak -> python2

5, configure the normal use of yum, so that Yum uses the python2.7 version, otherwise it will be an error.
5.1, modify the/usr/bin/yum file to #! /usr/bin/python modified to #! /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, if use PIP, resume PIP3 soft connection.

[[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/pip3lrwxrwxrwx 1 root root 29 Mar  7 05:41 /usr/bin/pip3 -> /usr/local/python3.6/bin/pip3

At this point, the default version on a single server is Python3, and the Python2 page exists, and Yum can be used to install the required software.

CentOS7 installation of Python3 and Python2 coexist

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.