blob: 24c563cf3c670a29072f41c64874813655d6b6ef [file] [log] [blame]
Benjamin Peterson40202212008-07-24 02:45:37 +00001.. _2to3-reference:
2
32to3 - Automated Python 2 to 3 code translation
4===============================================
5
Georg Brandl686d53e2009-01-14 00:08:09 +00006.. sectionauthor:: Benjamin Peterson <benjamin@python.org>
Benjamin Peterson40202212008-07-24 02:45:37 +00007
Benjamin Petersoneb55fd82008-09-03 00:21:32 +000082to3 is a Python program that reads Python 2.x source code and applies a series
9of *fixers* to transform it into valid Python 3.x code. The standard library
Benjamin Peterson15ad6c02008-09-04 23:31:27 +000010contains a rich set of fixers that will handle almost all code. 2to3 supporting
11library :mod:`lib2to3` is, however, a flexible and generic library, so it is
12possible to write your own fixers for 2to3. :mod:`lib2to3` could also be
13adapted to custom applications in which Python code needs to be edited
14automatically.
Benjamin Peterson40202212008-07-24 02:45:37 +000015
16
17Using 2to3
18----------
19
Benjamin Peterson15ad6c02008-09-04 23:31:27 +0000202to3 will usually be installed with the Python interpreter as a script. It is
21also located in the :file:`Tools/scripts` directory of the Python root.
22
232to3's basic arguments are a list of files or directories to transform. The
24directories are to recursively traversed for Python sources.
Benjamin Peterson40202212008-07-24 02:45:37 +000025
26Here is a sample Python 2.x source file, :file:`example.py`::
27
28 def greet(name):
Georg Brandl340739e2008-07-24 07:09:21 +000029 print "Hello, {0}!".format(name)
Benjamin Peterson40202212008-07-24 02:45:37 +000030 print "What's your name?"
31 name = raw_input()
32 greet(name)
33
34It can be converted to Python 3.x code via 2to3 on the command line::
35
36 $ 2to3 example.py
37
Benjamin Peterson15ad6c02008-09-04 23:31:27 +000038A diff against the original source file is printed. 2to3 can also write the
Georg Brandl40e15ed2009-04-05 21:48:06 +000039needed modifications right back to the source file. (A backup of the original
40file is made unless :option:`-n` is also given.) Writing the changes back is
41enabled with the :option:`-w` flag::
Benjamin Peterson40202212008-07-24 02:45:37 +000042
43 $ 2to3 -w example.py
44
Benjamin Petersonad2a9e72008-09-06 03:00:00 +000045After transformation, :file:`example.py` looks like this::
Benjamin Peterson40202212008-07-24 02:45:37 +000046
47 def greet(name):
Benjamin Peterson3ac2f242008-07-25 21:59:53 +000048 print("Hello, {0}!".format(name))
Benjamin Peterson40202212008-07-24 02:45:37 +000049 print("What's your name?")
50 name = input()
51 greet(name)
52
Georg Brandl4aef7032008-11-07 08:56:27 +000053Comments and exact indentation are preserved throughout the translation process.
Benjamin Peterson40202212008-07-24 02:45:37 +000054
Fred Drake23681da2008-10-24 02:28:15 +000055By default, 2to3 runs a set of predefined fixers. The :option:`-l` flag lists
Georg Brandl4aef7032008-11-07 08:56:27 +000056all available fixers. An explicit set of fixers to run can be given with
57:option:`-f`. Likewise the :option:`-x` explicitly disables a fixer. The
58following example runs only the ``imports`` and ``has_key`` fixers::
Benjamin Peterson40202212008-07-24 02:45:37 +000059
60 $ 2to3 -f imports -f has_key example.py
61
Georg Brandl4aef7032008-11-07 08:56:27 +000062This command runs every fixer except the ``apply`` fixer::
63
64 $ 2to3 -x apply example.py
65
Fred Drake23681da2008-10-24 02:28:15 +000066Some fixers are *explicit*, meaning they aren't run by default and must be
Benjamin Peterson15ad6c02008-09-04 23:31:27 +000067listed on the command line to be run. Here, in addition to the default fixers,
68the ``idioms`` fixer is run::
Benjamin Peterson40202212008-07-24 02:45:37 +000069
70 $ 2to3 -f all -f idioms example.py
71
Benjamin Peterson15ad6c02008-09-04 23:31:27 +000072Notice how passing ``all`` enables all default fixers.
Benjamin Peterson40202212008-07-24 02:45:37 +000073
Georg Brandl4aef7032008-11-07 08:56:27 +000074Sometimes 2to3 will find a place in your source code that needs to be changed,
75but 2to3 cannot fix automatically. In this case, 2to3 will print a warning
76beneath the diff for a file. You should address the warning in order to have
77compliant 3.x code.
Benjamin Peterson15ad6c02008-09-04 23:31:27 +000078
792to3 can also refactor doctests. To enable this mode, use the :option:`-d`
Benjamin Petersonb51f81d2008-09-28 01:53:29 +000080flag. Note that *only* doctests will be refactored. This also doesn't require
81the module to be valid Python. For example, doctest like examples in a reST
82document could also be refactored with this option.
Benjamin Peterson15ad6c02008-09-04 23:31:27 +000083
Georg Brandl4aef7032008-11-07 08:56:27 +000084The :option:`-v` option enables output of more information on the translation
85process.
Benjamin Peterson15ad6c02008-09-04 23:31:27 +000086
Benjamin Peterson40202212008-07-24 02:45:37 +000087
Benjamin Peterson40202212008-07-24 02:45:37 +000088:mod:`lib2to3` - 2to3's library
89-------------------------------
90
91.. module:: lib2to3
92 :synopsis: the 2to3 library
93.. moduleauthor:: Guido van Rossum
94.. moduleauthor:: Collin Winter
95
Benjamin Peterson7f8f6602008-09-27 16:23:55 +000096
Georg Brandl38853142009-04-28 18:23:28 +000097.. note::
Benjamin Peterson7f8f6602008-09-27 16:23:55 +000098
99 The :mod:`lib2to3` API should be considered unstable and may change
100 drastically in the future.
101
Benjamin Peterson40202212008-07-24 02:45:37 +0000102.. XXX What is the public interface anyway?