Install Python3 and Py under CentOS7

(1) Brief description
The newly installed centos7.4 version installs Python2.7 by default. Due to some commands that need to be used, such as yum, etc., the 2.7.5 version is used. Because many libraries including django now use Python3,
Therefore, there is a demand: yum uses the python2.7 version, and django and others use the default python3 version.

(2) View and backup python2 related information
1 , Use python -V to view the default version information.

[ root@localhost ~]# python -V
Python 2.7.5

2 , And then use which python to view the location of the python executable file.

[ root@localhost ~]# which python
/usr/bin/python

3 , The executable file is in the /usr/bin/ directory, check the python-related files, ll /usr/bin/python*. By looking at it, you can know that python defaults to python2.7

[ root@localhost ~]# ll /usr/bin/python*
lrwxrwxrwx.1 root root    7 Dec 2115:23/usr/bin/python -> python2
lrwxrwxrwx.1 root root    9 Dec 2115:23/usr/bin/python2 -> python2.7-rwxr-xr-x.1 root root 7136 Aug  42017/usr/bin/python2.7

Important: By checking, you can know that python defaults to python2.7. Since python3 has not been installed, you can back up the python file first, and then create a soft connection after python3 is installed
4 , Backup the default python2.7 version
[ root@localhost bin]# mv /usr/bin/python /usr/bin/python.bak

(3) Method 1: Install and configure python3
1 , Install related packages yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make

[ root@localhost 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 , Go to python official website to download the stable version (https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz)

[ root@localhost usr]# wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
- - 2018- 03- 0800: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,62449.4KB/s  eta 4m 49s

3 , Install python3

[ root@localhost tmp]# tar xf Python-3.6.1.tar.xz
[ root@localhost tmp]# cd Python-3.6.1[root@localhost Python-3.6.1]# ./configure prefix=/usr/local/python3
[ root@localhost Python-3.6.1]# make &&make install

4, Create a soft connection under /usr/bin/

[ root@localhost Python-3.6.1]# ln -s /usr/local/python3/bin/python3  /usr/bin/python
[ root@localhost Python-3.6.1]# python -V
Python 3.6.1[root@localhost Python-3.6.1]# python2 -V
Python 2.7.5[root@localhost Python-3.6.1]# ll /usr/bin/python*
lrwxrwxrwx  1 root root   32 Mar  705:38/usr/bin/python ->/usr/local/python3.6/bin/python3
lrwxrwxrwx.1 root root    9 Jan 2916:34/usr/bin/python2 -> python2.7-rwxr-xr-x.1 root root 7136 Aug  42017/usr/bin/python2.7
lrwxrwxrwx.1 root root    7 Jan 2916:34/usr/bin/python2.bak -> python2

5 , Configure to use yum normally and make yum use python2.7 version, otherwise an error will be reported.
5.1 , Modify the /usr/bin/yum file and change #! /usr/bin/python to #! /usr/bin/python2

[ root@localhost Python-3.6.1]# vim /usr/bin/yum 

#! /usr/bin/python2
5.2 ,modify/usr/libexec/urlgrabber-ext-down file, put inside#!/usr/bin/python should also be modified to#!/usr/bin/python2
[ root@localhost Python-3.6.1]# vim /usr/libexec/urlgrabber-ext-down 

#! /usr/bin/python2

6 , If you use pip, resume pip3 soft connection.

[ root@localhost Python-3.6.1]#ln -s /usr/local/python3/bin/pip3   /usr/bin/pip3
[ root@localhost Python-3.6.1]# ll /usr/bin/pip3
lrwxrwxrwx 1 root root 29 Mar  705:41/usr/bin/pip3 ->/usr/local/python3.6/bin/pip3

At this point, the default version on a server is python3, and the python2 page exists, and you can use yum to install the required software normally.

(4) Method 2: Build a Python virtual environment (convenient and fast, recommended)
1 , Install dependencies
[ root@localhost ~]# yum -y install wget sqlite-devel xz gcc automake zlib-devel openssl-devel epel-release git
2 , Compile and install

[ root@localhost ~]# wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
[ root@localhost ~]# tar xvf Python-3.6.1.tar.xz  && cd Python-3.6.1[root@localhost ~]# ./configure && make && make install

3 , Establish a Python virtual environment. Because CentOS 6/7 comes with Python2, and tools such as Yum rely on the original Python, in order not to disturb the original environment, we will use the Python virtual environment

[ root@localhost ~]#cd /opt
[ root@localhost ~]#python3 -m venv py3
[ root@localhost ~]#source /opt/py3/bin/activate(py3)[root@localhost ~]#

############ Seeing the following prompt means success. You must run the above source command first to run the Python script in the future. All the following commands are run in this virtual environment ############

Recommended Posts

Install Python3 and Py under CentOS7
Install Python3 and ansible under CentOS8
centos7 install python3 and ipython
CentOS7 install python3 and pip3
CentOS 6.9 compile and install python
CentOS 6 compile and install python 3
Centos6 install Python2.7.13
Install and configure keepalived under CentOS 5.9
Compile and install LAMP under Centos 5.2
Centos7 install Python 3.6.
CentOS install Python 3.6
Centos7 install Python2.7
CentOS quickly install Python3 and pip3
Install and use docker under CentOS 6.8
Centos install Python3
CentOS6.8 install python2.7
Install Mono 3.2 and Jexus 5.4 under CentOS 6.3
Compile and install libmodbus library under CentOS7
Install Mono 2.10.8 and Jexus 5.0 under 32- and 64-bit CentOS 6.0
Install centos7 and connect
Install mysql5.7 under CentOS7
Install ActiveMQ under Centos7
Install PostgreSQL12 under CentOS7
Install CentOS under VMware
[python] python2 and python3 under ubuntu
Install mysql under Centos 7
Install Jenkins under Centos 7
Install mysql5.1 under CentOS6.5
Install svn and configuration through yum under CentOS
Know Linux and install CentOS
CentOs7.3 compile and install Nginx 1.9.9
Install python environment under Linux
CentOS 7 install Mono and MonoDevelop
CentOS6.5 install Java 8 and Tomcat8
Centos 6.10 reinstall python and yum
Install Oracle11gR2 database under CentOS6.9
Install MySQL under Linux (CentOS 7)
Centos6.5 install and configure mongodb
Install Java JDK8 under CentOS6
CentOS7 install OracleJDK and JRE
CentOS6.5 install Java 8 and Tomcat8
CentOS6 install and crack Jira 7
CentOS6.5 install Java 8 and Tomcat8
Centos7 compile and install ntp-4.2.8p11
Centos6 install python3 pip3 ipython3
CentOS6 install and crack confluence
Install MongoDB database under CentOS7
CentOS6 install and crack Jira 7
CentOS 6.8 under linux install mongodb
Install Mesos tutorial under CentOS7
Compile and install Lnmp shell script under Linux centos
Centos 7 install jdk and package service service
CentOS7 yum install and start mysql
Install and use dig under ubuntu/debian
CentOS Yum compile and install MySQL 5.6
CentOS 8 install Git and basic configuration
Use Nginx and u under CentOS
[Introduction to redis] Install redis under Centos
Upgrade OpenSSL and OpenSSH under CentOS7
CentOS + Python3.6+
CentOS 6.x compile and install Nginx