LinuxCommandLibrary

2to3

Convert Python 2 code to Python 3

TLDR

Display the changes that would be performed without performing them (dry-run)

$ 2to3 [path/to/file.py]
copy

Convert a Python 2 file to Python 3
$ 2to3 [[-w|--write]] [path/to/file.py]
copy

Convert specific Python 2 language features to Python 3
$ 2to3 [[-w|--write]] [path/to/file.py] [[-f|--fix]] [raw_input] [[-f|--fix]] [print]
copy

Convert all Python 2 language features except the specified ones to Python 3
$ 2to3 [[-w|--write]] [path/to/file.py] [[-x|--nofix]] [has_key] [[-x|--nofix]] [isinstance]
copy

List all available language features that can be converted from Python 2 to Python 3
$ 2to3 [[-l|--list-fixes]]
copy

Convert all Python 2 files in a directory to Python 3
$ 2to3 [[-o|--output-dir]] [path/to/python3_directory] [[-W|--write-unchanged-files]] [[-n|--nobackups]] [path/to/python2_directory]
copy

Run 2to3 with multiple threads
$ 2to3 [[-j|--processes]] [4] [[-o|--output-dir]] [path/to/python3_directory] [[-w|--write]] [[-n|--nobackups]] --no-diffs [path/to/python2_directory]
copy

SYNOPSIS

2to3 [-h] [--add-suffix ADDSUFFIX] [-a] [--always-alter] [-b] [-d] [-f FIX] [--fix=FIX] [-i] [-l] [-n] [-o DIR] [-p NOFIX] [-r] [-v] [-x NOFIX] [file| dir ...]

PARAMETERS

-h, --help
    Show help message and exit

--add-suffix=ADDSUFFIX
    Append suffix to output filenames (implies -n)

-a, --all
    Apply all fixers including experimental

--always-alter
    Alter files even without changes

-b, --nobackups
    Do not create backup files

-d, --doctests_only
    Transform only doctests

-f FIX, --fix=FIX
    Apply specific fixers (all, ALL, NONE; repeatable)

-i, --inplace
    Edit files in place (implies backups)

-l, --list_fixers
    List all available fixers

-n, --dry-run
    Preview changes without writing

-o DIR, --output-dir=DIR
    Write output to specified directory

-p NOFIX, --print-function=NOFIX
    Control print function usage

-r, --recursive
    Recursively process directories

-v, --verbose
    Enable verbose output

-x NOFIX, --nofix=NOFIX
    Skip specific fixers (repeatable)

DESCRIPTION

2to3 is a Python utility for automatically converting Python 2.x source code to Python 3.x compatibility. It leverages the lib2to3 library, which contains a comprehensive set of fixers—small transformation rules addressing syntactic and semantic differences between Python 2 and 3, such as converting print statements to print() functions, xrange() to range(), iteritems() to items(), handling unicode literals, long integers, and except clauses.

The tool scans files or directories, applies selected fixers, and outputs transformed code. It supports dry runs for previewing changes, in-place edits with backups, recursive processing, verbose logging, and selective fixer inclusion or exclusion. While powerful for bulk conversions, 2to3 focuses on syntax; it cannot resolve all issues like library incompatibilities, performance changes, or idiomatic Python 3 patterns. Users should always review and test output manually.

Common in Python installations on Linux, 2to3 aids legacy code migration, though modern projects prefer tools like modernize or pyupgrade. It processes .py files and doctests, making it invaluable for large codebases during Python 3 transitions.

CAVEATS

Not all Python 2 code converts perfectly; manual review required for semantics, libraries, and tests. Fixers may introduce new issues or miss subtle changes. Deprecated in recent Python; prefer pyupgrade for Python 3+ code.

BASIC USAGE

Preview: 2to3 -n file.py
Convert in place: 2to3 -i *.py
Recursive: 2to3 -r src/

LIST FIXERS

Run 2to3 -l to see all available fixers like print, xrange, has_key.

HISTORY

Introduced in Python 2.6 (2008) for Python 3 migration. Evolved with lib2to3 refactorings; maintained in Python 3.x for legacy use but largely superseded by community tools post-Python 2 EOL (2020).

SEE ALSO

python(1), lib2to3(1)

Copied to clipboard