Easy way to upgrade Python on Raspbian

Upgrading Python for OctoPrint on Raspbian GNU/Linux 10 (Buster)

This guide provides step-by-step instructions on how to upgrade your Python version on Raspbian GNU/Linux 10 (Buster) for use with OctoPrint.

Remember to always back up your OctoPrint data before performing major upgrades or system changes.

For safe and flexible Python version management, especially when dealing with system-critical applications, pyenv is the recommended tool. It allows you to install multiple Python versions and switch between them without affecting your system's default Python installation. This has been tested to work with:

  • OctoPrint 1.11.2
  • Python 3.12.3
  • OctoPi* 0.18.0 (build 2022.08.09.123918)

Using pyenv (Recommended)

This method is the safest as it isolates your OctoPrint Python environment from the system's Python, preventing potential conflicts.

Step 1: Install Prerequisites

First, ensure your system is up to date and install necessary build dependencies for compiling Python.


sudo apt update

sudo apt upgrade -y

sudo apt install -y make build-essential libssl-dev zlib1g-dev \

libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncursesw5-dev \

xgcc libgdbm-dev libc6-dev libffi-dev python3-dev python3-pip

Step 2: Install pyenv

Install pyenv using the pyenv-installer script.


curl https://pyenv.run | bash

Step 3: Configure Your Shell for pyenv

Add pyenv to your shell's environment variables. This typically involves adding lines to your ~/.bashrc (for Bash) or ~/.zshrc (for Zsh) file.


echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc

echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc

echo 'eval "$(pyenv init --path)"' >> ~/.bashrc

echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc

source ~/.bashrc

If you are using Zsh, replace ~/.bashrc with ~/.zshrc.

Step 4: Install a Stable Python Version (e.g., 3.12.x)

List available Python versions and then install your desired stable version. For example, to install Python 3.12.3:


pyenv install 3.12.3

You can install other stable versions as needed in the same way. For example, to install Python 3.11.6:


pyenv install 3.11.6

Step 5: Verify Python Installation

Check if the newly installed Python version is recognized by pyenv.


pyenv versions

This should list the Python version you just installed.

Step 6: Stop OctoPrint

Before migrating, stop your OctoPrint service. The exact command may vary depending on your OctoPrint installation (e.g., OctoPi, manual install).


sudo systemctl stop octoprint

# Or if you run it manually:

# pkill -f octoprint

Step 7: Install OctoPrint's venv-tool

Download and make the octoprint-venv-tool executable.


curl -LO https://get.octoprint.org/octoprint-venv-tool && chmod +x octoprint-venv-tool

Step 8: Recreate OctoPrint's Virtual Environment

Navigate to your OctoPrint installation directory. This is typically /home/pi/OctoPrint or /opt/octoprint if you followed a standard OctoPi setup or a manual installation guide. You will need to find the path to your OctoPrint virtual environment (e.g., ~/oprint or venv within the OctoPrint directory).

Important: Replace /path/to/your/octoprint/venv with the actual path to your OctoPrint virtual environment and /home/pi/.pyenv/versions/3.12.3/bin/python with the path to the Python executable installed by pyenv.


# Example for OctoPi default venv path and Python 3.12.3

./octoprint-venv-tool recreate-venv /home/pi/oprint --python /home/pi/.pyenv/versions/3.12.3/bin/python

The venv-tool will attempt to reinstall all plugins into the new virtual environment.

Step 9: Start OctoPrint and Verify

Start your OctoPrint service again.

sudo systemctl start octoprint

After OctoPrint starts, open its web interface. In the lower-left corner of the OctoPrint web interface, you should see the Python version it is currently running under. It should now reflect the version you installed (e.g., Python 3.12.3).

Possible Issue: User password invalid for WebUI

Once the web interface is loaded and you encounter a bad username password error you will need to run the following command to reset the passoword for the username you are using.

Activate the virtual environment. On OctoPi:

source ~/oprint/bin/activate

Next run the following command to reset the password for the user you are using.

octoprint user password <user>
# (ex: octoprint user password MyUserName)

It will prompt you for a new password and then set it on the user.

Restart OctoPrint for your changes to take effect, using:

sudo service octoprint restart or similar.

I have tested this on three other installs and all does seem to work, but my setup might be different than yours so always have a backup!

-Enjoy!

1 Like

I have a standard Pi4 install and tried to follow this procedure but getting the following failure during setup.py;
Preparing metadata (setup.py): started
Preparing metadata (setup.py): finished with status 'error'

stderr:
error: subprocess-exited-with-error

Γ— python setup.py egg_info did not run successfully.
β”‚ exit code: 1
╰─> [6 lines of output]
Traceback (most recent call last):
File "", line 2, in
File "", line 35, in
File "/tmp/pip-req-build-w09iyee5/setup.py", line 6, in
from distutils.msvccompiler import MSVCCompiler
ModuleNotFoundError: No module named 'distutils.msvccompiler'
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
Any advice?

@GA_Dutchman - That looks like a package dependency wasn't installed. I'm not sure if this will help you but it might help others.

To everyone:
I encountered a couple small issues while following thist that are worth bringing up.

First and foremost: Make sure you are logged in as the pi user and not anything else. You need to do this as that user.

In Step 1, the list of dependencies isn't quite right and can result in some issues depending on what exactly is missing. Pyenv keeps a list of what's needed on their wiki. Go to this link and scroll until you see the "Ubuntu/Debian/Mint" bullet. You can directly copy-paste what's there.

In Step 3, pyenv suggests a slightly different second line after it installs. Compare what in this step with what pyenv suggests and change what you see to match pyenv's output, as it's built to be more flexible.

In Step 4, you can install the latest patch of a python version by leaving off the final number. Example: pyenv install 3.12 will install the latest version of 3.12 (.12 as of this post).

After Step 4, I highly recommend you change the global version of python to match what you intend to use for OctoPrint using pyenv global VERSION where VERSION is the version that you just installed. Example: pyenv global 3.12.12. This way, you can be sure that anything executed from a bash script is going to use the same python version.

Final note: Be patient. Some of this stuff will seem to take a long time and look like it's hung up. Let it sit. Trust me. It will eventually finish or it will error out.

If you get an error, google it. There's a good chance someone knows the answer.

as I am looking over the above steps, at what step does the setup.py fail? tring to recreate the issue.