The Raspberry Pi is made for running programme and projects written in Python. The standard (of the May 2020 release of the Raspberry Pi OS) ships with Python 2.7.1 which might not meet the requirements need. This How-to-Do installs Python version 3.7.4 but should work for 3.8.x as well. As we will install from source code we will need the development environment if not installed already to compile the binaries. Before we start and to avoid any confusion (unless for experimental reasons you need multiple version of Python installed) it is advisable to remove any old versions:
apt list –installed sudo apt-get autoremove python*
This followed by ensuring the system is up-to-date:
sudo apt-get update
In the next step we install the development tools and any dependencies for building the binaries:
sudo apt-get install -y build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev libffi-dev
Now we are good to go to get the source code:
wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tar.xz tar xf Python-3.7.4.tar.xz cd Python-3.7.4
Configuring and compiling (This can take hours depending on your Raspberry’s performance and configuration):
./configure –prefix=/usr/local/opt/python-3.7.4 make -j 4
Installing the new Python version:
sudo make altinstall
Configuration the environment, create aliases and some housekeeping:
sudo ln -s /usr/local/opt/python-3.7.4/bin/pydoc3.7 /usr/bin/pydoc3.7.4 sudo ln -s /usr/local/opt/python-3.7.4/bin/python3.7 /usr/bin/python3.7.4 sudo ln -s /usr/local/opt/python-3.7.4/bin/python3.7 /usr/bin/python sudo ln -s /usr/local/opt/python-3.7.4/bin/python3.7m /usr/bin/python3.7.4m sudo ln -s /usr/local/opt/python-3.7.4/bin/pyvenv-3.7 /usr/bin/pyvenv-3.7.4 sudo ln -s /usr/local/opt/python-3.7.4/bin/pip3.7 /usr/bin/pip3.7.4 sudo ln -s /usr/local/opt/python-3.7.4/bin/pip3.7 /usr/bin/pip alias python=’/usr/bin/python3.7.4′ alias python3=’/usr/bin/python3.7.4′ ls /usr/bin/python* cd .. sudo rm -r Python-3.7.4 rm Python-3.7.4.tar.xz . ~/.bashrc
And finally check if everything works:
python -V