Skip to content
Tutorials Center
My learning

Topics

Lesson 100 of 149 ยท Beyond the Basics

What's New in Python 3.13

A practical tour of Python 3.13 - the new interactive shell, experimental free-threaded mode and JIT, copy.replace, warnings.deprecated, TypeVar defaults, removed modules and what changed afterwards.

Python ships a new feature release every October. Version 3.13 (October 2024) is best known for two experiments that point to the language's future, a build without the Global Interpreter Lock and a first just-in-time compiler, but it also brought a nicer interactive prompt, clearer error colors, a handful of useful library additions and the removal of old modules.

This lesson explains each change, shows the ones you can try right away and tells you what is stable and what is still experimental. The runnable examples here execute on the Python version this site uses, which is newer than 3.13, so everything introduced in 3.13 works in them. Check your own version first with python --version or in code:

Python Which Python is this?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A better interactive interpreter

The REPL (the prompt you get by typing python) was rebuilt in 3.13 with code borrowed from the PyPy project. Highlights:

  • Multi-line editing: a pasted or typed block such as a function stays one unit, and the up arrow recalls the whole block, not one line.
  • Colored prompts and tracebacks by default (the exception names and file locations get colors). Set NO_COLOR=1 to turn the colors off, or PYTHON_COLORS=0.
  • F1 opens a help browser, F2 a history browser you can copy from, and F3 a paste mode that accepts large blocks without running each line.
  • You can type plain exit and quit (without parentheses) and help works the same.

You will mostly notice that the shell feels like a small editor. The older basic REPL is still available with the environment variable PYTHON_BASIC_REPL=1. The details of the standard prompt are in Running Python.

Free-threaded Python (experimental)

CPython has always had a Global Interpreter Lock (GIL): only one thread runs Python bytecode at a time, which keeps the interpreter simple and safe but stops threads from using several CPU cores for computing (see Concurrency). PEP 703 made the GIL optional.

In 3.13 you can install a separate free-threaded build (commonly named python3.13t) where threads really run in parallel. You can also switch the lock back on with the environment variable PYTHON_GIL=1 or the option -X gil=1, and PYTHON_GIL=0 turns it off. The check you ran above, sys._is_gil_enabled(), tells you which mode is active.

Things to know before using it:

  • It is marked experimental in 3.13. Single-threaded code runs noticeably slower than on the normal build, and not every C-extension package supports it yet.
  • Your own code needs the same care any multi-threaded program needs: shared data should be protected with a threading.Lock, or passed through a queue.Queue.
  • Pure-Python CPU-bound work with threads is where it can help, and I/O-bound code already benefits from threads on the normal build.

Later releases keep building on this work, and the free-threaded build moved from experimental to officially supported in Python 3.14.

An experimental JIT compiler

3.13 also includes a first just-in-time (JIT) compiler (PEP 744), a "copy-and-patch" design that turns hot sequences of the interpreter's micro-operations into machine code at run time. It is disabled by default and has to be enabled when building Python (--enable-experimental-jit) and then switched on with PYTHON_JIT=1. In 3.13 the speed gain is small, because the project's goal was to lay groundwork. You do not need to change your code to use it, and you don't need it to write good Python. Performance tips for today's Python are in Performance.

Library additions you can use now

copy.replace()

copy.replace(obj, **changes) makes a copy of an object with some fields changed. It works with dataclasses, named tuples, datetime objects and any class that defines a __replace__ method:

Python copy.replace on common types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

This is the clean way to "modify" frozen dataclasses and other immutable values. See Dataclasses.

warnings.deprecated

A decorator that marks a function or class as deprecated. Callers get a DeprecationWarning at run time, and type checkers flag the use. It takes a message that should name the replacement:

Python Marking an old function deprecated

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Other additions

  • queue.Queue.shutdown() lets one thread tell consumers that no more items will arrive, so loops over a queue can end cleanly.
  • math.fma(x, y, z) computes x * y + z with a single rounding step.
  • os.process_cpu_count() returns the number of CPUs the current process may use, which can be less than os.cpu_count() in containers.
  • pathlib.Path.from_uri(), glob.translate() and a dbm.sqlite3 backend were also added.
  • random and argparse gained small conveniences, such as python -m random on the command line.

