Python 3.11 Guide: Speedups, New Features, Installation, Compatibility

目次

1. What is Python 3.11? [Overview and Release Background]

The Emergence of Python 3.11 and the Background of Attention

Python 3.11 is one of the latest versions in the Python 3 series, officially released on October 24, 2022. This version focuses significantly on performance improvements and enhanced development experience compared to the previous Python 3.10, with numerous enhancements added.

Python has been supported in various fields such as web development, data analysis, AI, and automation due to its ease of use and rich library ecosystem. Among them, Python 3.11 emphasizes “faster and clearer,” evolving into a more refined language specification through dramatic improvements in execution speed and further advancements in type hints.

Development Background and CPython Team’s Efforts

The development of Python 3.11 was primarily led by the CPython (Python’s official implementation) team, with performance improvements as the primary goal. In particular, the “Faster CPython” project by Google Python developer Mark Shannon was a central initiative.

This project aims for “5x speed improvement” in the future, and with 3.11, it achieved up to 60% faster performance as the first step. This is intended to elevate Python to a level where it can compete with other languages in terms of execution speed.

Differences from Python 3.10: What Has Changed Significantly?

Python 3.11 has the following major changes compared to the previous version 3.10.

  • Significant improvement in execution speed (CPython optimization)
  • Enhanced type hint functionality (Self, TypeVarTuple, etc.)
  • Introduction of new syntax (addition of exception groups)
  • Native support for TOML files

While Python 3.10 was characterized by “syntax enhancements,” Python 3.11 shifts focus to “execution performance” and “ease of development,” making it a practical update.

Why Should You Use Python 3.11 Now?

Python 3.11 not only adds new features but also includes a wealth of practical improvements useful in actual development environments, such as “faster execution speed” and “improved type safety.” In particular, in fields requiring execution performance like business applications and AI models, migration from older versions is recommended.

Additionally, the Python community is advancing library updates assuming version 3.11, and it is likely to become the future standard.

2. [Core of Acceleration] Performance Improvements in Python 3.11

Up to 60% Speedup: What Are the Reasons?

The biggest highlight of Python 3.11 is, without a doubt, the improvement in processing speed. According to the official announcement, compared to Python 3.10, an average speedup of around 25% and up to 60% has been achieved. This is an evolution that directly impacts the perceived speed in a wide range of scenarios, from everyday script execution to large-scale data processing.

This speed improvement goes beyond simple compiler optimizations and was achieved by overhauling the internal processing of CPython itself, which forms the basis of Python.

What is the Faster CPython Project?

Behind this speedup is the existence of the aforementioned “Faster CPython project.” Led by Google’s Mark Shannon, it aims to dramatically improve Python’s execution speed through phased optimizations.

In Python 3.11, the following specific improvements were made:

  • Interpreter’s instruction set overhaul
    Bytecode instructions have been organized and simplified, enabling more efficient execution.
  • Implementation of “Zero-cost exception handling”
    The exception handling mechanism has been lightweighted, reducing the performance impact even when using try-except statements.
  • Speedup of function calls
    The processing for calling Python functions has been simplified, significantly speeding up the execution of function-based code.
  • Internal caching for high-frequency operations
    Frequently used operations such as attribute access and comparison operations have been optimized.

These improvements are highly attractive to developers because they make Python code faster without requiring major rewrites—just use it as is.

Benchmark Comparison: Python 3.10 vs 3.11

The following is an example of comparison results using the “pyperformance” benchmark suite provided by the official Python:

Test ContentPython 3.10Python 3.11Improvement Rate
File Reading1.00 seconds0.78 secondsApproximately 22% improvement
JSON Parsing0.95 seconds0.70 secondsApproximately 26% improvement
Complex Recursion Processing2.40 seconds1.45 secondsApproximately 40% improvement
Regular Expression Matching1.20 seconds0.85 secondsApproximately 29% improvement

*Note: Values may vary depending on the environment and code, but overall, significant improvements are observed.

Impact on the Development Field

Python has often been called “slow,” but with the advent of 3.11, its weaknesses in execution performance are being significantly improved. The value of using Python has increased even further in all kinds of fields, such as web apps, batch processing, machine learning pipelines, and more.

In particular, achieving this performance without using JIT (Just-In-Time) compilation is an important point for fields that prioritize stability and portability.

The Wave of Speedups Continuing into Python 3.12 and Beyond

The Faster CPython project is planned to continue with Python 3.12, 3.13, and beyond. In the future, further considerations include the introduction of JIT-like approaches and revisions to GC (garbage collection).

Python is steadily evolving into a language that is not only easy to write but also fast.

年収訴求

3. Summary of Major New Features in Python 3.11

Introduction of New Exception Groups (ExceptionGroup)

