The process record of installing Python-3.9.10 with offline compressed package on CentOS 7.5

According to the needs of the project, a Python 3.9.10 installation environment needs to be deployed on CentOS 7.5.

It is worth noting that in the CentOS 7.5 system, Python 2.7.x is installed by default.
Python 2.7.x is a version that many system tools and applications depend on, so it is included in the CentOS 7.5 distribution. If you run the python command on a CentOS 7.5 system, it will use the default Python 2.7.x interpreter.

We can test that Python 2.7.x is installed by default.
Use the following command to test:

python --version

insert image description here
From the above running results, CentOS 7.5 is installed with Python 2.7.5 by default.

The next question is, obviously, in CentOS 7.5, some programs or software, including those installed by users, need to depend on Python 2.7.x, so for Python 2.7.x, we cannot uninstall it.

So how do we ensure that the Python code we write can be executed by the Python 3 interpreter, not by the .Python 2.7.x interpreter?
There are two common methods:
The first one: use the python3 command to run Python 3.x codes on the command line. For example, assuming your Python 3.x code is kept in the test.py file, you can run it with:

python3 test.py

Option 2: Add the following code on the first line of the Python 3.x code, which will ensure that the code runs in Python 3.x:

# !/usr/bin/env python3

Here is a detailed explanation of the meaning of the above code:

#!/usr/bin/env python3 is a special comment, also known as Shebang or Hashbang. Its role is to tell the system which interpreter this script should use to run.

When running a script in a Linux or Unix-like system, the kernel looks at the first line of the script, if it starts with #!, then the kernel interprets the rest as the path to the interpreter, and then uses the script file as the path to the interpreter. parameters to run the script.

In #!/usr/bin/env python3, #! indicates that this is a Shebang comment. /usr/bin/env is an executable that searches the system's PATH environment variable for executables and executes it. python3 is the name of the interpreter to use.
Note:
/usr/bin/env is an executable file. The function of the executable file env is: search for the executable file corresponding to the command in the PATH environment variable, and use the first executable file found to execute the order.
When #!/usr/bin/env python3 appears on the first line of a script file, the kernel parses this line of code and uses the env executable to find the location of the executable corresponding to the python3 command. Since env is an executable, it searches all the directories in the PATH environment variable until it finds an executable named python3. env then passes control to the python3 executable, which runs the Python 3.x interpreter and begins interpreting the remaining script code.
The advantage of using env to find executable files is that you can avoid hardcoding the path of executable files in the script, but use the PATH environment variable to find executable files, which can increase the portability of scripts. If you run a script with the same shebang line on another system, env will use the correct version of the Python interpreter on that system to run the script, even though the Python interpreter's installation path may be different on different systems.

We can choose the above two methods according to our actual situation.

Next, there is another question, that is, for every Python code we write, do we need to add the above code?
The answer is: If you want to run with the Python3 interpreter, then add them all.

Usually we also need to add an encoding format statement at the beginning of each Python code file:

# -*- coding: utf-8 -*-

So the question is, what should be the order of these two descriptive sentences?
yes

# -*- coding: utf-8 -*-

# !/usr/bin/env python3

still:

# -*- coding: utf-8 -*-

# !/usr/bin/env python3

Woolen cloth?

The answer shows that the statement should be

# -*- coding: utf-8 -*-

On the first line, because the encoding format specification statement should be placed before anything else. When the Python interpreter reads a Python code file, it looks for an encoding format specification statement to determine the character encoding used in the code file before it starts interpreting the rest of the file.

After these two preconditions are clarified, we can then install Python 3 on CentOS 7.5.

Before installing Python 3, the command is generally used

sudo yum update

Function: Update the installed software packages in your system to the latest version.

Specifically, the sudo yum update command does the following:

Check for differences between the installed package version and the latest package version available on your system.
Download available updates and install them.
You are asked to confirm that you want to install these updates, and if you agree, they will be installed.
During the installation process, you may be asked for some additional information if required.
After updating packages, the yum command restarts the services and applications associated with those packages to ensure that the new version of the package runs correctly.
By regularly updating the software package, you get the latest features and security fixes to improve the reliability and security of your system.

Question: Why do you need to update other installed software packages in the system to the latest before installing Python 3?
Answer: It is good practice to update installed packages before installing Python 3, as many packages require some specific versions of libraries and dependencies at runtime. Updating the packages on your system ensures that the packages installed on your system are compatible with Python 3 and avoids possible incompatibilities.
In addition, updating software packages can also improve system security and performance. New versions of packages often fix security holes and other bugs, and offer better performance and stability. If you don't update your packages, you may experience issues such as performance degradation or security holes.
Therefore, updating installed packages before installing Python 3 is highly recommended to ensure your system is safe, stable and compatible.

Question: Is it okay to not update other installed software packages in the system to the latest before installing Python 3?
A: In theory, it would be fine to not update installed packages before installing Python 3. However, this may cause some incompatibilities that may affect the proper functioning of other packages on your system.

