Home

2to3

2to3 is a Python program that automates the conversion of Python 2 source code to Python 3. It is part of the Python standard library, distributed with the lib2to3 package, and has been available since the mid-2000s. The tool analyzes Python 2 code and applies a collection of transformation rules to produce Python 3 compatible output. It can write changes in place with the -w option, or print the results to stdout; other options adjust which fixers are applied or control the level of reporting.

The conversion works through a set of fixers located in lib2to3.fixes. Each fixer handles a specific class

Limitations: 2to3 cannot automatically fix all Python 2 code paths or all third-party library dependencies; some

of
incompatibilities
between
Python
2
and
Python
3,
such
as
converting
print
statements
to
calls
to
the
print()
function,
updating
exception
syntax
(from
"except
Exception,
e"
to
"except
Exception
as
e"),
and
adjusting
certain
standard
library
imports
to
their
Python
3
equivalents.
The
tool
also
provides
fixes
for
many
common
Python
2-3
migration
scenarios,
including
division
semantics,
iterators
and
items
methods,
and
string/byte
handling
where
relevant.
code
relies
on
behavior
that
differs
between
versions
or
on
unported
modules
with
no
Python
3
equivalent.
In
such
cases,
manual
review
and
additional
refactoring
are
often
necessary.
While
useful
for
producing
a
starting
point
for
porting,
2to3
is
typically
combined
with
testing
and
manual
changes
as
part
of
a
broader
porting
strategy.