In traditional Python, it has been difficult to handle multiple exceptions simultaneously. Python 3.11 addresses this by introducing the ExceptionGroup class and the except* syntax.

This feature is particularly powerful in cases where multiple exceptions occur simultaneously in asynchronous or parallel processing.

try:
    raise ExceptionGroup("Multiple Exceptions", [ValueError("Invalid value"), TypeError("Type error")])
except* ValueError as ve:
    print(f"ValueError: {ve}")
except* TypeError as te:
    print(f"TypeError: {te}")

This allows handling individual exceptions as a group while managing them by type.

tomllib: Standard Support for TOML Files

The popular TOML format for configuration files is now supported for reading via the standard library tomllib starting from Python 3.11.

This eliminates the need to install external libraries (e.g., the toml package) separately.

import tomllib

with open("config.toml", "rb") as f:
    config = tomllib.load(f)
print(config["database"]["user"])

This provides significant convenience for Python projects that use pyproject.toml and similar files.

Enhancements to Type Hints: Self, TypeVarTuple, Required/NotRequired

In Python 3.11, static typing (typing) features are further enhanced, making complex type specifications and safer designs easier.

  • Self: Explicitly indicates the type returned by a method as its own instance.
  from typing import Self

  class Builder:
      def set_option(self) -> Self:
          return self
  • TypeVarTuple: Supports variable-length generic types. It can be used for data structures like tuples with variable lengths (applications are somewhat advanced).
  • Required / NotRequired: When combined with TypedDict, explicitly indicates required/optional fields.
  from typing import TypedDict, NotRequired

  class User(TypedDict):
      name: str
      email: NotRequired[str]

These features dramatically improve the expressiveness of type-safe code.

asyncio.TaskGroup Addition and Improvements to Asynchronous Processing

The asynchronous processing library asyncio has also seen major improvements. In particular, the new TaskGroup allows grouping multiple asynchronous tasks, significantly improving code readability.

import asyncio

async def fetch_data(n):
    await asyncio.sleep(n)
    print(f"Completed after {n} seconds")

async def main():
    async with asyncio.TaskGroup() as tg:
        tg.create_task(fetch_data(1))
        tg.create_task(fetch_data(2))
        tg.create_task(fetch_data(3))

asyncio.run(main())

Compared to asyncio.gather(), this code is more structured and enables robust descriptions including exception handling.

Other Minor Improvements

  • Improved readability of stack traces (error messages) (relevant code lines are displayed more clearly)
  • Clearer error message hints (improvements to “Did you mean…?”)
  • Numerous small optimizations to the standard library and syntax

Quick Summary: New Features Evolve Toward “Speed” and “Clarity”

The new features in Python 3.11 show designs aimed at balancing performance and readability throughout. They include many highly practical enhancements with minimal impact on existing code, making it a version worth actively considering for adoption.

4. [Environment Setup] How to Install Python 3.11 and Recommended Tools

4-1. Python 3.11 Installation Methods by OS