Language and typing changes

Defined behavior for locals()

PEP 667 gives locals() precise rules. Inside a function it now returns a fresh snapshot dictionary on every call, so changing it never affects the real variables, and in a module or class body it returns the live namespace. Earlier versions also behaved this way, but mixed behaviors in debuggers and tracing tools made it hard to rely on.

Python locals() in a function is a snapshot

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Docstrings lose their indentation

The compiler now strips the common leading whitespace from every line of a docstring when it creates the string, so __doc__ is smaller and tools no longer need to clean it:

Python Compiler-stripped docstrings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Type hint updates

  • Type parameter defaults (PEP 696): generic classes and functions can give a default for a type variable, class Box[T = int]: ....
  • typing.ReadOnly marks keys of a TypedDict that code may not change.
  • typing.TypeIs is a type-narrowing return annotation that works in both branches of an if, unlike the older TypeGuard.
  • typing.NoDefault is a sentinel meaning a type parameter has no default.
Python A type parameter with a default

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Type hints don't change how the program runs. A checker such as mypy reads them (see mypy and Static Typing).

Removed modules ("dead batteries")

PEP 594 retired the standard-library modules that were obsolete, unmaintained or tied to old hardware. 3.13 removed 19 of them:

aifc, audioop, cgi, cgitb, chunk, crypt, imghdr, mailcap, msilib, nis, nntplib, ossaudiodev, pipes, sndhdr, spwd, sunau, telnetlib, uu and xdrlib. The lib2to3 package went as well.

Code that imports them will fail with ModuleNotFoundError on 3.13 or newer. Use the replacements: html/urllib.parse and a web framework instead of cgi, subprocess instead of pipes, hashlib or passlib-style libraries instead of crypt, Pillow instead of imghdr and base64 instead of uu.

Python Removed modules fail to import

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Platform and other notes

  • iOS and Android became officially supported (tier 3) platforms, which helps tools that bundle Python into mobile apps.
  • A more detailed set of error messages and colored tracebacks continues the improvements of the previous few releases.
  • Python versions are supported for about five years: bug fixes for two years, then security fixes only. Plan upgrades before your version reaches end of life.

Upgrading safely

  1. Read the "What's New" page and the list of removed modules for the release.
  2. Run your test suite on the new version, with python -W error::DeprecationWarning to turn deprecation warnings into errors.
  3. Update dependencies. Packages with compiled parts must publish wheels for the new version.
  4. Create a fresh virtual environment for the new version rather than modifying the old one.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the free-threaded build of Python 3.13 change?

    Without the GIL, threads can use several cores, but shared data still needs locks. In 3.13 the build was experimental and slower for single-threaded work.
  2. 2. Which function copies an immutable object with one field changed?

    copy.replace() was added in 3.13 and works with dataclasses, named tuples and datetimes.
  3. 3. What happens when you import telnetlib on Python 3.13 or newer?

    PEP 594 removed nineteen obsolete modules, including telnetlib, in 3.13.
  4. 4. In a function, what does locals() return in 3.13?

    Function locals() returns an independent snapshot, so changing it doesn't change the variables.
  5. 5. How do you enable the experimental JIT?

    The 3.13 JIT is a build-time option, disabled by default, and switched on with PYTHON_JIT=1.

Summary

  • Python 3.13 (October 2024) added a new multi-line, colored REPL, with F1, F2 and F3 browsers.
  • The free-threaded build (no GIL) and the experimental JIT are opt-in groundwork for future speed, not everyday features yet. Check sys._is_gil_enabled() to see which mode you are in.
  • New tools: copy.replace(), warnings.deprecated, queue.Queue.shutdown(), math.fma() and typing additions such as type-parameter defaults, ReadOnly and TypeIs.
  • locals() has defined snapshot semantics in functions, and docstrings are stored without their indentation.
  • Nineteen "dead battery" modules were removed, so old imports such as cgi or telnetlib fail.
  • Test upgrades in a fresh virtual environment with deprecation warnings enabled.

Next: Networking, where Python programs talk to other machines.