1. By default, Linux will install Python itself , you can run the python --version command to view it, as shown in the figure:
Start python, use the command: python:
Ctrl+D to exit python
2. Check the location of Python installed by default on Linux
3. Install python3
(1) Log in to Python Source Releases | Python.org and find the corresponding version (we take Python 3.7.12 as an example) as shown in the figure:
DownloadPython 3.7.12.tgz
(2) Upload the downloaded tgz to a certain directory under linux, and then decompress it;
(3) Prepare the compilation environment
Execute the following command:
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel libffi-devel gcc make |
You can also use the yum -y group install "Development tools" command to install all development environment dependencies.
Install the dependencies required by python. The installation is successful as shown in the figure:
(4) Compile and install
Execute cd Python-3.7.12 to enter the decompressed Python-3.7.12 directory, and execute the following three commands in sequence:
./configure --prefix=/usr/local/tools/Python-3.7.12 make make install |
Note: --prefix is the installation directory of Python.
If there is no error message, the installation is successful.
(5) Establish a soft connection (Python and pip both establish a soft connection, respectively distinguishing between python3, pip3 and the Python2 that comes with the system)
ln -s /usr/local/tools/Python-3.7.12/bin/python3.7 /usr/bin/python3 ln -s /usr/local/tools/Python-3.7.12/bin/pip3.7 /usr/bin/pip3 |
You can use the command ls -l /usr/bin/ to check whether the soft link has been created successfully:
(6) Start python and use the command python3 in the command window, as shown in the figure:
(7) Configure environment variables
Configuring environment variables is mainly to quickly use the pip3 installation command.
Execute vi ~/.bash_profile, open the configuration file, and add the following configuration:
# configure python export PYTHON_HOME=/usr/local/tools/Python-3.7.12 export PATH=$PYTHON_HOME/bin:$PATH |
Save and exit (:wq), and execute the source ~/.bash_profile command to make the configuration take effect. Execute the echo command to check whether the configuration is successful, as shown in the figure: