Python 3.11 Preview: Even Better Error Messages

Python

Python 3.11 will be released in October 2022. Even though October is still months away, you can already preview some of the upcoming features, including how Python 3.11 will offer more readable and actionable error messages.

In this tutorial, you’ll:

Install Python 3.11 Alpha on your computer, next to your current Python installations
Interpret the improved error messages in Python 3.11 and learn to more efficiently debug your code
Connect these improvements to the PEG parser and the better error messages in Python 3.10
Explore third-party packages offering enhanced error messages
Test smaller improvements in Python 3.11, including new math functions and more readable fractions

There are many other improvements and features coming in Python 3.11. Keep track of what’s new in the changelog for an up-to-date list.

Free Bonus: 5 Thoughts On Python Mastery, a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.

Python 3.11 Alpha

A new version of Python is released in October each year. The code is developed and tested over a seventeen-month period before the release date. New features are implemented during the alpha phase, which lasts until May, about five months before the final release.

About once a month during the alpha phase, Python’s core developers release a new alpha version to show off the new features, test them, and get early feedback. Currently, the latest version of Python 3.11 is 3.11.0 alpha 5, released on February 3, 2022.

Note: This tutorial uses the fifth alpha version of Python 3.11. You might experience small differences if you use a later version. However, you can expect most of what you learn here to stay the same through the alpha and beta phases and in the final release of Python 3.11.

The first beta release of Python 3.11 is planned for May 6, 2022. Typically, no new features are added during the beta phase. Instead, the time between the feature freeze and the release date is used to test and solidify the code.

Cool New Features

Some of the currently announced highlights of Python 3.11 include:

Enhanced error messages, which will help you more effectively debug your code
Exception groups, which will allow programs to raise and handle multiple exceptions at the same time
Optimizations, promising to make Python 3.11 significantly faster than previous versions
Static typing improvements, which will let you annotate your code more precisely

There’s a lot to look forward to in the upcoming Python 3.11 release! In this tutorial, you’ll focus on how the enhanced error reporting can improve your developer experience by letting you debug your code more efficiently. You’ll also get a peek at some of the other, smaller features that’ll be shipping with Python 3.11.

Installation

To play with the code examples in this tutorial, you’ll need to install a version of Python 3.11 onto your system. In this section, you’ll learn about a few different ways to do this: using Docker, using pyenv, or installing from source. Pick the one that works best for you and your system.

Note: Alpha versions are previews of upcoming features. While most features will work well, you shouldn’t depend on any Python 3.11 alpha version in production or anywhere else where bugs will have serious consequences.

If you have access to Docker on your system, then you can download the latest version of Python 3.11 by pulling and running the python:3.11-rc-slim Docker image:

$ docker pull python:3.11-rc-slim
Unable to find image ‘python:3.11-rc-slim’ locally
latest: Pulling from library/python
[…]

$ docker run -it –rm python:3.11-rc-slim

This drops you into a Python 3.11 REPL. Check out Run Python Versions in Docker for more information about how you can work with Python through Docker, including how you can run scripts.

The pyenv tool is great for managing different versions of Python on your system, and you can use it to install Python 3.11 Alpha if you like. It comes with two different versions, one for Windows and one for Linux and macOS:

Windows

Linux + macOS

On Windows, you can use pyenv-win. First update your pyenv installation:

C:> pyenv update
:: [Info] :: Mirror: https://www.python.org/ftp/python
[…]

Doing an update ensures that you can install the latest version of Python. You could also update pyenv manually.

On Linux and macOS, you can use pyenv. First update your pyenv installation, using the pyenv-update plugin:

$ pyenv update
Updating /home/realpython/.pyenv…
[…]

Doing an update ensures that you can install the latest version of Python. If you don’t want to use the update plugin, you can update pyenv manually.

Use pyenv install –list to check which versions of Python 3.11 are available. Then, install the latest one:

$ pyenv install 3.11.0a5
Downloading Python-3.11.0a5.tar.xz…
[…]

Read the full article at https://realpython.com/python311-error-messages/ »

[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]