2to3

Automated Python 2 to 3 code translation.

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 --write [path/to/file.py]
copy

Convert specific Python 2 language features to Python 3

$ 2to3 --write [path/to/file.py] --fix=[raw_input] --fix=[print]
copy

Convert all Python 2 language features except the specified ones to Python 3

$ 2to3 --write [path/to/file.py] --nofix=[has_key] --nofix=[isinstance]
copy

Display a list of all available language features that can be converted from Python 2 to Python 3

$ 2to3 --list-fixes
copy

Convert all Python 2 files in a directory to Python 3

$ 2to3 --output-dir=[path/to/python3_directory] --write-unchanged-files --nobackups [path/to/python2_directory]
copy

Run 2to3 with multiple threads

$ 2to3 --processes=[4] --output-dir=[path/to/python3_directory] --write --nobackups --no-diff [path/to/python2_directory]
copy

Help

Usage: 2to3 [options] file|dir ... 

Options:
  -h, --help            show this help message and exit 
  -d, --doctests_only   Fix up doctests only 
  -f FIX, --fix=FIX     Each FIX specifies a transformation; default: all 
  -j PROCESSES, --processes=PROCESSES 
                        Run 2to3 concurrently 
  -x NOFIX, --nofix=NOFIX 
                        Prevent a transformation from being run 
  -l, --list-fixes      List available transformations 
  -p, --print-function  Modify the grammar so that print() is a function 
  -e, --exec-function   Modify the grammar so that exec() is a function 
  -v, --verbose         More verbose logging 
  --no-diffs            Don't show diffs of the refactoring 
  -w, --write           Write back modified files 
  -n, --nobackups       Don't write backups for modified files 
  -o OUTPUT_DIR, --output-dir=OUTPUT_DIR 
                        Put output files in this directory instead of 
                        overwriting the input files.  Requires -n. 
  -W, --write-unchanged-files 
                        Also write files even if no changes were required 
                        (useful with --output-dir); implies -w. 
  --add-suffix=ADD_SUFFIX 
                        Append this string to all output filenames. Requires 
                        -n if non-empty.  ex: --add-suffix='3' will generate 
                        .py3 files. 

Copied to clipboard
Dodo Farming