openEuler self-upgrades Python3

# Python Standalone Binary Package Installation Guide

:pushpin: Note: This document provides general installation instructions. The specific version numbers, dates, and filenames mentioned are merely examples for illustration. Always replace them with the latest version available on the python-build-standalone Releases page.


:bullseye: Overview

This guide uses pre-compiled standalone binary packages from the python-build-standalone project to install Python. Key advantages include:

  • :white_check_mark: No compilation required: Extract and run immediately—no build dependencies or waiting time.
  • :white_check_mark: Non-invasive to the system: Does not interfere with the system’s default Python installation.
  • :white_check_mark: High compatibility: Supports glibc 2.17 and above, suitable for most Linux distributions.
  • :white_check_mark: Version isolation: Can coexist peacefully with the system Python.

:wrench: 1. Environment Preparation

1.1 Check System Information

# View OS version
cat /etc/os-release

# View glibc version
ldd --version

# Check current system Python version
python3 --version

1.2 Create Installation Directory

# Create a directory for downloads (optional)
mkdir -p ~/downloads && cd ~/downloads

:inbox_tray: 2. Download Python Standalone Binary Package

2.1 Method 1: Download from GitHub Releases (Recommended)

Visit the project’s release page and download the Linux-compatible archive:

  • Project URL: https://github.com/astral-sh/python-build-standalone/releases
  • Filename format: cpython-<version>+<release-date>-<arch>-unknown-linux-gnu-install_only.tar.gz

:pushpin: Example: The version 3.14.7 and date 20260825 below are placeholders. Replace them with the actual latest version and date from the Releases page.

# Example download command (replace with actual latest link)
wget https://github.com/astral-sh/python-build-standalone/releases/download/20260825/cpython-3.14.7+20260825-x86_64-unknown-linux-gnu-install_only.tar.gz

2.2 Method 2: Download from Domestic Mirror Sites (Recommended for Users in China)

If GitHub access is restricted, use domestic mirrors:

:pushpin: Example: Replace version and date with actual values.

# University of Science and Technology of China (USTC) Mirror
wget https://cmcc.mirrors.ustc.edu.cn/github-release/astral-sh/python-build-standalone/20260825/cpython-3.14.7+20260825-x86_64-unknown-linux-gnu-install_only.tar.gz

# Nanjing University Mirror
wget https://mirror.nju.edu.cn/github-release/astral-sh/python-build-standalone/20260825/cpython-3.14.7+20260825-x86_64-unknown-linux-gnu-install_only.tar.gz

2.3 File Description

CPU Architecture Variant Description Recommended Use Case
x86_64-unknown-linux-gnu General-purpose version, best compatibility Default recommendation
x86_64_v2 Supports SSE4.2, POPCNT, etc. Newer CPUs
x86_64_v3 Supports AVX, AVX2, etc. Latest CPUs
aarch64-unknown-linux-gnu ARM64 architecture Kunpeng, Phytium, and other ARM servers

:open_file_folder: 3. Installation and Configuration

3.1 Extract to Target Directory

# Extract to /opt/python (recommended for system-wide installation)
# Note: Replace the filename with the actual downloaded archive name
sudo tar -xzf cpython-3.14.7+20260825-x86_64-unknown-linux-gnu-install_only.tar.gz -C /opt/

# Check extracted directory structure
ls -la /opt/python/

3.2 Verify Extraction

# Confirm Python executable exists
/opt/python/bin/python3 --version

# Expected output: Python <installed version>

:gear: 4. Environment Variable Configuration

4.1 Add Standalone Python to PATH

Option 1: Configure for Current User (Recommended)

# Append to ~/.bashrc
echo 'export PATH=/opt/python/bin:$PATH' >> ~/.bashrc

# Apply changes immediately
source ~/.bashrc

Option 2: Configure for All Users (Requires root)

# Create system-wide config file (e.g., python-standalone.sh)
echo 'export PATH=/opt/python/bin:$PATH' > /etc/profile.d/python314.sh

# Apply changes
source /etc/profile.d/python314.sh

4.2 Verify PATH Configuration

# Check PATH search order
echo $PATH | tr ':' '\n' | head -5

# Confirm python3 points to standalone version
which python3

# Expected output: /opt/python/bin/python3
# Confirm version
python3 --version

# Expected output: Python <installed version>

:counterclockwise_arrows_button: 5. Switching Between Python Versions

