GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||
GeezerVison is showing docs.python.org — 25.4. 2to3 - Automated Python 2 to 3 code translation — Python 2.7.18 documentation
This document is for an old version of Python that is no longer supported.
You should upgrade and read the
Python documentation for the current stable release.
25.4. 2to3 - Automated Python 2 to 3 code translation¶2to3 is a Python program that reads Python 2.x source code and applies a series
of fixers to transform it into valid Python 3.x code. The standard library
contains a rich set of fixers that will handle almost all code. 2to3 supporting
library 25.4.1. Using 2to3¶2to3 will usually be installed with the Python interpreter as a script. It is
also located in the 2to3’s basic arguments are a list of files or directories to transform. The directories are recursively traversed for Python sources. Here is a sample Python 2.x source file, def greet(name): print "Hello, {0}!".format(name) print "What's your name?" name = raw_input() greet(name) It can be converted to Python 3.x code via 2to3 on the command line: $ 2to3 example.py
A diff against the original source file is printed. 2to3 can also write the
needed modifications right back to the source file. (A backup of the original
file is made unless $ 2to3 -w example.py
After transformation, def greet(name): print("Hello, {0}!".format(name)) print("What's your name?") name = input() greet(name) Comments and exact indentation are preserved throughout the translation process. By default, 2to3 runs a set of predefined fixers. The
$ 2to3 -f imports -f has_key example.py
This command runs every fixer except the $ 2to3 -x apply example.py
Some fixers are explicit, meaning they aren’t run by default and must be
listed on the command line to be run. Here, in addition to the default fixers,
the $ 2to3 -f all -f idioms example.py
Notice how passing Sometimes 2to3 will find a place in your source code that needs to be changed, but 2to3 cannot fix automatically. In this case, 2to3 will print a warning beneath the diff for a file. You should address the warning in order to have compliant 3.x code. 2to3 can also refactor doctests. To enable this mode, use the The Since some print statements can be parsed as function calls or statements, 2to3
cannot always read files containing the print function. When 2to3 detects the
presence of the The New in version 2.7.3: The The New in version 2.7.3: The The $ 2to3 -n -W --add-suffix=3 example.py Will cause a converted file named New in version 2.7.3: The To translate an entire project from one directory tree to another use: $ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode 25.4.2. Fixers¶Each step of transforming code is encapsulated in a fixer. The command
© Copyright 1990-2020, Python Software Foundation.
The Python Software Foundation is a non-profit corporation. Please donate. Last updated on Jun 19, 2020. Found a bug? Created using Sphinx 2.3.1. | ||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |