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 the new task and exception groups that Python 3.11 has to offer. Task groups let you organize your asynchronous code better, while exception groups can collect several errors happening at the same time and let you handle them in a straightforward manner.
In this tutorial, you’ll:
- Install Python 3.11 alpha on your computer, next to your current Python installations
- Explore how exception groups can organize several unrelated errors
- Filter exception groups with
except*and handle different types of errors - Use task groups to set up your asynchronous code
- Test smaller improvements in Python 3.11, including exception notes and a new internal representation of exceptions
There are many other improvements and features coming in Python 3.11. Check out what’s new in the changelog for an up-to-date list.
Free Download: Click here to download free sample code that demonstrates some of the new features of Python 3.11.
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 alpha version of Python 3.11 is 3.11.0a7, released on April 5, 2022.
Note: This tutorial uses the seventh alpha version of Python 3.11. You might experience small differences if you use a later version. In particular, a few aspects of the task group implementation are still being discussed. 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 just around the corner, 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:
- Exception groups, which will allow programs to raise and handle multiple exceptions at the same time
- Task groups, to improve how you run asynchronous code
- Enhanced error messages, which will help you more effectively debug your code
- Optimizations, promising to make Python 3.11 significantly faster than previous versions
- Static typing improvements, which will let you annotate your code more precisely
- TOML support, which allows you to parse TOML documents using the standard library
There’s a lot to look forward to in Python 3.11! For a comprehensive overview, check out Python 3.11: Cool New Features for You to Try. You can also dive deeper into some of the features listed above in the other articles in this series:
In this tutorial, you’ll focus on how exception groups can handle multiple unrelated exceptions at once and how this feature paves the way for task groups, which make concurrent programming in Python more convenient. 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 subsection, 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 potential 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 working with Python through Docker, including how to 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. Choose your platform with the switcher below:
Use pyenv install --list to check which versions of Python 3.11 are available. Then, install the latest one:
$ pyenv install 3.11.0a7
Downloading Python-3.11.0a7.tar.xz...
[...]
The installation may take a few minutes. Once your new alpha version is installed, then you can create a virtual environment where you can play with it: