Compiling and Installing Python

The following describes how to install Python 3.7.5. Before compilation, ensure that the system time matches the current time to prevent an infinite loop during 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.

  1. Install the dependencies required for source code compilation.
    Debian/Ubuntu/veLinux:
    sudo apt-get install -y gcc g++ make cmake libsqlite3-dev libbz2-dev zlib1g-dev libssl-dev libffi-dev
    openEuler, CentOS, Kylin, BCLinux, UOS V20, AntOS, AliOS, CTyunOS, CULinux, Tlinux, MTOS, vesselOS:
    sudo yum install -y gcc gcc-c++ make cmake sqlite-devel bzip2-devel zlib-devel openssl-devel libffi-devel
  2. Run the following command to download the source package of Python 3.7.5 to any directory of the installation environment:
    wget https://www.python.org/ftp/python/3.7.5/Python-3.7.5.tgz
  3. Go to the download directory and run the following command to extract the source package:
    tar -zxvf Python-3.7.5.tgz
  4. Go to the new folder and run the following configuration, compilation, and installation commands:
    cd Python-3.7.5
    ./configure --prefix=${HOME}/python3.7.5 --enable-loadable-sqlite-extensions --enable-shared
    make
    make install
    • --prefix specifies the Python installation path. You can change it as required.
    • The --enable-shared option compiles the libpython3.7m.so.1.0 dynamic library.
    • The --enable-loadable-sqlite-extensions option loads libsqlite3-dev.
  5. Set the Python 3.7.5 environment variables.
    export LD_LIBRARY_PATH=${HOME}/python3.7.5/lib:$LD_LIBRARY_PATH
    export PATH=${HOME}/python3.7.5/bin:$PATH

    You can also write the preceding commands to the ~/.bashrc file and run the source ~/.bashrc command to make them valid immediately.

  6. 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
  7. Configuring the PIP source
    1. 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
    2. Edit the pip.conf file.

      Run the vi pip.conf command to open the pip.conf file and edit the file as follows.

      [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
    3. Run the :wq! command to save the file and exit.