5.1 Use Standalone Python (Default)

# Use python3 directly (already prioritized via PATH)
python3 --version

5.2 Use System Python

# Option 1: Use absolute path
/usr/bin/python3 --version

# Option 2: Temporarily override PATH (current session only)
PATH=/usr/bin:$PATH python3 --version

5.3 Restore System Python as Default

# Option 1: Comment out PATH line in ~/.bashrc
sed -i 's/^export PATH=\/opt\/python\/bin/#&/' ~/.bashrc
source ~/.bashrc

# Option 2: Remove system-wide config (requires root)
rm -f /etc/profile.d/python314.sh

# Re-login for changes to take effect

5.4 Version Switching Comparison Table

Action Command Used Version
Use standalone Python by default python3 Standalone version
Force use of system Python /usr/bin/python3 System Python
Temporarily switch to system version PATH=/usr/bin:$PATH python3 System Python
Check current path which python3 Shows exact path
List all available versions ls -la /usr/bin/python* /opt/python/bin/python*

:white_check_mark: 6. Functionality Verification

6.1 Verify pip

# Check pip version
pip --version

# Or more precisely:
python3 -m pip --version

# Expected output should include the installed Python version

6.2 Verify Basic Functionality

# Quick test of core modules
python3 -c "import sys, json, hashlib, ssl; print('✅ Basic modules imported successfully')"

6.3 Verify Network and SSL

python3 -c "import urllib.request; print(urllib.request.urlopen('https://www.python.org').status)"

# Expected output: 200

6.4 Verify Third-Party Package Installation

# Install test packages
python3 -m pip install requests numpy

# Verify import
python3 -c "import requests, numpy; print('✅ Third-party packages imported successfully')"

6.5 Verify System Compatibility

# Confirm system tools like dnf/yum still use system Python
dnf list installed python3 2>&1 | head -5

# Should work normally without interference

:file_folder: 7. Directory Structure Explanation

/opt/python/

├── bin/
│   ├── python3          # Main Python interpreter
│   ├── python3.x        # Version-specific link (x = major version)
│   ├── pip3             # pip package manager
│   └── pip3.x           # Version-specific pip

├── lib/
│   └── python3.x/
│       ├── site-packages/  # Directory for third-party packages
│       └── …               # Standard library

└── include/
    └── python3.x/          # Header files (for compiling C extensions)

:pushpin: Note: The 3.x in the paths is illustrative. Actual directory names match the installed Python version.


:wastebasket: 8. Uninstallation Instructions

To uninstall the standalone Python:

# 1. Remove installation directory
sudo rm -rf /opt/python

# 2. Remove environment variable configuration
# Edit ~/.bashrc and remove the line: export PATH=/opt/python/bin:$PATH
# Or remove system-wide config
sudo rm -f /etc/profile.d/python314.sh

# 3. Reload configuration
source ~/.bashrc

# 4. Confirm system Python is restored
python3 --version

# Should display the system's default Python version

:warning: 9. Common Issues and Solutions

9.1 python3 Command Still Uses System Version

Symptom: Running python3 --version shows the old system version.

Solution:

# Check PATH order
echo $PATH | tr ':' '\n' | head -3

# Ensure /opt/python/bin appears before /usr/bin
# Reload configuration
source ~/.bashrc

# Or refresh hash cache
hash -r

9.2 Installed Packages Not Found

Symptom: After pip install, import fails with ModuleNotFoundError.

Solution:

# Use full path to ensure correct Python is used
/opt/python/bin/python3 -m pip install <package-name>

# Or verify which Python pip is using
pip --version

9.3 System Tools (e.g., dnf) Fail with Python Errors

Symptom: dnf or yum reports Python-related errors.

Cause: System tools use absolute paths like /usr/bin/python3, unaffected by PATH. Issues may arise if python3 is aliased incorrectly.

Solution:

# Check if system Python is functional
/usr/bin/python3 --version

# If aliased, remove the alias
unalias python3

9.4 GLIBC_2.XX not found Error

Symptom: Runtime error indicating missing glibc version.

Cause: The downloaded binary requires a higher glibc version than available.

Solution:

  • Ensure you downloaded the x86_64-unknown-linux-gnu universal version (minimum glibc 2.17).
  • If still incompatible, try the musl version (fully statically linked, no glibc dependency).

:books: 10. References