Due to personal needs, I want to upload a Python file to the cloud server for operation. The Linux image installed on the server is CentOS 7.2. During the use of the server, I found that the default Python version installed in the system is Python2, which has not only officially stopped updating, but also does not meet most users’ choice. For example, the file I uploaded is written in Python 3. In order to prevent the incompatibility of Python versions in the future, I will directly upgrade the version to Python 3.9.5. The specific version selection is at your discretion, and there is no need to pursue a higher version. The upgrade procedure is as follows:
1. Download the installation package
Start by creating a new directory to store the downloaded Python installation package and unzip it.
[root@lg10 ~]# mkdir -p /opt/python3 [root@lg10 ~]# cd /opt/python3/ [root@lg10 python3]# wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5.tar.xz [root @ lg10 python3] # tar XVF Python - 3.9.5, tar. Xz [root @ lg10 Python3] # CD Python - 3.9.5 /Copy the code
Here we directly download from the official Website of Python, so the speed is a little slow. If there is a suitable image in China, we suggest changing the download link to the domestic one, which will be much faster.
2. Compile and install
[root@lg10 python-3.9.5]#./configure --prefix=/usr/local/python3 [root@lg10 python-3.9.5]# make && make installCopy the code
The prefix parameter specifies the installation directory of the software. All the installation files of the software are copied to this directory. It is recommended to uninstall or transplant the software.
During the installation process, I encountered the following problems:
Zipimport. ZipImportError: can ‘t decompress the data
The cause of the problem is the lack of zlib related toolkits, we only need to install the related dependency packages
yum -y install zlib*
Copy the code
Then re-compile successfully.
3. Create a soft connection
Soft connect is a common Linux command that establishes a link to a file in another location. A soft link file is similar to a Windows shortcut in that it is actually a special file that holds information about the location of another file.
Since python2 has been installed on the system, the startup program under bin is still python2. In this case, we need to move the old version and rename it as a backup, otherwise we can run python directly from the command line and open the old version.
[root@lg10 Python-3.9.5]# mv /usr/bin/python /usr/bin/python2
Copy the code
Then we need to create a new soft connection to /usr/bin, so that opening Python directly by default points to the newly installed PYTHon3.
[root @ lg10 Python - 3.9.5] # ln - sv/usr/local/python3 / bin/python3.9.5 / usr/bin/PythonCopy the code
Now, if we type Python directly, Python 3 is turned on by default.
[root@lg10 ~]# python Python 3.9.5 (default, Aug 21 2021, [GCC 4.8.520150623 (Red Hat 4.8.5-39)] on Linux Type "help", "copyright", "credits" or "license" for more information.Copy the code
Instead of deleting PYTHon2, we leave it here as a backup, and if we type python2 we can switch to Python2.
[root@lg10 ~]# python2
Python 2.7.5 (default, Aug 7 2019, 00:51:29)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Copy the code