Upgrade Python version under CentOS7

Article directory

Upgrade Python version under CentOS7

Server version: CentOS 7.3 64 bit

Old Python version: 2.7.5

New Python version: 3.8.0

1. Switch directory to / usr/local

[root@ chenc01 ~]# cd /usr/local/
[root@ chenc01 local]# ls
bin  etc  games  include  lib  lib64  libexec  sbin  share  src

2. Download directory Python version package

[root@ chenc01 local]# wget http://npm.taobao.org/mirrors/python/3.8.0/Python-3.8.0.tgz
--2020-03-10 17:14:37--  http://npm.taobao.org/mirrors/python/3.8.0/Python-3.8.0.tgz
[root@ chenc01 local]# ls
bin  etc  games  include  lib  lib64  libexec  Python-3.8.0.tgz  sbin  share  src

3. Unzip Python package

[root@ chenc01 local]# tar zxf Python-3.8.0.tgz

4. Create folder in current directory - Python 3

[root@ chenc01 local]# mkdir python3
[root@ chenc01 local]# ls
bin  games    lib    libexec  Python-3.8.0      sbin   src
etc  include  lib64  python3  Python-3.8.0.tgz  share

5. Build install

# Enter the unzip file path
[root@ chenc01 local]# cd Python-3.8.0/
# Generate makefile file
[root@ chenc01 Python-3.8.0]# ./configure --prefix=/usr/local/python3
# Operation on makefile file
[root@ chenc01 Python-3.8.0]# make
# install
[root@ chenc01 Python-3.8.0]# make install

Command interpretation:

When installing the program through the source code in Linux, execute. / configure, make, and then make install to extract the file

The make command operates on the makefile file, and the make install command is the install command. What is. / configure? . / configure is actually generating makefile

prefix function: used to specify the program storage path when compiling.

No prefix is specified, the executable file is in / usr/local/bin by default, the library file is in / usr/local/lib by default, the configuration file is in / usr/local/etc by default, and other resource files are in / usr/local/share

Specify prefix and delete a folder directly

6. Create Python 3 soft connection directly

# Create a python 3 soft chain under the / usr/bin path, pointing to the installed Python 3
[root@ chenc01 Python-3.8.0]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
# Create a pip3 soft chain under the / usr/bin path, pointing to the installed pip3
[root@ chenc01 Python-3.8.0]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

7. Overwrite existing python, point to Python 3.8 when starting Python

[root@ chenc01 Python-3.8.0]# mv /usr/bin/python /usr/bin/python2_old
[root@ chenc01 Python-3.8.0]# mv /usr/bin/pip3 /usr/bin/pip2_old
# Modify soft connection
[root@ chenc01 Python-3.8.0]# ln -s /usr/local/python3/bin/python3 /usr/bin/python
[root@ chenc01 Python-3.8.0]# ln -s /usr/local/python3/bin/pip /usr/bin/pip

8. Closing work

Due to the modification of python version, some programs that depend on the old version will encounter errors, such as yum, which can be configured as follows:

[root@ chenc01 Python-3.8.0]# vim /usr/bin/yum
# primary
#!/usr/bin/python
import sys
try:
    import yum
except ImportError:
    print >> sys.stderr, """\
# change
#!/usr/bin/python2.7
import sys
try:
    import yum
except ImportError:
    print >> sys.stderr, """\
 
[root@ chenc01 Python-3.8.0]# python -V
Python 3.8.0
47 original articles published, 55 praised, 9075 visited
Private letter follow

Keywords: Python Makefile yum npm

Added by sonehs on Tue, 10 Mar 2020 12:33:05 +0200