Compiling and Installing Python
The following takes the installation of Python 3.9.2 as an example to demonstrate the compilation and installation process. Before compiling, verify the system time to ensure it matches the current time. This prevents falling into an infinite loop during the Python compilation and installation.
If you need to compile and install another Python version, replace the source package or refer to the Python official website.
- Install the dependencies required for source code compilation.Debian, Ubuntu, veLinux:
sudo apt-get install -y make zlib1g zlib1g-dev build-essential libbz2-dev libsqlite3-dev libssl-dev libxslt1-dev libffi-dev openssl python3-tk
openEuler, CentOS, Kylin, BC-Linux, UOS, AntOS, AliOS, CTyunOS, CULinux, Tlinux, MTOS:sudo yum install -y make zlib-devel libffi-devel openssl-devel sqlite-devel
- Run the following command to download the source package of Python 3.9.2 to any directory of the installation environment:
wget https://www.python.org/ftp/python/3.9.2/Python-3.9.2.tgz
- Go to the download directory and run the following command to extract the source package:
tar -zxvf Python-3.9.2.tgz
- Go to the decompressed folder and run the following configuration, build, and installation commands:
cd Python-3.9.2 ./configure --prefix=/usr/local/python3.9.2 --with-ssl-default-suites=openssl --enable-shared CFLAGS=-fPIC make sudo make install
- --prefix specifies the Python installation path. You can change it as required.
- The --enable-shared option compiles the dynamic library.
- Set the Python 3.9.2 environment variables.
export LD_LIBRARY_PATH=${HOME}/python3.9.2/lib:$LD_LIBRARY_PATH export PATH=${HOME}/python3.9.2/bin:$PATHYou can also write the preceding commands to the ~/.bashrc file and run the source ~/.bashrc command for the environment variables to take effect immediately.
- After the installation is complete, run the following commands to check the installation version. If the required version information is displayed, the installation is successful.
python3 --version pip3 --version
- Configuring the PIP source
- Run the following command as the installation user of the software package:
cd ~/.pip
If a message indicating that the directory does not exist is displayed, run the following commands to create and go to the directory:
mkdir ~/.pip cd ~/.pip
- Edit the pip.conf file.
Run the vi pip.conf command to open the pip.conf file and add the following content to the file.
[global] # Huawei source is used as an example. Replace it based on the actual situation. index-url = https://mirrors.huaweicloud.com/repository/pypi/simple trusted-host = mirrors.huaweicloud.com timeout = 120
- Run the :wq! command to save the file and exit.
- Run the following command as the installation user of the software package: