Installing Python Source Code
Prerequisites
GCC, CMake, and Make have been installed.
Procedure
This section uses the openEuler OS as an example to describe how to install Python from source.
- Go to Python Source Releases, download the XZ software package of Python 3.11.x, and decompress the package to the local environment.This section uses Python 3.11.4 as an example and /opt/buildtools as the installation directory.
cd /opt/buildtools tar -xf Python-3.11.4.tar.xz
- Set compilation parameters and create a Python working directory /opt/buildtools/python-3.11.
tmp_cpus=$(grep -w processor /proc/cpuinfo|wc -l) install_dir=/opt/buildtools/python-3.11 mkdir -p ${install_dir} - Compile Python.
cd Python-3.11.4 ./configure CFLAGS="-D_FORTIFY_SOURCE=2 -O2 -fstack-protector-strong" CPPFLAGS="-D_FORTIFY_SOURCE=2 -O2 -fstack-protector-strong" LDFLAGS="-Wl,-z,relro,-z,now -s" --prefix=${install_dir} -enable-shared make -j ${tmp_cpus} make install sudo cp libpython3.11.so.1.0 libpython3.11.so /usr/lib64
If the OS is not openEuler, copy libpython3.11.so.1.0 and libpython3.11.so to the corresponding system library directory (for example, /lib/aarch64-linux-gnu of Ubuntu in the Arm architecture).
- Modify Python symbolic links in /usr/local/bin. If a rollback is required, back up the original Python file.
[ -f /usr/local/bin/python3 ] && rm -f /usr/local/bin/python3 [ -e ${install_dir}/bin/python3.11 ] && ln -sf ${install_dir}/bin/python3.11 /usr/local/bin/python3 [ -f /usr/local/bin/python ] && rm -f /usr/local/bin/python [ -e ${install_dir}/bin/python3.11 ] && ln -sf ${install_dir}/bin/python3.11 /usr/local/bin/python [ -f /usr/local/bin/pip3 ] && rm -f /usr/local/bin/pip3 [ -e ${install_dir}/bin/pip3.11 ] && ln -sf ${install_dir}/bin/pip3.11 /usr/local/bin/pip3 - Query the Python version.
/usr/local/bin/python3 -V
The installation is successful if the following information is displayed:Python 3.11.4
- After the installation is complete, an error message similar to "xxx /usr/bin/python3 xxx" may be reported during firmware compilation. Open the terminal again and continue to run the firmware compilation command.
- The preceding steps modify only the Python symbolic links in /usr/local/bin and do not modify those in /usr/bin. This is to avoid affecting OS commands, such as yum. If Python-related errors occur during subsequent operations, try restoring the symbolic links in /usr/local/bin to resolve the issue.
Parent topic: FAQs