For Windows

  1. Access the official website (https://www.python.org)
  2. Click “Download Python 3.11.x” on the top page
  3. Run the installer and check “Add Python to PATH”
  4. Click “Install Now” to install

After installation, run the following in the command prompt to verify it works:

python --version

Python 3.11.x will be displayed if it’s complete.

For macOS (Homebrew)

For macOS users, installation using the package manager Homebrew is convenient.

brew install python@3.11

If using multiple versions, you may also need to switch links:

brew link --overwrite python@3.11

For Linux (Ubuntu-based)

For Ubuntu or Debian-based Linux, the official repositories may not support 3.11, so building from source or using pyenv is common.

sudo apt update
sudo apt install -y build-essential libssl-dev zlib1g-dev libncurses5-dev libncursesw5-dev libreadline-dev libsqlite3-dev libgdbm-dev libdb5.3-dev libbz2-dev libexpat1-dev liblzma-dev tk-dev

cd /usr/src
sudo wget https://www.python.org/ftp/python/3.11.x/Python-3.11.x.tgz
sudo tar xzf Python-3.11.x.tgz
cd Python-3.11.x
sudo ./configure --enable-optimizations
sudo make -j$(nproc)
sudo make altinstall

python3.11 --version can be used to check the version.

4-2. Creating Virtual Environments and Managing Python Versions

Creating Virtual Environments with venv

Python 3.11 also includes the standard venv module, allowing you to create independent environments for each project.

python3.11 -m venv myenv
source myenv/bin/activate  # On Windows: myenv\Scripts\activate

By operating Python within the virtual environment, you can develop without affecting other projects.

Using pyenv (Multi-Version Management)

For developers who want to switch between multiple Python versions, we strongly recommend installing pyenv.

# Install pyenv (Linux/macOS)
curl https://pyenv.run | bash

# Check installable versions
pyenv install --list

# Install and set Python 3.11
pyenv install 3.11.x
pyenv global 3.11.x

This method allows flexible switching of versions for each project.

mise (formerly: asdf-python + direnv) as an Option

Recently, as a more modern version management tool, mise (formerly asdf + direnv integration) is also gaining popularity. .tool-versions files allow explicit specification of the Python version, making it suitable for team development.

5. [Leveraging in Development] Practical Code Examples Using New Features

Example 1: Using New Exception Groups and except*

Introduced in Python 3.11, the ExceptionGroup class and except* syntax are effective for scenarios where multiple exceptions occur simultaneously in parallel processing.

def raise_multiple_exceptions():
    raise ExceptionGroup("Multiple Exceptions", [
        ValueError("Invalid value"),
        TypeError("Type error"),
        RuntimeError("Runtime error")
    ])

try:
    raise_multiple_exceptions()
except* ValueError as ve:
    print("ValueError occurred:", ve)
except* TypeError as te:
    print("TypeError occurred:", te)

Thus, the major feature is the ability to branch-process multiple type-specific exceptions in parallel within a single try block.

Example 2: Simplifying Class Design Using Self

Previously, it was difficult to accurately express the return type when using method chaining, but with the introduction of typing.Self in Python 3.11, it became possible to write safer and more self-descriptive code.

from typing import Self

class ConfigBuilder:
    def __init__(self):
        self.config = {}

    def set(self, key: str, value: str) -> Self:
        self.config[key] = value
        return self

    def build(self) -> dict:
        return self.config

cfg = ConfigBuilder().set("host", "localhost").set("port", "3306").build()
print(cfg)

Even with chain methods, type completion works, significantly improving IDE assistance and type checking accuracy.

Example 3: Utilizing NotRequired in TypedDict

One of the attractions of 3.11 is that you can now express optional fields in dictionary-type data structures using types.

from typing import TypedDict, NotRequired

class UserProfile(TypedDict):
    username: str
    email: NotRequired[str]

def show_user(profile: UserProfile):
    print("Username:", profile["username"])
    if "email" in profile:
        print("Email:", profile["email"])

show_user({"username": "nao"})
show_user({"username": "nao", "email": "nao@example.com"})

This is also useful for dictionary-type API validation, making type hints more flexible.

Example 4: Organizing Asynchronous Code with asyncio.TaskGroup

In Python’s asynchronous processing, asyncio is the standard, but in Python 3.11, batch management with task groups (TaskGroup) finally became possible.

import asyncio

async def fetch_data(n):
    await asyncio.sleep(n)
    print(f"Completed after {n} seconds")

async def main():
    async with asyncio.TaskGroup() as tg:
        tg.create_task(fetch_data(1))
        tg.create_task(fetch_data(2))
        tg.create_task(fetch_data(3))

asyncio.run(main())

This code is more structured compared to asyncio.gather(), and robust description including exception handling became possible.

6. [Migration Points] Upgrading to Python 3.11 and Compatibility Checks

Basic Upgrade Policy: Use Virtual Environments

When introducing Python 3.11 into an existing project, it is a golden rule to use virtual environments to avoid breaking the existing environment. The following are the recommended steps:

  1. Install Python 3.11 on the system
  2. Create a virtual environment:
   python3.11 -m venv venv311
   source venv311/bin/activate  # On Windows: venv311\Scripts\activate
  1. Reinstall libraries (using requirements.txt):
   pip install -r requirements.txt

By separating the environments with this method, safe migration to 3.11 becomes possible.

How to Check Library Compatibility

When upgrading to Python 3.11, the most important thing is to check whether the libraries in use are compatible with the new version. There are several ways to check:

1. Check the Official PyPI Page

Access the PyPI page for each library and check descriptions such as “Requires: Python >= X.X”.

2. Check pip Dependency Errors

Errors that occur when installing libraries in a virtual environment will clearly indicate compatibility.

3. caniusepython3 Batch Check with Command (※Partially Deprecated)

Some developers use the caniusepython3 package to investigate compatibility, but since updates have stopped recently, ultimately, explicit verification on PyPI is recommended.

Representative Examples of Incompatibilities: Changed Features or Deprecated Syntax

Although there are few major breaking changes in Python 3.11, pay attention to the following points:

  • Incompatibility of Some C Extension Modules
    If Cython or NumPy are not yet compatible, there is a possibility of build failures.
  • Features Transitioned from Deprecated to Removed
    For example, there are libraries where collections.MutableMapping and the like have been completely removed.
  • Stricter Syntax
    Since grammar error detection has become stricter, code that previously only issued warnings may now raise exceptions.

Don’t Forget to Review Tests and CI/CD

After migration, run the existing test code to confirm there are no issues in the new environment. Especially if using CI/CD, don’t forget to add 3.11 to the target Python versions for builds.

Example: Specification in GitHub Actions (python-version)

- uses: actions/setup-python@v4
  with:
    python-version: "3.11"

Migration Best Practices

What to DoReasons/Effects
Verify operation in virtual environmentUpgrade safely without breaking existing environment
Investigate compatibility of dependent librariesPrevent installation errors and runtime errors
Run unit tests and CI in advanceEarly detection and prevention of bugs
Replace deprecated APIsEnsure long-term maintainability

Summary: Careful Migration Yields Great Benefits

Migrating to Python 3.11, if you don’t neglect prior preparation and compatibility checks, will yield very high returns. To enjoy benefits like improved speed and clearer code, gradual migration and establishing a testing framework are important.

7. Frequently Asked Questions (FAQ)

Q1. Is Python 3.11 recommended for beginners?

Yes, Python 3.11 is highly recommended for beginners as well. Compared to previous versions, it has faster execution speed and improved readability of error messages, making it easier to notice mistakes during learning. Additionally, you can develop using the conventional syntax without using complex new features.

Q2. How do I upgrade from Python 3.10 to 3.11?

To avoid affecting the existing environment, we recommend first verifying using a virtual environment. The following steps are general:

  1. Install Python 3.11 (using the official site or pyenv, etc.)
  2. Create a virtual environment and set it to use 3.11
  3. pip install -r requirements.txt to rebuild the libraries
  4. Verify operation with test scripts

Q3. Are there libraries that do not yet support Python 3.11?

Since 2023, many major libraries (NumPy, pandas, Flask, Django, etc.) have been compatible, but some older libraries or those dependent on C extensions may be delayed in support. It is reliable to check the notation “Requires: Python >=3.11” on PyPI or to experimentally install and verify in a virtual environment.

Q4. Do I have to use the new features of Python 3.11?

You don’t need to use all of them. It’s fine to adopt only the necessary features according to the project’s or team’s objectives. For example, TaskGroup is convenient in scenarios with heavy asynchronous processing, but there’s no need to force its use in typical business applications.

Q5. For new development starting now, which is better, Python 3.10 or 3.11?

We recommend Python 3.11. In addition to speed improvements, it offers many new features with high maintainability, and future library support is expected to mainstream from 3.11 onward. Since a stable environment is ready at present, starting from 3.11 is ideal.

Q6. Is preparation for Python 3.12 or future versions necessary?

Yes. Since Python releases new versions annually, avoiding deprecated APIs and getting used to using type hints will prepare you for the future. Additionally, regularly checking the documentation to understand deprecated features will provide peace of mind.

Q7. Is it true that code runs faster with Python 3.11?

It’s true. Due to internal optimizations in CPython, official benchmarks report an average 25% speedup compared to 3.10, and up to 60% in some cases. Even running existing code as is may benefit from this.

Q8. In what use cases are the new features of Python 3.11 useful?

New FeatureCommon Use Cases
ExceptionGroupException handling in parallel or asynchronous processing
TaskGroupSimplification of code for complex asynchronous processing
SelfMethod chaining or builder patterns
NotRequiredFlexible design of JSON/API response types
tomllibReading configuration files (TOML)

8. Summary and Future Outlook

Python 3.11: An Evolved Version Pursuing “Speed” and “Clarity”

Python 3.11 is a version with many clear evolution points compared to previous versions. In particular, the following three points are improvements that resonate strongly with many developers:

  • Up to 60% speedup, reducing performance bottlenecks
  • Enhanced exception handling and asynchronous control, enabling robust application design
  • Strengthened type hinting features, making static analysis and IDE support more powerful

Furthermore, the fact that these improvements can be used without significantly rewriting existing code is a major appeal. It can be said to be an update that embodies Python’s philosophy of “simplicity” and “practicality.”

Future Outlook: Toward Python 3.12 and Beyond

The Python development team will continue the “Faster CPython” project in the future, aiming for further speed improvements, stability, and modernization.

Key points to watch in the future are as follows:

  • Introduction of JIT compilation (planned)
    A more dynamically optimized execution environment is being conceptualized.
  • Memory management (GC) improvements
    Scalability enhancements are expected in large-scale systems.
  • Further evolution of error messages and type inference
    Consideration for beginners and improvements to the development experience are planned to continue.

Through these advancements, Python will continue to evolve from a “simple but slow language” to a “simple and fast language.”

Recommendations for Readers

Python 3.11 is already widely used as a stable version. The following actions are recommended.

The insights gained through migration and testing will surely be useful for future version upgrades.

In Closing

Python 3.11 brings many valuable improvements that enrich the developer experience. It is the optimal choice for those thinking, “It’s about time to upgrade versions” or “I want to write more modern code.”

Take this opportunity to pick up Python 3.11 and experience its appeal firsthand.

RUNTEQ(ランテック)|超実戦型エンジニア育成スクール