Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 1 | .. _2to3-reference: |
| 2 | |
| 3 | 2to3 - Automated Python 2 to 3 code translation |
| 4 | =============================================== |
| 5 | |
| 6 | .. sectionauthor:: Benjamin Peterson |
| 7 | |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame^] | 8 | 2to3 is a Python program that reads Python 2.x source code and applies a series |
| 9 | of *fixers* to transform it into valid Python 3.x code. The standard library |
| 10 | contains a rich set of fixers that will handle almost all code. It is, however, |
| 11 | possible to write your own fixers. |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 12 | |
| 13 | |
| 14 | Using 2to3 |
| 15 | ---------- |
| 16 | |
| 17 | 2to3 can be run with a list of files to transform or a directory to recursively |
| 18 | traverse looking for files with the ``.py`` extension. |
| 19 | |
| 20 | Here is a sample Python 2.x source file, :file:`example.py`:: |
| 21 | |
| 22 | def greet(name): |
Georg Brandl | 340739e | 2008-07-24 07:09:21 +0000 | [diff] [blame] | 23 | print "Hello, {0}!".format(name) |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 24 | print "What's your name?" |
| 25 | name = raw_input() |
| 26 | greet(name) |
| 27 | |
| 28 | It can be converted to Python 3.x code via 2to3 on the command line:: |
| 29 | |
| 30 | $ 2to3 example.py |
| 31 | |
| 32 | A diff against the original source file will be printed. 2to3 can also write |
| 33 | the needed modifications right back to the source file. (A backup of the |
| 34 | original file will also be made.) This is done with the :option:`-w` flag:: |
| 35 | |
| 36 | $ 2to3 -w example.py |
| 37 | |
| 38 | :file:`example.py` will now look like this:: |
| 39 | |
| 40 | def greet(name): |
Benjamin Peterson | 3ac2f24 | 2008-07-25 21:59:53 +0000 | [diff] [blame] | 41 | print("Hello, {0}!".format(name)) |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 42 | print("What's your name?") |
| 43 | name = input() |
| 44 | greet(name) |
| 45 | |
| 46 | Comments and and exact indentation will be preserved throughout the translation |
| 47 | process. |
| 48 | |
| 49 | By default, 2to3 will run a set of predefined fixers. The :option:`-l` flag |
| 50 | lists all avaible fixers. An explicit set of fixers to run can be given by use |
| 51 | of the :option:`-f` flag. The following example runs only the ``imports`` and |
| 52 | ``has_key`` fixers:: |
| 53 | |
| 54 | $ 2to3 -f imports -f has_key example.py |
| 55 | |
| 56 | Some fixers are *explicit*, meaning they aren't run be default and must be |
| 57 | listed on the command line. Here, in addition to the default fixers, the |
| 58 | ``idioms`` fixer is run:: |
| 59 | |
| 60 | $ 2to3 -f all -f idioms example.py |
| 61 | |
| 62 | Notice how ``all`` enables all default fixers. |
| 63 | |
| 64 | Sometimes 2to3 will find will find a place in your source code that needs to be |
| 65 | changed, but 2to3 cannot fix automatically. In this case, 2to3 will print a |
| 66 | warning beneath the diff for a file. |
| 67 | |
| 68 | |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 69 | :mod:`lib2to3` - 2to3's library |
| 70 | ------------------------------- |
| 71 | |
| 72 | .. module:: lib2to3 |
| 73 | :synopsis: the 2to3 library |
| 74 | .. moduleauthor:: Guido van Rossum |
| 75 | .. moduleauthor:: Collin Winter |
| 76 | |
| 77 | .. XXX What is the public interface anyway? |