/
usr
/
share
/
doc
/
python2-docs
/
html
/
library
/
/usr/share/doc/python2-docs/html/library
mkdir
upload
Name
Size
Mode
Actions
2to3.html
59507
0644
edit
dl
rm
abc.html
25826
0644
edit
dl
rm
aepack.html
14164
0644
edit
dl
rm
aetools.html
16389
0644
edit
dl
rm
aetypes.html
21021
0644
edit
dl
rm
aifc.html
25064
0644
edit
dl
rm
al.html
18672
0644
edit
dl
rm
allos.html
35297
0644
edit
dl
rm
anydbm.html
18124
0644
edit
dl
rm
archiving.html
10078
0644
edit
dl
rm
argparse.html
263616
0644
edit
dl
rm
array.html
31855
0644
edit
dl
rm
ast.html
38167
0644
edit
dl
rm
asynchat.html
33930
0644
edit
dl
rm
asyncore.html
40631
0644
edit
dl
rm
atexit.html
18455
0644
edit
dl
rm
audioop.html
34252
0644
edit
dl
rm
autogil.html
8733
0644
edit
dl
rm
base64.html
21862
0644
edit
dl
rm
basehttpserver.html
37441
0644
edit
dl
rm
bastion.html
11798
0644
edit
dl
rm
bdb.html
41395
0644
edit
dl
rm
binascii.html
22997
0644
edit
dl
rm
binhex.html
11309
0644
edit
dl
rm
bisect.html
24666
0644
edit
dl
rm
bsddb.html
28589
0644
edit
dl
rm
bz2.html
29048
0644
edit
dl
rm
calendar.html
41762
0644
edit
dl
rm
carbon.html
51998
0644
edit
dl
rm
cd.html
30033
0644
edit
dl
rm
cgi.html
55922
0644
edit
dl
rm
cgihttpserver.html
14026
0644
edit
dl
rm
cgitb.html
12263
0644
edit
dl
rm
chunk.html
15899
0644
edit
dl
rm
cmath.html
28365
0644
edit
dl
rm
cmd.html
29042
0644
edit
dl
rm
code.html
26959
0644
edit
dl
rm
codecs.html
118258
0644
edit
dl
rm
codeop.html
15898
0644
edit
dl
rm
collections.html
147647
0644
edit
dl
rm
colorpicker.html
8035
0644
edit
dl
rm
colorsys.html
11919
0644
edit
dl
rm
commands.html
15516
0644
edit
dl
rm
compileall.html
18621
0644
edit
dl
rm
compiler.html
75897
0644
edit
dl
rm
configparser.html
67751
0644
edit
dl
rm
constants.html
13979
0644
edit
dl
rm
contextlib.html
22777
0644
edit
dl
rm
cookie.html
41854
0644
edit
dl
rm
cookielib.html
91032
0644
edit
dl
rm
copy.html
13004
0644
edit
dl
rm
copy_reg.html
14691
0644
edit
dl
rm
crypt.html
10647
0644
edit
dl
rm
crypto.html
7761
0644
edit
dl
rm
csv.html
76456
0644
edit
dl
rm
ctypes.html
264575
0644
edit
dl
rm
curses.ascii.html
24873
0644
edit
dl
rm
curses.html
167501
0644
edit
dl
rm
curses.panel.html
15824
0644
edit
dl
rm
custominterp.html
8026
0644
edit
dl
rm
datatypes.html
18015
0644
edit
dl
rm
datetime.html
253438
0644
edit
dl
rm
dbhash.html
16825
0644
edit
dl
rm
dbm.html
13507
0644
edit
dl
rm
debug.html
10703
0644
edit
dl
rm
decimal.html
222315
0644
edit
dl
rm
development.html
14828
0644
edit
dl
rm
difflib.html
91975
0644
edit
dl
rm
dircache.html
12299
0644
edit
dl
rm
dis.html
84156
0644
edit
dl
rm
distribution.html
7679
0644
edit
dl
rm
distutils.html
10152
0644
edit
dl
rm
dl.html
17668
0644
edit
dl
rm
doctest.html
185883
0644
edit
dl
rm
docxmlrpcserver.html
17573
0644
edit
dl
rm
dumbdbm.html
15549
0644
edit
dl
rm
dummy_thread.html
10065
0644
edit
dl
rm
dummy_threading.html
8934
0644
edit
dl
rm
easydialogs.html
33124
0644
edit
dl
rm
email-examples.html
47424
0644
edit
dl
rm
email.charset.html
29424
0644
edit
dl
rm
email.encoders.html
12847
0644
edit
dl
rm
email.errors.html
17952
0644
edit
dl
rm
email.generator.html
23141
0644
edit
dl
rm
email.header.html
29108
0644
edit
dl
rm
email.html
55722
0644
edit
dl
rm
email.iterators.html
12623
0644
edit
dl
rm
email.message.html
70189
0644
edit
dl
rm
email.mime.html
31655
0644
edit
dl
rm
email.parser.html
35142
0644
edit
dl
rm
email.utils.html
27076
0644
edit
dl
rm
ensurepip.html
18327
0644
edit
dl
rm
errno.html
40246
0644
edit
dl
rm
exceptions.html
63693
0644
edit
dl
rm
fcntl.html
26245
0644
edit
dl
rm
filecmp.html
23997
0644
edit
dl
rm
fileformats.html
9653
0644
edit
dl
rm
fileinput.html
27071
0644
edit
dl
rm
filesys.html
10866
0644
edit
dl
rm
fl.html
56525
0644
edit
dl
rm
fm.html
13009
0644
edit
dl
rm
fnmatch.html
16258
0644
edit
dl
rm
formatter.html
37371
0644
edit
dl
rm
fpectl.html
16879
0644
edit
dl
rm
fpformat.html
11455
0644
edit
dl
rm
fractions.html
24888
0644
edit
dl
rm
framework.html
36934
0644
edit
dl
rm
frameworks.html
7551
0644
edit
dl
rm
ftplib.html
49577
0644
edit
dl
rm
functions.html
205640
0644
edit
dl
rm
functools.html
29645
0644
edit
dl
rm
future_builtins.html
14477
0644
edit
dl
rm
gc.html
28423
0644
edit
dl
rm
gdbm.html
17805
0644
edit
dl
rm
gensuitemodule.html
12574
0644
edit
dl
rm
getopt.html
25280
0644
edit
dl
rm
getpass.html
11426
0644
edit
dl
rm
gettext.html
84971
0644
edit
dl
rm
gl.html
24342
0644
edit
dl
rm
glob.html
14430
0644
edit
dl
rm
grp.html
11316
0644
edit
dl
rm
gzip.html
20576
0644
edit
dl
rm
hashlib.html
25467
0644
edit
dl
rm
heapq.html
34890
0644
edit
dl
rm
hmac.html
14374
0644
edit
dl
rm
hotshot.html
20147
0644
edit
dl
rm
htmllib.html
27682
0644
edit
dl
rm
htmlparser.html
42433
0644
edit
dl
rm
httplib.html
70932
0644
edit
dl
rm
i18n.html
10047
0644
edit
dl
rm
ic.html
18654
0644
edit
dl
rm
idle.html
42148
0644
edit
dl
rm
imageop.html
16099
0644
edit
dl
rm
imaplib.html
58515
0644
edit
dl
rm
imgfile.html
12732
0644
edit
dl
rm
imghdr.html
12238
0644
edit
dl
rm
imp.html
37603
0644
edit
dl
rm
importlib.html
8926
0644
edit
dl
rm
imputil.html
33563
0644
edit
dl
rm
index.html
79088
0644
edit
dl
rm
inspect.html
56823
0644
edit
dl
rm
internet.html
26138
0644
edit
dl
rm
intro.html
9353
0644
edit
dl
rm
io.html
113701
0644
edit
dl
rm
ipc.html
16598
0644
edit
dl
rm
itertools.html
125397
0644
edit
dl
rm
jpeg.html
13757
0644
edit
dl
rm
json.html
73678
0644
edit
dl
rm
keyword.html
8210
0644
edit
dl
rm
language.html
11687
0644
edit
dl
rm
linecache.html
11416
0644
edit
dl
rm
locale.html
61576
0644
edit
dl
rm
logging.config.html
80045
0644
edit
dl
rm
logging.handlers.html
80194
0644
edit
dl
rm
logging.html
110254
0644
edit
dl
rm
mac.html
23376
0644
edit
dl
rm
macos.html
16129
0644
edit
dl
rm
macosa.html
14080
0644
edit
dl
rm
macostools.html
16898
0644
edit
dl
rm
macpath.html
8386
0644
edit
dl
rm
mailbox.html
171121
0644
edit
dl
rm
mailcap.html
14132
0644
edit
dl
rm
markup.html
19862
0644
edit
dl
rm
marshal.html
19456
0644
edit
dl
rm
math.html
44015
0644
edit
dl
rm
md5.html
15123
0644
edit
dl
rm
mhlib.html
24123
0644
edit
dl
rm
mimetools.html
21206
0644
edit
dl
rm
mimetypes.html
30634
0644
edit
dl
rm
mimewriter.html
16078
0644
edit
dl
rm
mimify.html
15279
0644
edit
dl
rm
miniaeframe.html
13109
0644
edit
dl
rm
misc.html
7236
0644
edit
dl
rm
mm.html
9570
0644
edit
dl
rm
mmap.html
30832
0644
edit
dl
rm
modulefinder.html
18096
0644
edit
dl
rm
modules.html
9017
0644
edit
dl
rm
msilib.html
57853
0644
edit
dl
rm
msvcrt.html
21232
0644
edit
dl
rm
multifile.html
26256
0644
edit
dl
rm
multiprocessing.html
414535
0644
edit
dl
rm
mutex.html
12147
0644
edit
dl
rm
netdata.html
18379
0644
edit
dl
rm
netrc.html
14021
0644
edit
dl
rm
new.html
13177
0644
edit
dl
rm
nis.html
11502
0644
edit
dl
rm
nntplib.html
45689
0644
edit
dl
rm
numbers.html
40636
0644
edit
dl
rm
numeric.html
14292
0644
edit
dl
rm
operator.html
93622
0644
edit
dl
rm
optparse.html
250109
0644
edit
dl
rm
os.html
240195
0644
edit
dl
rm
os.path.html
43402
0644
edit
dl
rm
ossaudiodev.html
45594
0644
edit
dl
rm
othergui.html
9403
0644
edit
dl
rm
parser.html
42558
0644
edit
dl
rm
pdb.html
38603
0644
edit
dl
rm
persistence.html
15676
0644
edit
dl
rm
pickle.html
109684
0644
edit
dl
rm
pickletools.html
11475
0644
edit
dl
rm
pipes.html
19685
0644
edit
dl
rm
pkgutil.html
27367
0644
edit
dl
rm
platform.html
31599
0644
edit
dl
rm
plistlib.html
18344
0644
edit
dl
rm
popen2.html
27601
0644
edit
dl
rm
poplib.html
24320
0644
edit
dl
rm
posix.html
16626
0644
edit
dl
rm
posixfile.html
21387
0644
edit
dl
rm
pprint.html
32207
0644
edit
dl
rm
profile.html
72788
0644
edit
dl
rm
pty.html
10195
0644
edit
dl
rm
pwd.html
12388
0644
edit
dl
rm
pyclbr.html
15812
0644
edit
dl
rm
pydoc.html
13650
0644
edit
dl
rm
pyexpat.html
80886
0644
edit
dl
rm
python.html
12854
0644
edit
dl
rm
py_compile.html
11932
0644
edit
dl
rm
queue.html
26856
0644
edit
dl
rm
quopri.html
12739
0644
edit
dl
rm
random.html
42722
0644
edit
dl
rm
re.html
155985
0644
edit
dl
rm
readline.html
37383
0644
edit
dl
rm
repr.html
21778
0644
edit
dl
rm
resource.html
28286
0644
edit
dl
rm
restricted.html
12366
0644
edit
dl
rm
rexec.html
40566
0644
edit
dl
rm
rfc822.html
46408
0644
edit
dl
rm
rlcompleter.html
14460
0644
edit
dl
rm
robotparser.html
13403
0644
edit
dl
rm
runpy.html
21925
0644
edit
dl
rm
sched.html
19878
0644
edit
dl
rm
scrolledtext.html
9728
0644
edit
dl
rm
select.html
44090
0644
edit
dl
rm
sets.html
40151
0644
edit
dl
rm
sgi.html
10371
0644
edit
dl
rm
sgmllib.html
34464
0644
edit
dl
rm
sha.html
13062
0644
edit
dl
rm
shelve.html
29955
0644
edit
dl
rm
shlex.html
35033
0644
edit
dl
rm
shutil.html
45454
0644
edit
dl
rm
signal.html
34023
0644
edit
dl
rm
simplehttpserver.html
20351
0644
edit
dl
rm
simplexmlrpcserver.html
37794
0644
edit
dl
rm
site.html
26887
0644
edit
dl
rm
smtpd.html
13587
0644
edit
dl
rm
smtplib.html
47054
0644
edit
dl
rm
sndhdr.html
10993
0644
edit
dl
rm
socket.html
116414
0644
edit
dl
rm
socketserver.html
76430
0644
edit
dl
rm
someos.html
16472
0644
edit
dl
rm
spwd.html
11163
0644
edit
dl
rm
sqlite3.html
150014
0644
edit
dl
rm
ssl.html
202453
0644
edit
dl
rm
stat.html
34597
0644
edit
dl
rm
statvfs.html
11326
0644
edit
dl
rm
stdtypes.html
298144
0644
edit
dl
rm
string.html
120219
0644
edit
dl
rm
stringio.html
20061
0644
edit
dl
rm
stringprep.html
17729
0644
edit
dl
rm
strings.html
15905
0644
edit
dl
rm
struct.html
44896
0644
edit
dl
rm
subprocess.html
110447
0644
edit
dl
rm
sun.html
7253
0644
edit
dl
rm
sunau.html
30011
0644
edit
dl
rm
sunaudio.html
19236
0644
edit
dl
rm
symbol.html
8140
0644
edit
dl
rm
symtable.html
25871
0644
edit
dl
rm
sys.html
110996
0644
edit
dl
rm
sysconfig.html
26299
0644
edit
dl
rm
syslog.html
19737
0644
edit
dl
rm
tabnanny.html
11393
0644
edit
dl
rm
tarfile.html
88728
0644
edit
dl
rm
telnetlib.html
27782
0644
edit
dl
rm
tempfile.html
31903
0644
edit
dl
rm
termios.html
17309
0644
edit
dl
rm
test.html
57030
0644
edit
dl
rm
textwrap.html
30135
0644
edit
dl
rm
thread.html
21486
0644
edit
dl
rm
threading.html
86655
0644
edit
dl
rm
time.html
63798
0644
edit
dl
rm
timeit.html
40529
0644
edit
dl
rm
tix.html
50582
0644
edit
dl
rm
tk.html
26568
0644
edit
dl
rm
tkinter.html
84316
0644
edit
dl
rm
token.html
20999
0644
edit
dl
rm
tokenize.html
20607
0644
edit
dl
rm
trace.html
28668
0644
edit
dl
rm
traceback.html
40900
0644
edit
dl
rm
ttk.html
108515
0644
edit
dl
rm
tty.html
9753
0644
edit
dl
rm
turtle.html
230483
0644
edit
dl
rm
types.html
29833
0644
edit
dl
rm
undoc.html
24680
0644
edit
dl
rm
unicodedata.html
20186
0644
edit
dl
rm
unittest.html
225021
0644
edit
dl
rm
unix.html
11223
0644
edit
dl
rm
urllib.html
68266
0644
edit
dl
rm
urllib2.html
113380
0644
edit
dl
rm
urlparse.html
43474
0644
edit
dl
rm
user.html
12707
0644
edit
dl
rm
userdict.html
32069
0644
edit
dl
rm
uu.html
11810
0644
edit
dl
rm
uuid.html
30137
0644
edit
dl
rm
warnings.html
50950
0644
edit
dl
rm
wave.html
24894
0644
edit
dl
rm
weakref.html
38808
0644
edit
dl
rm
webbrowser.html
26565
0644
edit
dl
rm
whichdb.html
9499
0644
edit
dl
rm
windows.html
9817
0644
edit
dl
rm
winsound.html
20466
0644
edit
dl
rm
wsgiref.html
88384
0644
edit
dl
rm
xdrlib.html
33018
0644
edit
dl
rm
xml.dom.html
97877
0644
edit
dl
rm
xml.dom.minidom.html
43230
0644
edit
dl
rm
xml.dom.pulldom.html
13835
0644
edit
dl
rm
xml.etree.elementtree.html
111804
0644
edit
dl
rm
xml.html
18007
0644
edit
dl
rm
xml.sax.handler.html
41877
0644
edit
dl
rm
xml.sax.html
23458
0644
edit
dl
rm
xml.sax.reader.html
44802
0644
edit
dl
rm
xml.sax.utils.html
16087
0644
edit
dl
rm
xmlrpclib.html
69191
0644
edit
dl
rm
zipfile.html
62227
0644
edit
dl
rm
zipimport.html
22769
0644
edit
dl
rm
zlib.html
31230
0644
edit
dl
rm
_winreg.html
64631
0644
edit
dl
rm
__builtin__.html
11069
0644
edit
dl
rm
__future__.html
14688
0644
edit
dl
rm
__main__.html
7512
0644
edit
dl
rm
Edit:
/usr/share/doc/python2-docs/html/library/2to3.html
(59507B)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>25.4. 2to3 - Automated Python 2 to 3 code translation — Python 2.7.16 documentation</title> <link rel="stylesheet" href="../_static/classic.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.16 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="next" title="25.5. test — Regression tests package for Python" href="test.html" /> <link rel="prev" title="25.3. unittest — Unit testing framework" href="unittest.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <link rel="canonical" href="https://docs.python.org/2/library/2to3.html" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head><body> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="test.html" title="25.5. test — Regression tests package for Python" accesskey="N">next</a> |</li> <li class="right" > <a href="unittest.html" title="25.3. unittest — Unit testing framework" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.16 documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-2"><a href="development.html" accesskey="U">25. Development Tools</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <div class="section" id="to3-automated-python-2-to-3-code-translation"> <span id="to3-reference"></span><h1>25.4. 2to3 - Automated Python 2 to 3 code translation<a class="headerlink" href="#to3-automated-python-2-to-3-code-translation" title="Permalink to this headline">¶</a></h1> <p>2to3 is a Python program that reads Python 2.x source code and applies a series of <em>fixers</em> 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 <a class="reference internal" href="#module-lib2to3" title="lib2to3: the 2to3 library"><code class="xref py py-mod docutils literal notranslate"><span class="pre">lib2to3</span></code></a> is, however, a flexible and generic library, so it is possible to write your own fixers for 2to3. <a class="reference internal" href="#module-lib2to3" title="lib2to3: the 2to3 library"><code class="xref py py-mod docutils literal notranslate"><span class="pre">lib2to3</span></code></a> could also be adapted to custom applications in which Python code needs to be edited automatically.</p> <div class="section" id="using-2to3"> <span id="to3-using"></span><h2>25.4.1. Using 2to3<a class="headerlink" href="#using-2to3" title="Permalink to this headline">¶</a></h2> <p>2to3 will usually be installed with the Python interpreter as a script. It is also located in the <code class="file docutils literal notranslate"><span class="pre">Tools/scripts</span></code> directory of the Python root.</p> <p>2to3’s basic arguments are a list of files or directories to transform. The directories are recursively traversed for Python sources.</p> <p>Here is a sample Python 2.x source file, <code class="file docutils literal notranslate"><span class="pre">example.py</span></code>:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">greet</span><span class="p">(</span><span class="n">name</span><span class="p">):</span> <span class="nb">print</span> <span class="s2">"Hello, </span><span class="si">{0}</span><span class="s2">!"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="nb">print</span> <span class="s2">"What's your name?"</span> <span class="n">name</span> <span class="o">=</span> <span class="n">raw_input</span><span class="p">()</span> <span class="n">greet</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> </pre></div> </div> <p>It can be converted to Python 3.x code via 2to3 on the command line:</p> <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> 2to3 example.py </pre></div> </div> <p>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 <code class="xref std std-option docutils literal notranslate"><span class="pre">-n</span></code> is also given.) Writing the changes back is enabled with the <code class="xref std std-option docutils literal notranslate"><span class="pre">-w</span></code> flag:</p> <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> 2to3 -w example.py </pre></div> </div> <p>After transformation, <code class="file docutils literal notranslate"><span class="pre">example.py</span></code> looks like this:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">greet</span><span class="p">(</span><span class="n">name</span><span class="p">):</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"Hello, </span><span class="si">{0}</span><span class="s2">!"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="p">))</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"What's your name?"</span><span class="p">)</span> <span class="n">name</span> <span class="o">=</span> <span class="nb">input</span><span class="p">()</span> <span class="n">greet</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> </pre></div> </div> <p>Comments and exact indentation are preserved throughout the translation process.</p> <p>By default, 2to3 runs a set of <a class="reference internal" href="#to3-fixers"><span class="std std-ref">predefined fixers</span></a>. The <code class="xref std std-option docutils literal notranslate"><span class="pre">-l</span></code> flag lists all available fixers. An explicit set of fixers to run can be given with <code class="xref std std-option docutils literal notranslate"><span class="pre">-f</span></code>. Likewise the <code class="xref std std-option docutils literal notranslate"><span class="pre">-x</span></code> explicitly disables a fixer. The following example runs only the <code class="docutils literal notranslate"><span class="pre">imports</span></code> and <code class="docutils literal notranslate"><span class="pre">has_key</span></code> fixers:</p> <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> 2to3 -f imports -f has_key example.py </pre></div> </div> <p>This command runs every fixer except the <code class="docutils literal notranslate"><span class="pre">apply</span></code> fixer:</p> <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> 2to3 -x apply example.py </pre></div> </div> <p>Some fixers are <em>explicit</em>, 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 <code class="docutils literal notranslate"><span class="pre">idioms</span></code> fixer is run:</p> <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> 2to3 -f all -f idioms example.py </pre></div> </div> <p>Notice how passing <code class="docutils literal notranslate"><span class="pre">all</span></code> enables all default fixers.</p> <p>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.</p> <p>2to3 can also refactor doctests. To enable this mode, use the <code class="xref std std-option docutils literal notranslate"><span class="pre">-d</span></code> flag. Note that <em>only</em> doctests will be refactored. This also doesn’t require the module to be valid Python. For example, doctest like examples in a reST document could also be refactored with this option.</p> <p>The <code class="xref std std-option docutils literal notranslate"><span class="pre">-v</span></code> option enables output of more information on the translation process.</p> <p>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 <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">print_function</span></code> compiler directive, it modifies its internal grammar to interpret <a class="reference internal" href="functions.html#print" title="print"><code class="xref py py-func docutils literal notranslate"><span class="pre">print()</span></code></a> as a function. This change can also be enabled manually with the <code class="xref std std-option docutils literal notranslate"><span class="pre">-p</span></code> flag. Use <code class="xref std std-option docutils literal notranslate"><span class="pre">-p</span></code> to run fixers on code that already has had its print statements converted.</p> <p>The <code class="xref std std-option docutils literal notranslate"><span class="pre">-o</span></code> or <code class="xref std std-option docutils literal notranslate"><span class="pre">--output-dir</span></code> option allows specification of an alternate directory for processed output files to be written to. The <code class="xref std std-option docutils literal notranslate"><span class="pre">-n</span></code> flag is required when using this as backup files do not make sense when not overwriting the input files.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.7.3: </span>The <code class="xref std std-option docutils literal notranslate"><span class="pre">-o</span></code> option was added.</p> </div> <p>The <code class="xref std std-option docutils literal notranslate"><span class="pre">-W</span></code> or <code class="xref std std-option docutils literal notranslate"><span class="pre">--write-unchanged-files</span></code> flag tells 2to3 to always write output files even if no changes were required to the file. This is most useful with <code class="xref std std-option docutils literal notranslate"><span class="pre">-o</span></code> so that an entire Python source tree is copied with translation from one directory to another. This option implies the <code class="xref std std-option docutils literal notranslate"><span class="pre">-w</span></code> flag as it would not make sense otherwise.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.7.3: </span>The <code class="xref std std-option docutils literal notranslate"><span class="pre">-W</span></code> flag was added.</p> </div> <p>The <code class="xref std std-option docutils literal notranslate"><span class="pre">--add-suffix</span></code> option specifies a string to append to all output filenames. The <code class="xref std std-option docutils literal notranslate"><span class="pre">-n</span></code> flag is required when specifying this as backups are not necessary when writing to different filenames. Example:</p> <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> 2to3 -n -W --add-suffix<span class="o">=</span><span class="m">3</span> example.py </pre></div> </div> <p>Will cause a converted file named <code class="docutils literal notranslate"><span class="pre">example.py3</span></code> to be written.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.7.3: </span>The <code class="xref std std-option docutils literal notranslate"><span class="pre">--add-suffix</span></code> option was added.</p> </div> <p>To translate an entire project from one directory tree to another use:</p> <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> 2to3 --output-dir<span class="o">=</span>python3-version/mycode -W -n python2-version/mycode </pre></div> </div> </div> <div class="section" id="fixers"> <span id="to3-fixers"></span><h2>25.4.2. Fixers<a class="headerlink" href="#fixers" title="Permalink to this headline">¶</a></h2> <p>Each step of transforming code is encapsulated in a fixer. The command <code class="docutils literal notranslate"><span class="pre">2to3</span> <span class="pre">-l</span></code> lists them. As <a class="reference internal" href="#to3-using"><span class="std std-ref">documented above</span></a>, each can be turned on and off individually. They are described here in more detail.</p> <dl class="2to3fixer"> <dt id="2to3fixer-apply"> <code class="descname">apply</code><a class="headerlink" href="#2to3fixer-apply" title="Permalink to this definition">¶</a></dt> <dd><p>Removes usage of <a class="reference internal" href="functions.html#apply" title="apply"><code class="xref py py-func docutils literal notranslate"><span class="pre">apply()</span></code></a>. For example <code class="docutils literal notranslate"><span class="pre">apply(function,</span> <span class="pre">*args,</span> <span class="pre">**kwargs)</span></code> is converted to <code class="docutils literal notranslate"><span class="pre">function(*args,</span> <span class="pre">**kwargs)</span></code>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-asserts"> <code class="descname">asserts</code><a class="headerlink" href="#2to3fixer-asserts" title="Permalink to this definition">¶</a></dt> <dd><p>Replaces deprecated <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> method names with the correct ones.</p> <table border="1" class="docutils"> <colgroup> <col width="43%" /> <col width="57%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">From</th> <th class="head">To</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">failUnlessEqual(a,</span> <span class="pre">b)</span></code></td> <td><a class="reference internal" href="unittest.html#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertEqual(a,</span> <span class="pre">b)</span></code></a></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">assertEquals(a,</span> <span class="pre">b)</span></code></td> <td><a class="reference internal" href="unittest.html#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertEqual(a,</span> <span class="pre">b)</span></code></a></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">failIfEqual(a,</span> <span class="pre">b)</span></code></td> <td><a class="reference internal" href="unittest.html#unittest.TestCase.assertNotEqual" title="unittest.TestCase.assertNotEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNotEqual(a,</span> <span class="pre">b)</span></code></a></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">assertNotEquals(a,</span> <span class="pre">b)</span></code></td> <td><a class="reference internal" href="unittest.html#unittest.TestCase.assertNotEqual" title="unittest.TestCase.assertNotEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNotEqual(a,</span> <span class="pre">b)</span></code></a></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">failUnless(a)</span></code></td> <td><a class="reference internal" href="unittest.html#unittest.TestCase.assertTrue" title="unittest.TestCase.assertTrue"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertTrue(a)</span></code></a></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">assert_(a)</span></code></td> <td><a class="reference internal" href="unittest.html#unittest.TestCase.assertTrue" title="unittest.TestCase.assertTrue"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertTrue(a)</span></code></a></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">failIf(a)</span></code></td> <td><a class="reference internal" href="unittest.html#unittest.TestCase.assertFalse" title="unittest.TestCase.assertFalse"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertFalse(a)</span></code></a></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">failUnlessRaises(exc,</span> <span class="pre">cal)</span></code></td> <td><a class="reference internal" href="unittest.html#unittest.TestCase.assertRaises" title="unittest.TestCase.assertRaises"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertRaises(exc,</span> <span class="pre">cal)</span></code></a></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">failUnlessAlmostEqual(a,</span> <span class="pre">b)</span></code></td> <td><a class="reference internal" href="unittest.html#unittest.TestCase.assertAlmostEqual" title="unittest.TestCase.assertAlmostEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertAlmostEqual(a,</span> <span class="pre">b)</span></code></a></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">assertAlmostEquals(a,</span> <span class="pre">b)</span></code></td> <td><a class="reference internal" href="unittest.html#unittest.TestCase.assertAlmostEqual" title="unittest.TestCase.assertAlmostEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertAlmostEqual(a,</span> <span class="pre">b)</span></code></a></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">failIfAlmostEqual(a,</span> <span class="pre">b)</span></code></td> <td><a class="reference internal" href="unittest.html#unittest.TestCase.assertNotAlmostEqual" title="unittest.TestCase.assertNotAlmostEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNotAlmostEqual(a,</span> <span class="pre">b)</span></code></a></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">assertNotAlmostEquals(a,</span> <span class="pre">b)</span></code></td> <td><a class="reference internal" href="unittest.html#unittest.TestCase.assertNotAlmostEqual" title="unittest.TestCase.assertNotAlmostEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNotAlmostEqual(a,</span> <span class="pre">b)</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-basestring"> <code class="descname">basestring</code><a class="headerlink" href="#2to3fixer-basestring" title="Permalink to this definition">¶</a></dt> <dd><p>Converts <a class="reference internal" href="functions.html#basestring" title="basestring"><code class="xref py py-class docutils literal notranslate"><span class="pre">basestring</span></code></a> to <a class="reference internal" href="functions.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-buffer"> <code class="descname">buffer</code><a class="headerlink" href="#2to3fixer-buffer" title="Permalink to this definition">¶</a></dt> <dd><p>Converts <a class="reference internal" href="functions.html#buffer" title="buffer"><code class="xref py py-class docutils literal notranslate"><span class="pre">buffer</span></code></a> to <a class="reference internal" href="stdtypes.html#memoryview" title="memoryview"><code class="xref py py-class docutils literal notranslate"><span class="pre">memoryview</span></code></a>. This fixer is optional because the <a class="reference internal" href="stdtypes.html#memoryview" title="memoryview"><code class="xref py py-class docutils literal notranslate"><span class="pre">memoryview</span></code></a> API is similar but not exactly the same as that of <a class="reference internal" href="functions.html#buffer" title="buffer"><code class="xref py py-class docutils literal notranslate"><span class="pre">buffer</span></code></a>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-dict"> <code class="descname">dict</code><a class="headerlink" href="#2to3fixer-dict" title="Permalink to this definition">¶</a></dt> <dd><p>Fixes dictionary iteration methods. <a class="reference internal" href="stdtypes.html#dict.iteritems" title="dict.iteritems"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.iteritems()</span></code></a> is converted to <a class="reference internal" href="stdtypes.html#dict.items" title="dict.items"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.items()</span></code></a>, <a class="reference internal" href="stdtypes.html#dict.iterkeys" title="dict.iterkeys"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.iterkeys()</span></code></a> to <a class="reference internal" href="stdtypes.html#dict.keys" title="dict.keys"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.keys()</span></code></a>, and <a class="reference internal" href="stdtypes.html#dict.itervalues" title="dict.itervalues"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.itervalues()</span></code></a> to <a class="reference internal" href="stdtypes.html#dict.values" title="dict.values"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.values()</span></code></a>. Similarly, <a class="reference internal" href="stdtypes.html#dict.viewitems" title="dict.viewitems"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.viewitems()</span></code></a>, <a class="reference internal" href="stdtypes.html#dict.viewkeys" title="dict.viewkeys"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.viewkeys()</span></code></a> and <a class="reference internal" href="stdtypes.html#dict.viewvalues" title="dict.viewvalues"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.viewvalues()</span></code></a> are converted respectively to <a class="reference internal" href="stdtypes.html#dict.items" title="dict.items"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.items()</span></code></a>, <a class="reference internal" href="stdtypes.html#dict.keys" title="dict.keys"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.keys()</span></code></a> and <a class="reference internal" href="stdtypes.html#dict.values" title="dict.values"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.values()</span></code></a>. It also wraps existing usages of <a class="reference internal" href="stdtypes.html#dict.items" title="dict.items"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.items()</span></code></a>, <a class="reference internal" href="stdtypes.html#dict.keys" title="dict.keys"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.keys()</span></code></a>, and <a class="reference internal" href="stdtypes.html#dict.values" title="dict.values"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.values()</span></code></a> in a call to <code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-except"> <code class="descname">except</code><a class="headerlink" href="#2to3fixer-except" title="Permalink to this definition">¶</a></dt> <dd><p>Converts <code class="docutils literal notranslate"><span class="pre">except</span> <span class="pre">X,</span> <span class="pre">T</span></code> to <code class="docutils literal notranslate"><span class="pre">except</span> <span class="pre">X</span> <span class="pre">as</span> <span class="pre">T</span></code>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-exec"> <code class="descname">exec</code><a class="headerlink" href="#2to3fixer-exec" title="Permalink to this definition">¶</a></dt> <dd><p>Converts the <a class="reference internal" href="../reference/simple_stmts.html#exec"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">exec</span></code></a> statement to the <code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code> function.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-execfile"> <code class="descname">execfile</code><a class="headerlink" href="#2to3fixer-execfile" title="Permalink to this definition">¶</a></dt> <dd><p>Removes usage of <a class="reference internal" href="functions.html#execfile" title="execfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">execfile()</span></code></a>. The argument to <a class="reference internal" href="functions.html#execfile" title="execfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">execfile()</span></code></a> is wrapped in calls to <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>, <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a>, and <code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-exitfunc"> <code class="descname">exitfunc</code><a class="headerlink" href="#2to3fixer-exitfunc" title="Permalink to this definition">¶</a></dt> <dd><p>Changes assignment of <a class="reference internal" href="sys.html#sys.exitfunc" title="sys.exitfunc"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.exitfunc</span></code></a> to use of the <a class="reference internal" href="atexit.html#module-atexit" title="atexit: Register and execute cleanup functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">atexit</span></code></a> module.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-filter"> <code class="descname">filter</code><a class="headerlink" href="#2to3fixer-filter" title="Permalink to this definition">¶</a></dt> <dd><p>Wraps <a class="reference internal" href="functions.html#filter" title="filter"><code class="xref py py-func docutils literal notranslate"><span class="pre">filter()</span></code></a> usage in a <code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code> call.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-funcattrs"> <code class="descname">funcattrs</code><a class="headerlink" href="#2to3fixer-funcattrs" title="Permalink to this definition">¶</a></dt> <dd><p>Fixes function attributes that have been renamed. For example, <code class="docutils literal notranslate"><span class="pre">my_function.func_closure</span></code> is converted to <code class="docutils literal notranslate"><span class="pre">my_function.__closure__</span></code>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-future"> <code class="descname">future</code><a class="headerlink" href="#2to3fixer-future" title="Permalink to this definition">¶</a></dt> <dd><p>Removes <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">new_feature</span></code> statements.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-getcwdu"> <code class="descname">getcwdu</code><a class="headerlink" href="#2to3fixer-getcwdu" title="Permalink to this definition">¶</a></dt> <dd><p>Renames <a class="reference internal" href="os.html#os.getcwdu" title="os.getcwdu"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.getcwdu()</span></code></a> to <a class="reference internal" href="os.html#os.getcwd" title="os.getcwd"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.getcwd()</span></code></a>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-has_key"> <code class="descname">has_key</code><a class="headerlink" href="#2to3fixer-has_key" title="Permalink to this definition">¶</a></dt> <dd><p>Changes <code class="docutils literal notranslate"><span class="pre">dict.has_key(key)</span></code> to <code class="docutils literal notranslate"><span class="pre">key</span> <span class="pre">in</span> <span class="pre">dict</span></code>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-idioms"> <code class="descname">idioms</code><a class="headerlink" href="#2to3fixer-idioms" title="Permalink to this definition">¶</a></dt> <dd><p>This optional fixer performs several transformations that make Python code more idiomatic. Type comparisons like <code class="docutils literal notranslate"><span class="pre">type(x)</span> <span class="pre">is</span> <span class="pre">SomeClass</span></code> and <code class="docutils literal notranslate"><span class="pre">type(x)</span> <span class="pre">==</span> <span class="pre">SomeClass</span></code> are converted to <code class="docutils literal notranslate"><span class="pre">isinstance(x,</span> <span class="pre">SomeClass)</span></code>. <code class="docutils literal notranslate"><span class="pre">while</span> <span class="pre">1</span></code> becomes <code class="docutils literal notranslate"><span class="pre">while</span> <span class="pre">True</span></code>. This fixer also tries to make use of <a class="reference internal" href="functions.html#sorted" title="sorted"><code class="xref py py-func docutils literal notranslate"><span class="pre">sorted()</span></code></a> in appropriate places. For example, this block</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">L</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">some_iterable</span><span class="p">)</span> <span class="n">L</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span> </pre></div> </div> <p>is changed to</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">L</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">some_iterable</span><span class="p">)</span> </pre></div> </div> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-import"> <code class="descname">import</code><a class="headerlink" href="#2to3fixer-import" title="Permalink to this definition">¶</a></dt> <dd><p>Detects sibling imports and converts them to relative imports.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-imports"> <code class="descname">imports</code><a class="headerlink" href="#2to3fixer-imports" title="Permalink to this definition">¶</a></dt> <dd><p>Handles module renames in the standard library.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-imports2"> <code class="descname">imports2</code><a class="headerlink" href="#2to3fixer-imports2" title="Permalink to this definition">¶</a></dt> <dd><p>Handles other modules renames in the standard library. It is separate from the <a class="reference internal" href="#2to3fixer-imports"><code class="xref std std-2to3fixer docutils literal notranslate"><span class="pre">imports</span></code></a> fixer only because of technical limitations.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-input"> <code class="descname">input</code><a class="headerlink" href="#2to3fixer-input" title="Permalink to this definition">¶</a></dt> <dd><p>Converts <code class="docutils literal notranslate"><span class="pre">input(prompt)</span></code> to <code class="docutils literal notranslate"><span class="pre">eval(input(prompt))</span></code>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-intern"> <code class="descname">intern</code><a class="headerlink" href="#2to3fixer-intern" title="Permalink to this definition">¶</a></dt> <dd><p>Converts <a class="reference internal" href="functions.html#intern" title="intern"><code class="xref py py-func docutils literal notranslate"><span class="pre">intern()</span></code></a> to <code class="xref py py-func docutils literal notranslate"><span class="pre">sys.intern()</span></code>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-isinstance"> <code class="descname">isinstance</code><a class="headerlink" href="#2to3fixer-isinstance" title="Permalink to this definition">¶</a></dt> <dd><p>Fixes duplicate types in the second argument of <a class="reference internal" href="functions.html#isinstance" title="isinstance"><code class="xref py py-func docutils literal notranslate"><span class="pre">isinstance()</span></code></a>. For example, <code class="docutils literal notranslate"><span class="pre">isinstance(x,</span> <span class="pre">(int,</span> <span class="pre">int))</span></code> is converted to <code class="docutils literal notranslate"><span class="pre">isinstance(x,</span> <span class="pre">(int))</span></code>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-itertools_imports"> <code class="descname">itertools_imports</code><a class="headerlink" href="#2to3fixer-itertools_imports" title="Permalink to this definition">¶</a></dt> <dd><p>Removes imports of <a class="reference internal" href="itertools.html#itertools.ifilter" title="itertools.ifilter"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.ifilter()</span></code></a>, <a class="reference internal" href="itertools.html#itertools.izip" title="itertools.izip"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.izip()</span></code></a>, and <a class="reference internal" href="itertools.html#itertools.imap" title="itertools.imap"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.imap()</span></code></a>. Imports of <a class="reference internal" href="itertools.html#itertools.ifilterfalse" title="itertools.ifilterfalse"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.ifilterfalse()</span></code></a> are also changed to <code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.filterfalse()</span></code>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-itertools"> <code class="descname">itertools</code><a class="headerlink" href="#2to3fixer-itertools" title="Permalink to this definition">¶</a></dt> <dd><p>Changes usage of <a class="reference internal" href="itertools.html#itertools.ifilter" title="itertools.ifilter"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.ifilter()</span></code></a>, <a class="reference internal" href="itertools.html#itertools.izip" title="itertools.izip"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.izip()</span></code></a>, and <a class="reference internal" href="itertools.html#itertools.imap" title="itertools.imap"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.imap()</span></code></a> to their built-in equivalents. <a class="reference internal" href="itertools.html#itertools.ifilterfalse" title="itertools.ifilterfalse"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.ifilterfalse()</span></code></a> is changed to <code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.filterfalse()</span></code>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-long"> <code class="descname">long</code><a class="headerlink" href="#2to3fixer-long" title="Permalink to this definition">¶</a></dt> <dd><p>Renames <a class="reference internal" href="functions.html#long" title="long"><code class="xref py py-class docutils literal notranslate"><span class="pre">long</span></code></a> to <a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-map"> <code class="descname">map</code><a class="headerlink" href="#2to3fixer-map" title="Permalink to this definition">¶</a></dt> <dd><p>Wraps <a class="reference internal" href="functions.html#map" title="map"><code class="xref py py-func docutils literal notranslate"><span class="pre">map()</span></code></a> in a <code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code> call. It also changes <code class="docutils literal notranslate"><span class="pre">map(None,</span> <span class="pre">x)</span></code> to <code class="docutils literal notranslate"><span class="pre">list(x)</span></code>. Using <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">future_builtins</span> <span class="pre">import</span> <span class="pre">map</span></code> disables this fixer.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-metaclass"> <code class="descname">metaclass</code><a class="headerlink" href="#2to3fixer-metaclass" title="Permalink to this definition">¶</a></dt> <dd><p>Converts the old metaclass syntax (<code class="docutils literal notranslate"><span class="pre">__metaclass__</span> <span class="pre">=</span> <span class="pre">Meta</span></code> in the class body) to the new (<code class="docutils literal notranslate"><span class="pre">class</span> <span class="pre">X(metaclass=Meta)</span></code>).</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-methodattrs"> <code class="descname">methodattrs</code><a class="headerlink" href="#2to3fixer-methodattrs" title="Permalink to this definition">¶</a></dt> <dd><p>Fixes old method attribute names. For example, <code class="docutils literal notranslate"><span class="pre">meth.im_func</span></code> is converted to <code class="docutils literal notranslate"><span class="pre">meth.__func__</span></code>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-ne"> <code class="descname">ne</code><a class="headerlink" href="#2to3fixer-ne" title="Permalink to this definition">¶</a></dt> <dd><p>Converts the old not-equal syntax, <code class="docutils literal notranslate"><span class="pre"><></span></code>, to <code class="docutils literal notranslate"><span class="pre">!=</span></code>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-next"> <code class="descname">next</code><a class="headerlink" href="#2to3fixer-next" title="Permalink to this definition">¶</a></dt> <dd><p>Converts the use of iterator’s <a class="reference internal" href="stdtypes.html#iterator.next" title="iterator.next"><code class="xref py py-meth docutils literal notranslate"><span class="pre">next()</span></code></a> methods to the <a class="reference internal" href="functions.html#next" title="next"><code class="xref py py-func docutils literal notranslate"><span class="pre">next()</span></code></a> function. It also renames <a class="reference internal" href="stdtypes.html#iterator.next" title="iterator.next"><code class="xref py py-meth docutils literal notranslate"><span class="pre">next()</span></code></a> methods to <code class="xref py py-meth docutils literal notranslate"><span class="pre">__next__()</span></code>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-nonzero"> <code class="descname">nonzero</code><a class="headerlink" href="#2to3fixer-nonzero" title="Permalink to this definition">¶</a></dt> <dd><p>Renames <a class="reference internal" href="../reference/datamodel.html#object.__nonzero__" title="object.__nonzero__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__nonzero__()</span></code></a> to <code class="xref py py-meth docutils literal notranslate"><span class="pre">__bool__()</span></code>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-numliterals"> <code class="descname">numliterals</code><a class="headerlink" href="#2to3fixer-numliterals" title="Permalink to this definition">¶</a></dt> <dd><p>Converts octal literals into the new syntax.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-paren"> <code class="descname">paren</code><a class="headerlink" href="#2to3fixer-paren" title="Permalink to this definition">¶</a></dt> <dd><p>Add extra parenthesis where they are required in list comprehensions. For example, <code class="docutils literal notranslate"><span class="pre">[x</span> <span class="pre">for</span> <span class="pre">x</span> <span class="pre">in</span> <span class="pre">1,</span> <span class="pre">2]</span></code> becomes <code class="docutils literal notranslate"><span class="pre">[x</span> <span class="pre">for</span> <span class="pre">x</span> <span class="pre">in</span> <span class="pre">(1,</span> <span class="pre">2)]</span></code>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-print"> <code class="descname">print</code><a class="headerlink" href="#2to3fixer-print" title="Permalink to this definition">¶</a></dt> <dd><p>Converts the <a class="reference internal" href="../reference/simple_stmts.html#print"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">print</span></code></a> statement to the <a class="reference internal" href="functions.html#print" title="print"><code class="xref py py-func docutils literal notranslate"><span class="pre">print()</span></code></a> function.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-raise"> <code class="descname">raise</code><a class="headerlink" href="#2to3fixer-raise" title="Permalink to this definition">¶</a></dt> <dd><p>Converts <code class="docutils literal notranslate"><span class="pre">raise</span> <span class="pre">E,</span> <span class="pre">V</span></code> to <code class="docutils literal notranslate"><span class="pre">raise</span> <span class="pre">E(V)</span></code>, and <code class="docutils literal notranslate"><span class="pre">raise</span> <span class="pre">E,</span> <span class="pre">V,</span> <span class="pre">T</span></code> to <code class="docutils literal notranslate"><span class="pre">raise</span> <span class="pre">E(V).with_traceback(T)</span></code>. If <code class="docutils literal notranslate"><span class="pre">E</span></code> is a tuple, the translation will be incorrect because substituting tuples for exceptions has been removed in Python 3.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-raw_input"> <code class="descname">raw_input</code><a class="headerlink" href="#2to3fixer-raw_input" title="Permalink to this definition">¶</a></dt> <dd><p>Converts <a class="reference internal" href="functions.html#raw_input" title="raw_input"><code class="xref py py-func docutils literal notranslate"><span class="pre">raw_input()</span></code></a> to <a class="reference internal" href="functions.html#input" title="input"><code class="xref py py-func docutils literal notranslate"><span class="pre">input()</span></code></a>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-reduce"> <code class="descname">reduce</code><a class="headerlink" href="#2to3fixer-reduce" title="Permalink to this definition">¶</a></dt> <dd><p>Handles the move of <a class="reference internal" href="functions.html#reduce" title="reduce"><code class="xref py py-func docutils literal notranslate"><span class="pre">reduce()</span></code></a> to <a class="reference internal" href="functools.html#functools.reduce" title="functools.reduce"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.reduce()</span></code></a>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-renames"> <code class="descname">renames</code><a class="headerlink" href="#2to3fixer-renames" title="Permalink to this definition">¶</a></dt> <dd><p>Changes <a class="reference internal" href="sys.html#sys.maxint" title="sys.maxint"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.maxint</span></code></a> to <a class="reference internal" href="sys.html#sys.maxsize" title="sys.maxsize"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.maxsize</span></code></a>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-repr"> <code class="descname">repr</code><a class="headerlink" href="#2to3fixer-repr" title="Permalink to this definition">¶</a></dt> <dd><p>Replaces backtick repr with the <a class="reference internal" href="functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> function.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-set_literal"> <code class="descname">set_literal</code><a class="headerlink" href="#2to3fixer-set_literal" title="Permalink to this definition">¶</a></dt> <dd><p>Replaces use of the <a class="reference internal" href="stdtypes.html#set" title="set"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a> constructor with set literals. This fixer is optional.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-standarderror"> <code class="descname">standarderror</code><a class="headerlink" href="#2to3fixer-standarderror" title="Permalink to this definition">¶</a></dt> <dd><p>Renames <a class="reference internal" href="exceptions.html#exceptions.StandardError" title="exceptions.StandardError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">StandardError</span></code></a> to <a class="reference internal" href="exceptions.html#exceptions.Exception" title="exceptions.Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-sys_exc"> <code class="descname">sys_exc</code><a class="headerlink" href="#2to3fixer-sys_exc" title="Permalink to this definition">¶</a></dt> <dd><p>Changes the deprecated <a class="reference internal" href="sys.html#sys.exc_value" title="sys.exc_value"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.exc_value</span></code></a>, <a class="reference internal" href="sys.html#sys.exc_type" title="sys.exc_type"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.exc_type</span></code></a>, <a class="reference internal" href="sys.html#sys.exc_traceback" title="sys.exc_traceback"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.exc_traceback</span></code></a> to use <a class="reference internal" href="sys.html#sys.exc_info" title="sys.exc_info"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exc_info()</span></code></a>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-throw"> <code class="descname">throw</code><a class="headerlink" href="#2to3fixer-throw" title="Permalink to this definition">¶</a></dt> <dd><p>Fixes the API change in generator’s <code class="xref py py-meth docutils literal notranslate"><span class="pre">throw()</span></code> method.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-tuple_params"> <code class="descname">tuple_params</code><a class="headerlink" href="#2to3fixer-tuple_params" title="Permalink to this definition">¶</a></dt> <dd><p>Removes implicit tuple parameter unpacking. This fixer inserts temporary variables.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-types"> <code class="descname">types</code><a class="headerlink" href="#2to3fixer-types" title="Permalink to this definition">¶</a></dt> <dd><p>Fixes code broken from the removal of some members in the <a class="reference internal" href="types.html#module-types" title="types: Names for built-in types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">types</span></code></a> module.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-unicode"> <code class="descname">unicode</code><a class="headerlink" href="#2to3fixer-unicode" title="Permalink to this definition">¶</a></dt> <dd><p>Renames <a class="reference internal" href="functions.html#unicode" title="unicode"><code class="xref py py-class docutils literal notranslate"><span class="pre">unicode</span></code></a> to <a class="reference internal" href="functions.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-urllib"> <code class="descname">urllib</code><a class="headerlink" href="#2to3fixer-urllib" title="Permalink to this definition">¶</a></dt> <dd><p>Handles the rename of <a class="reference internal" href="urllib.html#module-urllib" title="urllib: Open an arbitrary network resource by URL (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib</span></code></a> and <a class="reference internal" href="urllib2.html#module-urllib2" title="urllib2: Next generation URL opening library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib2</span></code></a> to the <a class="reference internal" href="urllib.html#module-urllib" title="urllib: Open an arbitrary network resource by URL (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib</span></code></a> package.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-ws_comma"> <code class="descname">ws_comma</code><a class="headerlink" href="#2to3fixer-ws_comma" title="Permalink to this definition">¶</a></dt> <dd><p>Removes excess whitespace from comma separated items. This fixer is optional.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-xrange"> <code class="descname">xrange</code><a class="headerlink" href="#2to3fixer-xrange" title="Permalink to this definition">¶</a></dt> <dd><p>Renames <a class="reference internal" href="functions.html#xrange" title="xrange"><code class="xref py py-func docutils literal notranslate"><span class="pre">xrange()</span></code></a> to <a class="reference internal" href="functions.html#range" title="range"><code class="xref py py-func docutils literal notranslate"><span class="pre">range()</span></code></a> and wraps existing <a class="reference internal" href="functions.html#range" title="range"><code class="xref py py-func docutils literal notranslate"><span class="pre">range()</span></code></a> calls with <code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-xreadlines"> <code class="descname">xreadlines</code><a class="headerlink" href="#2to3fixer-xreadlines" title="Permalink to this definition">¶</a></dt> <dd><p>Changes <code class="docutils literal notranslate"><span class="pre">for</span> <span class="pre">x</span> <span class="pre">in</span> <span class="pre">file.xreadlines()</span></code> to <code class="docutils literal notranslate"><span class="pre">for</span> <span class="pre">x</span> <span class="pre">in</span> <span class="pre">file</span></code>.</p> </dd></dl> <dl class="2to3fixer"> <dt id="2to3fixer-zip"> <code class="descname">zip</code><a class="headerlink" href="#2to3fixer-zip" title="Permalink to this definition">¶</a></dt> <dd><p>Wraps <a class="reference internal" href="functions.html#zip" title="zip"><code class="xref py py-func docutils literal notranslate"><span class="pre">zip()</span></code></a> usage in a <code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code> call. This is disabled when <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">future_builtins</span> <span class="pre">import</span> <span class="pre">zip</span></code> appears.</p> </dd></dl> </div> <div class="section" id="module-lib2to3"> <span id="lib2to3-2to3-s-library"></span><h2>25.4.3. <a class="reference internal" href="#module-lib2to3" title="lib2to3: the 2to3 library"><code class="xref py py-mod docutils literal notranslate"><span class="pre">lib2to3</span></code></a> - 2to3’s library<a class="headerlink" href="#module-lib2to3" title="Permalink to this headline">¶</a></h2> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">The <a class="reference internal" href="#module-lib2to3" title="lib2to3: the 2to3 library"><code class="xref py py-mod docutils literal notranslate"><span class="pre">lib2to3</span></code></a> API should be considered unstable and may change drastically in the future.</p> </div> </div> </div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">25.4. 2to3 - Automated Python 2 to 3 code translation</a><ul> <li><a class="reference internal" href="#using-2to3">25.4.1. Using 2to3</a></li> <li><a class="reference internal" href="#fixers">25.4.2. Fixers</a></li> <li><a class="reference internal" href="#module-lib2to3">25.4.3. <code class="docutils literal notranslate"><span class="pre">lib2to3</span></code> - 2to3’s library</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="unittest.html" title="previous chapter">25.3. <code class="docutils literal notranslate"><span class="pre">unittest</span></code> — Unit testing framework</a></p> <h4>Next topic</h4> <p class="topless"><a href="test.html" title="next chapter">25.5. <code class="docutils literal notranslate"><span class="pre">test</span></code> — Regression tests package for Python</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/library/2to3.rst.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3>Quick search</h3> <div class="searchformwrapper"> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="test.html" title="25.5. test — Regression tests package for Python" >next</a> |</li> <li class="right" > <a href="unittest.html" title="25.3. unittest — Unit testing framework" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.16 documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-2"><a href="development.html" >25. Development Tools</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="https://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Mar 27, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.7.6. </div> </body> </html>
Save
cmd:
run