Updating Python in Companion

Hi @farhang,

Companion 0.0.28 still uses Raspbian Jessie, so the default Python 3 install is 3.4. That doesn’t stop you from installing a later version separately if you want to, but be aware it’s a few steps and takes a while for it to compile. I’ve provided instructions below if you’re interested in doing so.

As a timing indication, expect the full process to take ~30-60 minutes.

Requires Internet Connection

Check that your companion computer is connected to the internet. On the companion web interface Network page your “Internet Status” should say connected.

Connect to the Raspberry Pi

You can either use the terminal over browser functionality, or you can ssh in (default password is companion).

Stop Companion Functionality

Compiling code wants all the CPU it can get - close the non-essential screen sessions to allow compiling to have more resources. Keep the web-related sessions open to allow webterminal use, and monitoring CPU usage and temperature with companion web interface.

for SESSION in $(screen -ls | grep "(" | cut -f2 | grep -v web)
do
    screen -S $SESSION -X quit
done

Update Raspbian

Get the latest list of packages. Includes the main backport forJessie, so a suitable SSL can be installed.

# add the required key for jessie archives
gpg --keyserver keyserver.ubuntu.com --recv-key 7638D0442B90D010
gpg -a --export 7638D0442B90D010 | sudo apt-key add -
# add a source for the 'main' jessie backport
echo "deb http://archive.debian.org/debian/ jessie-backports main" | sudo tee /etc/apt/sources.list.d/jessie-backports.list
# ensure apt is pointing to a valid url for the required Raspbian sources
sudo sed -i 's/mirrordirector/legacy/g' /etc/apt/sources.list
# get the list of available packages, ignoring the expired jessie release-key
sudo apt -o Acquire::Check-Valid-Until=false update

Install Prerequisites

Installing Python on Linux is much smoother if some dependencies are installed first

sudo apt install -y build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev libffi-dev uuid-dev lzma-dev
# required for SSL/pip to work
sudo apt install -t jessie-backports libssl-dev=1.0.2l-1~bpo8+1

Download and Extract Python

# specify which version you want to install
VERSION=3.8.11
# download it
wget https://www.python.org/ftp/python/${VERSION}/Python-${VERSION}.tgz
# uncompress it
sudo tar zxf Python-${VERSION}.tgz

Make and Install

# enter the source folder
cd Python-${VERSION}
# configure for building (checks for prerequisites, takes a little while) 
sudo ./configure --enable-optimizations
# build the source - expected to be the longest step
# !WARNING! if RPi is enclosed use -j2 or no -j flag to avoid overheating/throttling
sudo make -j4
# install as an 'alternative' (doesn't overwrite existing python3)
# If you want to overwrite it you can do `sudo make install` instead,
#   but that may cause issues with existing libraries not being carried 
#   over - I haven't tried it
sudo make altinstall

Confirm working

The new python install should be available by the major version number. You can check that it’s working by running python3.x -V and seeing if the top line is what you tried to install (e.g. for python 3.8.11 you can run python3.8 -V).

Note there may be a bunch of messages like profiling:...: Cannot open - these are related to the source and will be removed in the next step.

Clean up

# exit the Python source directory
cd ..
# remove the compressed and extracted Python source
sudo rm -rf Python-${VERSION}*

Usage

  • From here you can run code using your new Python version by calling it with the major version number (e.g. python3.8 my_script.py).
  • If you prefer executing scripts directly you can set the shebang to #! /usr/local/bin/python3.8 (or whichever version you’re using)
  • For installing libraries pip is also accessible via the major version number (e.g. pip3.8 install ...), or you can use python3.8 -m pip install ....

The normal companion functionality will be available again after restarting the raspberry pi.

1 Like