(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