Upgrading to Python 3.12 on Your Raspberry Pi

An Exciting Adventure!

Why Python 3.12, and Why on Raspberry Pi?

Hey there, it's iTheo! Look, if you're anything like me—a Python developer who lives for home automation, energy solutions, and climate sustainability—then you'll understand why I find Python 3.12 an absolute game-changer. I've been tinkering with Python on my Raspberry Pi for a while, mainly for my smart home projects. The thing about Raspberry Pi is that it's not just a tiny computer; it's a sandbox for your imagination! And when fueled by Python 3.12? Oh boy, you're in for a ride.

The Journey to Python 3.12

I've been waiting eagerly for Python 3.12 for quite a while. The day it was released, you bet I was one of the first to download it. Why? Because the improvements were right up my alley—enhanced f-string parsing, improved error messages, support for isolated subinterpreters (just imagine the parallel processing possibilities for home automation), and the list goes on!

So how did I go about installing it? Let's dig in!

Step-by-Step Guide to Installing Python 3.12

Prerequisites

First, ensure your Raspberry Pi OS is up-to-date:

sudo apt update && sudo apt upgrade -y

Download Python 3.12 Source

Navigate to Python's official website and grab the Python 3.12 source code.

wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz

Extract and Navigate to Directory

tar -xf Python-3.12.0.tgz
cd Python-3.12.0

Configure and Compile

Run the following commands to configure and compile Python 3.12.

./configure --enable-optimizations
make -j 4
sudo make altinstall

And voila! Python 3.12 should now be installed.

If you want you can also use my automated script, but be aware it will set python 3.12 to you default python version. And that is not always a good thing.

Wanna try anyway?

wget -qO - https://raw.githubusercontent.com/tvdsluijs/sh-python-installer/main/python.sh | sudo bash -s 3.12

What's New in Python 3.12?

New Features

  1. More Flexible f-string Parsing (PEP 701)
    Imagine creating complex formatted strings with ease. This is perfect for logging sensor data in my home automation tasks.

  2. Support for Buffer Protocol in Python Code (PEP 688)
    I was ecstatic about this. Now manipulating binary data is a cinch.

  3. New Debugging/Profiling API (PEP 669)
    Debugging just got a whole lot easier. As an Agile Coach and Product Owner, streamlined debugging means faster deployment cycles.

  4. Isolated Subinterpreters with Separate GILs (PEP 684)
    This was a game-changer for my energy and home automation projects. Multiple tasks in parallel? Yes, please!

  5. Improved Error Messages
    For someone who codes in Python daily, the enhanced error messages are a huge time-saver.

  6. Support for Linux perf Profiler
    Now you can see Python function names directly in Linux perf traces.

  7. Performance Improvements
    With estimated 5% overall performance improvement, my Raspberry Pi feels snappier.

Type Annotations

  1. New Type Annotation Syntax for Generic Classes (PEP 695)
    It simplifies the way you define generic classes, making your code more readable.

  2. Override Decorator for Methods (PEP 698)
    This ensures that the method actually overrides a method in the base class, preventing subtle bugs.

Deprecations

  • Deprecated methods in the unittest module and old modules like smtpd and distutils have been removed.

  • SyntaxWarnings now for invalid backslash escape sequences in strings.

  • Plus, a bunch of other deprecated and broken functions were thrown out.

Concluding Thoughts

So, why did I rush to install Python 3.12 on my Raspberry Pi? The features align perfectly with my professional and hobbyist pursuits. As a Python Developer and Chief Sustainability Officer, I am always looking for ways to optimize energy use, streamline code, and innovate in the realm of sustainability. Python 3.12, with its features like isolated subinterpreters, new debugging API, and more, makes that journey exciting and impactful.

Upgrading to Python 3.12 has opened up a world of opportunities for me. So if you're like me, looking to push the boundaries of what's possible with Python and Raspberry Pi, make the upgrade. Trust me, you won't regret it!

Cheers,
Theo

Did you find this article valuable?

Support Theo van der Sluijs by becoming a sponsor. Any amount is appreciated!