all Python 3until 3.13Removed from the standard libraryterm
lib2to3
removed modules
The library behind the 2to3 tool: parsing Python code while keeping formatting, and porting from Python 2. Removed in Python 3.13 — use ast for parsing.
Syntax
import lib2to3 # до 3.13; 2to3 file.py
lib2to3 parsed source into a tree that keeps whitespace and comments and edited it with sets of fixers; it powered the 2to3 program, which ported code from Python 2 to Python 3 (print 'hi' → print('hi')), as well as black and other tools. It was removed because its LL(1) parser cannot handle new syntax: from Python 3.10 on (match, parenthesised with) it breaks. Deprecated in 3.11, removed in 3.13 together with the 2to3 program. How to port: ast.parse() gives the code tree, ast.unparse() turns it back into text (comments are lost); formatting-preserving edits are done by libcst and parso. Old Python 2 code is ported with 2to3 from Python 3.12 or the fissix package, a fork of lib2to3 on PyPI.
Examples
# Python 3.13+
try:
import lib2to3
except ModuleNotFoundError as e:
print(type(e).__name__) # → ModuleNotFoundError
# Python 3.13+
import ast
tree = ast.parse('x = 1+2 # sum')
print(ast.unparse(tree)) # → x = 1 + 2
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy