Use the following instructions to install an older version of Python on Raspberry Pi OS.
Open Menu -> Programming -> Visual Studio Code and create a file with the following script. Save the file in Documents with the nameinstall_python.sh
#!/bin/bash# Set the Python version to installPYTHON_VERSION=3.8.16# Update package list and upgrade existing packagessudoaptupdate#sudo apt upgrade -y# Install dependencies for building Python from sourcesudoaptinstall-ybuild-essentialtk-devlibncurses5-devlibncursesw5-devlibreadline6-devlibdb5.3-devlibgdbm-devlibsqlite3-devlibssl-devlibbz2-devlibexpat1-devliblzma-devzlib1g-devlibffi-dev# Download and extract the specified version of Python source codewgethttps://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tgztarxvfPython-$PYTHON_VERSION.tgz# Navigate to the extracted directorycdPython-$PYTHON_VERSION# Configure the build with optimizations and shared library support./configure--enable-optimizations--enable-shared# Build and install the specified version of Pythonmake-j$(nproc)sudomakealtinstall# Clean up downloaded files and extracted directorycd..rm-rfPython-$PYTHON_VERSION.tgzPython-$PYTHON_VERSION# Add /usr/local/lib at the end of the fileecho"/usr/local/lib"|sudotee-a/etc/ld.so.conf# Run ldconfig to update the shared library cachesudoldconfig# The update-alternatives command requires only major version of python so:PYTHON_MAJOR=$(echo $PYTHON_VERSION |cut-d'.'-f1,2)echo $PYTHON_MAJOR # will output "python3.8"# Set Python 3.8 as default Python3sudoupdate-alternatives--install/usr/bin/python3python3/usr/local/bin/python$PYTHON_MAJOR 1sudoupdate-alternatives--setpython3/usr/local/bin/python$PYTHON_MAJOR
Open the Terminal and execute the following commands one by one. The install_python.sh script will install Python 3.8.16. The installation may take several minutes.