2to3
automated Python 2 to Python 3 code converter
TLDR
SYNOPSIS
2to3 [options] file|directory
DESCRIPTION
2to3 is an automated Python 2 to Python 3 code translator. It parses Python 2 source code and applies "fixers" that transform incompatible syntax and library calls to their Python 3 equivalents.Common transformations include:- print statement to print() function- unicode to str, str to bytes- raw_input() to input()- Iterator methods (.iteritems() to .items())- except Exception, e to except Exception as e- Library renames (urllib, ConfigParser, etc.)2to3 handles most mechanical changes but may require manual review for semantic differences between Python 2 and 3.
PARAMETERS
-w, --write
Write changes to files-n, --nobackups
Don't create .bak backup files-f fixer, --fix fixer
Apply specific fixer-x fixer, --nofix fixer
Exclude specific fixer-l, --list-fixes
List available fixers-p, --print-function
Treat print as a function (skip the print fixer). Useful when the file already uses from __future__ import print_function.-d, --doctests_only
Only fix doctests inside the file.-W, --write-unchanged-files
Write output even for unchanged files (useful with -o).--add-suffix SUFFIX
Append this suffix to output filenames instead of overwriting.-j n, --processes n
Run in parallel with n processes-o dir, --output-dir dir
Write converted files to directory-v, --verbose
More verbose output
CAVEATS
2to3 makes syntactic changes but cannot handle all differences. Code behavior may still differ, especially around:- String/bytes handling- Integer division- Dictionary ordering (pre-Python 3.7)- Unicode text processingRunning -w without -n creates .bak files that may clutter version control.Python 2 reached end-of-life in January 2020. For major projects, consider modern porting tools like python-modernize or futurize.