If you don't update your packages, you may run into dependency conflicts between some package versions, causing the installation of Python 3 to fail. Additionally, some older versions of software packages may contain known security vulnerabilities, which may compromise the security of your system.

Therefore, it is recommended to update the installed packages in the system before installing Python 3 to ensure the compatibility, security and stability of the system. If you are unable to update packages for some reason, it is recommended to double check system and package compatibility before installing Python 3, and make sure you have installed all necessary dependencies.

Execution statement:

sudo yum update

insert image description here
insert image description here
insert image description here
A partially enlarged screenshot of the above figure is as follows:
insert image description here

Next, go to the Python official website to download the offline compressed package of Python-3.9.10.
The URL link for downloading the offline compressed package of Python-3.9.10 from the Python official website is as follows:
https://www.python.org/ftp/python/3.9.10/
Sometimes the connection with the Python official website is not smooth, so here is another one Baidu network disk download link:
https://pan.baidu.com/s/1hZ9NgWTHlY6Nwzht535xjw?pwd=kohf

The download file name is: Python-3.9.10.tgz, this is used to install on the Linux system.

After downloading, use the pagoda panel to upload to the directory /opt/myapp/:
insert image description here

Question: Why is it under the directory /opt/myapp/?
In the CentOS system, it is generally more professional and standardized to put the offline installation package uploaded by yourself in the /opt directory.

The /opt directory is specially used to store third-party software and applications. By default, only the root user has write permission. Putting the offline installation package in the /opt directory can facilitate management and maintenance, and it also follows the Filesystem Hierarchy Standard (FHS) standard of the Linux system.

In the /opt directory, you can create a subdirectory related to the application name, and then extract the offline installation package to this directory. For example, if the application you want to install is named myapp, you can create a subdirectory named myapp under the /opt directory, and extract the offline installation package to this directory.

In short, it is a relatively common practice to put the offline installation package in the /opt directory, and it is also a standard practice.

Then you can follow the above steps to install.
1 Enter the directory where Python-3.9.10.tgz is located.

cd /opt/myapp/

insert image description here

2 Unzip the Python-3.9.10.tgz file.

tar -xzvf Python-3.9.10.tgz

3 Enter the unzipped Python-3.9.10 directory:

cd Python-3.9.10

insert image description here

4 Configure the installation options:

./configure --prefix=/usr/local/python3.9

The installation path of Python is specified here as /usr/local/python3.9, which can also be modified as needed.
This statement configures the installation path of Python by executing the ./configure script with parameters. The specific parameter is "prefix=/usr/local/python3.9"
insert image description here

5 Compile and install Python:

make && make install

6 Verify that Python was successfully installed:

/usr/local/python3.9/bin/python3 -V

7 The path to the newly installed Python-3.9.10 is used here, the output should be:

Python 3.9.10

insert image description here
8-Create a soft link for Python (similar to configuring environment variables in Windows) to use the python3 command directly on the command line:

sudo ln -s /usr/local/python3.9/bin/python3 /usr/bin/python3

Through this command, you can directly use the python3 command to call Python 3.9.10 on the command line.
insert image description here
After running the above soft link creation command, the following soft link will appear in the directory /usr/bin/:
insert image description here
Because the /usr/bin/ path is the default environment variable path in the system environment variable PATH, so after completing this step , you can enter python3 -V on the command line to verify whether Python is installed successfully and whether the version number of Python is 3.9.10.
insert image description here
Digression: If the path of the soft link is wrong and you want to delete it, you can use the following command to achieve it:

sudo rm /usr/bin/python3

Through the establishment of the above soft link, if we add the following code to the Python code:

#!/usr/bin/env python3

Then the Python code will be executed using the Python3 interpreter instead of the Python2 interpreter when it is executed.

Through the above process, Python-3.9.10 is installed, and the next step is to test it with a practical example.

We use the following piece of code to print out the version number of the currently used Python interpreter.

# -*- coding: utf-8 -*-
# !/usr/bin/env python3

import sys

print("The current Python interpreter version number is:\n", sys.version_info)

We save the above code into the file P-0004.py and upload it to /opt/python_scripts of the CentOS 7.5 system.

First of all, make sure that the operation permission of the file P-0004.py is turned on.
For related knowledge about permissions, you can refer to the blog post:
https://blog.csdn.net/wenhao_ir/article/details/130151125
insert image description here

Then execute the following statement:

python3 /opt/python_scripts/P-0004.py

The running results are as follows:
insert image description here
From the above results, we can see that we have successfully run P-0004.py and got the version number of the Python interpreter.

So far, we have successfully implemented "Installing Python-3.9.10 with an offline compressed package on CentOS 7.5" and tested it with the test code.

Guess you like

Origin blog.csdn.net/wenhao_ir/article/details/130056634