2to3-Automatic python 2 to 3 code conversion
2to3 is a python program that reads Python 2.x source code and applies a series of Fix The program converts it to a valid Python 3.x code. The standard library contains a rich set of hotfixes that can handle almost any code. However,2to3 Support Library Lib2to3 is a flexible universal library, so you can write your own hotfixes for 2to3. Lib2to3 can also be used for custom applications that require automatic editing of Python code.
using the 2to3
2to3 is typically installed with the Python interpreter as a script. It is also located in the Tools/scripts directory of the python root directory .
The basic parameter of 2to3 is the list of files or directories to convert. These directories will recursively traverse the Python source code.
The following is an example of a Python 2.x source file example.py :
def (): . format ( name ) ()()
It can be converted to Python 3.x code by 2TO3 on the command line:
$2to3 example.py
Prints the difference to the original source file. 2to3 can also write the required modifications back to the source file. (of course, unless you also give the - n, otherwise the original text??) Backup of the Components . Use the-W flag to enable subsequent changes :
$2to3-w example.py
after conversion, example.py looks like this:
def (): (. () ) () () () ()
Keep comments and exact indentation throughout the translation process.
by default, 2to3 runs a set of predefined hotfixes. the - l flag lists all available fixings. You can use - F to give a clear set of running hotfixes . Similarly,-x explicitly disables the hotfix. The following example runs only the imports and has_key hotfixes:
$2to3-f Import-F Has_key example.py
This command runs except Application Hotfixes for each of the hotfixes outside of the hotfix :
$2to3-x Application example.py
some hotfixes are an explicit , which means that they are not run by default and must be listed on the command line to run. here, in addition to the default fix, the idiom fix is also running:
$2to3-f all-f Idioms example.py
Notice how you can pass All to Enable all default hotfixes.
sometimes 2to3 will find a location in the source code that needs to be changed, but 2to3 cannot fix it automatically. In this case, 2to3 will print a warning under the differences in the file. You should resolve the warning to obtain a compatible 3.x code.
2to3 can also reconstruct doctests. To enable this mode, use the- d flag. Please note that only doctests will be refactored. This also does not require that the module be valid for Python. For example, the doctest example in a rest document can also be refactored using this option.
the - v options can be output for detailed information about the translation process.
when- Pwhen passed, 2to3 willPrintis treated as a function instead of a statement. from __future__ import print_function when used , This is useful . If this option is not given, the print fix encloses the print call in an extra set of parentheses, because it cannot differentiate between the enclosed print statements (for example , Print ("a" + "B" + "C") ) and a real function call.
Lib2to3-2to3 's library
Warning
the Lib2to3 The API should be considered unstable and may change dramatically in the future.
python2x How to migrate code into Python3