Upgrade Python to version 3.5.2 under Linux

Original source: https://www.cnblogs.com/tssc/p/7762998.html

This article mainly introduces the method to upgrade the version of Python to 3.5.2 under Linux (CentOS)

As we all know, in 2020, python officially will no longer support the 2.7 version of python, so it is necessary to use the 3.x version of python, but the default installed python in linux is generally version 2.6 and 2.7, if you use python under linux, It is necessary to upgrade

Into the title

1. Check and confirm the relevant information of the system

[root@zstest1 ~]# cat /etc/redhat-release
CentOS release 6.5 (Final)
[root@zstest1 ~]# uname -a
Linux zstest1 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
[root@zstest1 ~]# python -V
Python 2.6.6

My software upgrade system is CentOS6.5-x86_64, the kernel is 2.6.32, and the current python version number is 2.6.6

2. Download and install Python-3.5.2

1) Download the Python-3.5.2.tgz package

[root@zstest1 ~]# mkdir -p /server/tools/
[root@zstest1 tools]# cd /server/tools/
[root@zstest1 tools]# wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz

2) Unzip, compile and install

[root@zstest1 tools]# tar -xf Python-3.5.2.tgz
[root@zstest1 tools]# cd Python-3.5.2
[root@zstest1 Python-3.5.2]# ./configure
[root@zstest1 Python-3.5.2]# make
[root@zstest1 Python-3.5.2]# make install

# If there is an error in the compilation, you need to check whether make, gcc, etc. are installed

yum install make gcc gcc-c++

3) After the installation is complete, you need to change the default python version

# Check the current default python2

[root@zstest1 Python-3.5.2]# ll /usr/bin/python*
-rwxr-xr-x  2 root root 9032 11月 22 2013 /usr/bin/python
lrwxrwxrwx. 1 root root    6 3月  20 2016 /usr/bin/python2 -> python
-rwxr-xr-x  2 root root 9032 11月 22 2013 /usr/bin/python2.6

# Check for newly installed python3

copy code
[root@zstest1 Python-3.5.2]# ll /usr/local/bin/python*
lrwxrwxrwx 1 root root       9 10月 31 16:48 /usr/local/bin/python3 -> python3.5
-rwxr-xr-x 2 root root 9630001 10月 31 16:48 /usr/local/bin/python3.5
lrwxrwxrwx 1 root root      17 10月 31 16:48 /usr/local/bin/python3.5-config -> python3.5m-config
-rwxr-xr-x 2 root root 9630001 10月 31 16:48 /usr/local/bin/python3.5m
-rwxr-xr-x 1 root root    3066 10月 31 16:48 /usr/local/bin/python3.5m-config
lrwxrwxrwx 1 root root      16 10月 31 16:48 /usr/local/bin/python3-config -> python3.5-config
copy code

# Modify the default python

[root@zstest1 Python-3.5.2]# cd /usr/bin/
[root@zstest1 bin]# rm -f python2
[root @ zstest1 bin] # mv python python2.6.ori
[root@zstest1 bin]# ln -s python2.6 python2
[root@zstest1 bin]# ln -s /usr/local/bin/python3 /usr/bin/python

# Delete the old python soft link and create new python2 and python3 soft links

4) Check the modified python version

[root@zstest1 bin]# python -V
Python 3.5.2
[root@zstest1 bin]# python2 -V
Python 2.6.6
[root@zstest1 bin]# python3 -V
Python 3.5.2

# As you can see, the python version opened by default is already 3.5.2, so far the python version has been upgraded successfully

3. Follow-up software problem handling

# After the default version of python is changed to 3.x, there will be a problem with the system's call to yum, so you need to modify the relevant configuration of yum

[root@zstest1 bin]# vim /usr/bin/yum

# Need to check the following configuration

#!/usr/bin/python2

# In this way, the modified yum can still call python2.6.6 python, and no error will be reported when using yum to install software

# The above is the python version upgrade on the CentOS6.5 system. The default python version on CentOS7.2 is 2.7.5, which can be modified using the same method, but the python link of CentOS7.2 is slightly different, so you need to pay attention

[root@centos72 ~]# ll /usr/bin/python*
lrwxrwxrwx. 1 root root    7 10月 26 2016 /usr/bin/python -> python2
lrwxrwxrwx. 1 root root    9 10月 26 2016 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 11月 20 2015 /usr/bin/python2.7

# In addition, CentOS7.2 has another file that needs to be modified

[root@centos72 ~]# vim /usr/libexec/urlgrabber-ext-down

# Confirm the following configuration:

#!/usr/bin/python2

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324766139&siteId=291194637