1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
|
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Automatic conversion to Py2/3 — Python-Future documentation</title>
<link rel="stylesheet" href="_static/basic.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/my-styles.css" type="text/css" />
<link rel="stylesheet" href="_static/bootswatch-3.3.4/cerulean/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="_static/bootstrap-sphinx.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</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/js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="_static/js/jquery-fix.js"></script>
<script type="text/javascript" src="_static/bootstrap-3.3.4/js/bootstrap.min.js"></script>
<script type="text/javascript" src="_static/bootstrap-sphinx.js"></script>
<link rel="shortcut icon" href="_static/python-future-icon-32.ico"/>
<link rel="top" title="Python-Future documentation" href="index.html" />
<link rel="next" title="Frequently Asked Questions (FAQ)" href="faq.html" />
<link rel="prev" title="What else you need to know" href="what_else.html" />
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1'>
<meta name="apple-mobile-web-app-capable" content="yes">
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-19344199-2']);
_gaq.push(['_trackPageview']);
</script>
</head>
<body role="document">
<a href="https://github.com/PythonCharmers/python-future"><img style="position: absolute; top: 45px; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png" alt="Fork me on GitHub"></a>
<div id="navbar" class="navbar navbar-inverse navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<!-- .btn-navbar is used as the toggle for collapsed navbar content -->
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="logo"> <img height="32" width="32" src="_static/python-future-logo-textless-transparent.png" /></a>
<a class="navbar-brand" href="index.html">Python-Future</a>
<span class="navbar-text navbar-version pull-left"><b></b></span>
</div>
<div class="collapse navbar-collapse nav-collapse">
<ul class="nav navbar-nav">
<li class="divider-vertical"></li>
<li><a href="overview.html">Overview</a></li>
<li><a href="compatible_idioms.html">Cheat Sheet</a></li>
<li><a href="faq.html">FAQ</a></li>
<li class="dropdown globaltoc-container">
<a role="button"
id="dLabelGlobalToc"
data-toggle="dropdown"
data-target="#"
href="index.html">Contents <b class="caret"></b></a>
<ul class="dropdown-menu globaltoc"
role="menu"
aria-labelledby="dLabelGlobalToc"><ul class="current">
<li class="toctree-l1"><a class="reference internal" href="whatsnew.html">What’s New</a><ul>
<li class="toctree-l2"><a class="reference internal" href="whatsnew.html#what-s-new-in-version-0-15-0-2015-07-25">What’s new in version 0.15.0 (2015-07-25)</a></li>
<li class="toctree-l2"><a class="reference internal" href="whatsnew.html#what-s-new-in-version-0-14-3-2014-12-15">What’s new in version 0.14.3 (2014-12-15)</a></li>
<li class="toctree-l2"><a class="reference internal" href="whatsnew.html#what-s-new-in-version-0-14-2-2014-11-21">What’s new in version 0.14.2 (2014-11-21)</a></li>
<li class="toctree-l2"><a class="reference internal" href="whatsnew.html#what-s-new-in-version-0-14-1-2014-10-02">What’s new in version 0.14.1 (2014-10-02)</a></li>
<li class="toctree-l2"><a class="reference internal" href="whatsnew.html#what-s-new-in-version-0-14-2014-10-02">What’s new in version 0.14 (2014-10-02)</a><ul>
<li class="toctree-l3"><a class="reference internal" href="whatsnew.html#bug-fixes">Bug fixes</a></li>
<li class="toctree-l3"><a class="reference internal" href="whatsnew.html#internal-cleanups">Internal cleanups</a></li>
<li class="toctree-l3"><a class="reference internal" href="whatsnew.html#deprecations">Deprecations</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="whatsnew.html#previous-versions">Previous versions</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="overview.html">Overview: Easy, clean, reliable Python 2/3 compatibility</a><ul>
<li class="toctree-l2"><a class="reference internal" href="overview.html#features">Features</a></li>
<li class="toctree-l2"><a class="reference internal" href="overview.html#code-examples">Code examples</a></li>
<li class="toctree-l2"><a class="reference internal" href="overview.html#automatic-conversion-to-py2-3-compatible-code">Automatic conversion to Py2/3-compatible code</a><ul>
<li class="toctree-l3"><a class="reference internal" href="overview.html#futurize-2-to-both">Futurize: 2 to both</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="overview.html#automatic-translation">Automatic translation</a></li>
<li class="toctree-l2"><a class="reference internal" href="overview.html#licensing">Licensing</a></li>
<li class="toctree-l2"><a class="reference internal" href="overview.html#next-steps">Next steps</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="quickstart.html">Quick-start guide</a><ul>
<li class="toctree-l2"><a class="reference internal" href="quickstart.html#installation">Installation</a></li>
<li class="toctree-l2"><a class="reference internal" href="quickstart.html#if-you-are-writing-code-from-scratch">If you are writing code from scratch</a></li>
<li class="toctree-l2"><a class="reference internal" href="quickstart.html#to-convert-existing-python-3-code">To convert existing Python 3 code</a></li>
<li class="toctree-l2"><a class="reference internal" href="quickstart.html#to-convert-existing-python-2-code">To convert existing Python 2 code</a></li>
<li class="toctree-l2"><a class="reference internal" href="quickstart.html#standard-library-reorganization">Standard library reorganization</a></li>
<li class="toctree-l2"><a class="reference internal" href="quickstart.html#python-2-only-dependencies">Python 2-only dependencies</a></li>
<li class="toctree-l2"><a class="reference internal" href="quickstart.html#next-steps">Next steps</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="compatible_idioms.html">Cheat Sheet: Writing Python 2-3 compatible code</a><ul>
<li class="toctree-l2"><a class="reference internal" href="compatible_idioms.html#setup">Setup</a></li>
<li class="toctree-l2"><a class="reference internal" href="compatible_idioms.html#essential-syntax-differences">Essential syntax differences</a><ul>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#print">print</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#raising-exceptions">Raising exceptions</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#catching-exceptions">Catching exceptions</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#division">Division</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#long-integers">Long integers</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#octal-constants">Octal constants</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#backtick-repr">Backtick repr</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#metaclasses">Metaclasses</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="compatible_idioms.html#strings-and-bytes">Strings and bytes</a><ul>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#unicode-text-string-literals">Unicode (text) string literals</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#byte-string-literals">Byte-string literals</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#basestring">basestring</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#unicode">unicode</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#stringio">StringIO</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="compatible_idioms.html#imports-relative-to-a-package">Imports relative to a package</a></li>
<li class="toctree-l2"><a class="reference internal" href="compatible_idioms.html#dictionaries">Dictionaries</a><ul>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#iterating-through-dict-keys-values-items">Iterating through <code class="docutils literal"><span class="pre">dict</span></code> keys/values/items</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#dict-keys-values-items-as-a-list">dict keys/values/items as a list</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="compatible_idioms.html#custom-class-behaviour">Custom class behaviour</a><ul>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#custom-iterators">Custom iterators</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#custom-str-methods">Custom <code class="docutils literal"><span class="pre">__str__</span></code> methods</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#custom-nonzero-vs-bool-method">Custom <code class="docutils literal"><span class="pre">__nonzero__</span></code> vs <code class="docutils literal"><span class="pre">__bool__</span></code> method:</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="compatible_idioms.html#lists-versus-iterators">Lists versus iterators</a><ul>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#xrange">xrange</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#range">range</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#map">map</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#imap">imap</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#zip-izip">zip, izip</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#filter-ifilter">filter, ifilter</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="compatible_idioms.html#other-builtins">Other builtins</a><ul>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#file-io-with-open">File IO with open()</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#reduce">reduce()</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#raw-input">raw_input()</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#input">input()</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#file">file()</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#execfile">execfile()</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#unichr">unichr()</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#intern">intern()</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#apply">apply()</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#chr">chr()</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#cmp">cmp()</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#reload">reload()</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="compatible_idioms.html#standard-library">Standard library</a><ul>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#dbm-modules">dbm modules</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#commands-subprocess-modules">commands / subprocess modules</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#subprocess-check-output">subprocess.check_output()</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#collections-counter-and-ordereddict">collections: Counter and OrderedDict</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#stringio-module">StringIO module</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#http-module">http module</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#xmlrpc-module">xmlrpc module</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#html-escaping-and-entities">html escaping and entities</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#html-parsing">html parsing</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#urllib-module">urllib module</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#tkinter">Tkinter</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#socketserver">socketserver</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#copy-reg-copyreg">copy_reg, copyreg</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#configparser">configparser</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#queue">queue</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#repr-reprlib">repr, reprlib</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#userdict-userlist-userstring">UserDict, UserList, UserString</a></li>
<li class="toctree-l3"><a class="reference internal" href="compatible_idioms.html#itertools-filterfalse-zip-longest">itertools: filterfalse, zip_longest</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="imports.html">Imports</a><ul>
<li class="toctree-l2"><a class="reference internal" href="imports.html#future-imports">__future__ imports</a></li>
<li class="toctree-l2"><a class="reference internal" href="imports.html#imports-of-builtins">Imports of builtins</a><ul>
<li class="toctree-l3"><a class="reference internal" href="imports.html#implicit-imports">Implicit imports</a></li>
<li class="toctree-l3"><a class="reference internal" href="imports.html#explicit-imports">Explicit imports</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="imports.html#standard-library-imports">Standard library imports</a><ul>
<li class="toctree-l3"><a class="reference internal" href="imports.html#direct-imports">Direct imports</a></li>
<li class="toctree-l3"><a class="reference internal" href="imports.html#aliased-imports">Aliased imports</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="imports.html#external-standard-library-backports">External standard-library backports</a></li>
<li class="toctree-l2"><a class="reference internal" href="imports.html#included-full-backports">Included full backports</a></li>
<li class="toctree-l2"><a class="reference internal" href="imports.html#using-python-2-only-dependencies-on-python-3">Using Python 2-only dependencies on Python 3</a></li>
<li class="toctree-l2"><a class="reference internal" href="imports.html#should-i-import-unicode-literals">Should I import unicode_literals?</a><ul>
<li class="toctree-l3"><a class="reference internal" href="imports.html#benefits">Benefits</a></li>
<li class="toctree-l3"><a class="reference internal" href="imports.html#drawbacks">Drawbacks</a></li>
<li class="toctree-l3"><a class="reference internal" href="imports.html#others-perspectives">Others’ perspectives</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="imports.html#next-steps">Next steps</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="what_else.html">What else you need to know</a><ul>
<li class="toctree-l2"><a class="reference internal" href="what_else.html#bytes">bytes</a></li>
<li class="toctree-l2"><a class="reference internal" href="what_else.html#str">str</a></li>
<li class="toctree-l2"><a class="reference internal" href="what_else.html#dict">dict</a><ul>
<li class="toctree-l3"><a class="reference internal" href="what_else.html#memory-efficiency-and-alternatives">Memory-efficiency and alternatives</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="what_else.html#int">int</a></li>
<li class="toctree-l2"><a class="reference internal" href="what_else.html#isinstance">isinstance</a></li>
<li class="toctree-l2"><a class="reference internal" href="what_else.html#passing-data-to-from-python-2-libraries">Passing data to/from Python 2 libraries</a></li>
<li class="toctree-l2"><a class="reference internal" href="what_else.html#native-string-type">Native string type</a></li>
<li class="toctree-l2"><a class="reference internal" href="what_else.html#open">open()</a></li>
<li class="toctree-l2"><a class="reference internal" href="what_else.html#custom-str-methods">Custom __str__ methods</a></li>
<li class="toctree-l2"><a class="reference internal" href="what_else.html#custom-iterators">Custom iterators</a></li>
<li class="toctree-l2"><a class="reference internal" href="what_else.html#binding-a-method-to-a-class">Binding a method to a class</a></li>
<li class="toctree-l2"><a class="reference internal" href="what_else.html#metaclasses">Metaclasses</a></li>
</ul>
</li>
<li class="toctree-l1 current"><a class="current reference internal" href="">Automatic conversion to Py2/3</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#futurize-py2-to-py2-3"><code class="docutils literal"><span class="pre">futurize</span></code>: Py2 to Py2/3</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#stage-1-safe-fixes">Stage 1: “safe” fixes</a></li>
<li class="toctree-l3"><a class="reference internal" href="#stage-2-py3-style-code-with-wrappers-for-py2">Stage 2: Py3-style code with wrappers for Py2</a></li>
<li class="toctree-l3"><a class="reference internal" href="#separating-text-from-bytes">Separating text from bytes</a></li>
<li class="toctree-l3"><a class="reference internal" href="#post-conversion">Post-conversion</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#futurize-quick-start-guide"><code class="docutils literal"><span class="pre">futurize</span></code> quick-start guide</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#step-0-setup">Step 0: setup</a></li>
<li class="toctree-l3"><a class="reference internal" href="#step-1-modern-py2-code">Step 1: modern Py2 code</a></li>
<li class="toctree-l3"><a class="reference internal" href="#step-2-working-py3-code-that-still-supports-py2">Step 2: working Py3 code that still supports Py2</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#pasteurize-py3-to-py2-3"><code class="docutils literal"><span class="pre">pasteurize</span></code>: Py3 to Py2/3</a></li>
<li class="toctree-l2"><a class="reference internal" href="#known-limitations">Known limitations</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="faq.html">Frequently Asked Questions (FAQ)</a><ul>
<li class="toctree-l2"><a class="reference internal" href="faq.html#who-is-this-for">Who is this for?</a></li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#why-upgrade-to-python-3">Why upgrade to Python 3?</a></li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#porting-philosophy">Porting philosophy</a><ul>
<li class="toctree-l3"><a class="reference internal" href="faq.html#why-write-python-3-style-code">Why write Python 3-style code?</a></li>
<li class="toctree-l3"><a class="reference internal" href="faq.html#can-t-i-just-roll-my-own-py2-3-compatibility-layer">Can’t I just roll my own Py2/3 compatibility layer?</a></li>
<li class="toctree-l3"><a class="reference internal" href="faq.html#what-inspired-this-project">What inspired this project?</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#maturity">Maturity</a><ul>
<li class="toctree-l3"><a class="reference internal" href="faq.html#how-well-has-it-been-tested">How well has it been tested?</a></li>
<li class="toctree-l3"><a class="reference internal" href="faq.html#is-the-api-stable">Is the API stable?</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#relationship-between-python-future-and-other-compatibility-tools">Relationship between python-future and other compatibility tools</a><ul>
<li class="toctree-l3"><a class="reference internal" href="faq.html#how-does-this-relate-to-2to3">How does this relate to <code class="docutils literal"><span class="pre">2to3</span></code>?</a></li>
<li class="toctree-l3"><a class="reference internal" href="faq.html#can-i-maintain-a-python-2-codebase-and-use-2to3-to-automatically-convert-to-python-3-in-the-setup-script">Can I maintain a Python 2 codebase and use 2to3 to automatically convert to Python 3 in the setup script?</a></li>
<li class="toctree-l3"><a class="reference internal" href="faq.html#what-is-the-relationship-between-future-and-six">What is the relationship between <code class="docutils literal"><span class="pre">future</span></code> and <code class="docutils literal"><span class="pre">six</span></code>?</a></li>
<li class="toctree-l3"><a class="reference internal" href="faq.html#what-is-the-relationship-between-python-future-and-python-modernize">What is the relationship between <code class="docutils literal"><span class="pre">python-future</span></code> and <code class="docutils literal"><span class="pre">python-modernize</span></code>?</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#platform-and-version-support">Platform and version support</a><ul>
<li class="toctree-l3"><a class="reference internal" href="faq.html#which-versions-of-python-does-python-future-support">Which versions of Python does <code class="docutils literal"><span class="pre">python-future</span></code> support?</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#support">Support</a><ul>
<li class="toctree-l3"><a class="reference internal" href="faq.html#is-there-a-mailing-list">Is there a mailing list?</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#contributing">Contributing</a><ul>
<li class="toctree-l3"><a class="reference internal" href="faq.html#can-i-help">Can I help?</a></li>
<li class="toctree-l3"><a class="reference internal" href="faq.html#where-is-the-repo">Where is the repo?</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="stdlib_incompatibilities.html">Standard library incompatibilities</a><ul>
<li class="toctree-l2"><a class="reference internal" href="stdlib_incompatibilities.html#array-array">array.array()</a></li>
<li class="toctree-l2"><a class="reference internal" href="stdlib_incompatibilities.html#array-array-read">array.array.read()</a></li>
<li class="toctree-l2"><a class="reference internal" href="stdlib_incompatibilities.html#base64-decodebytes-and-base64-encodebytes">base64.decodebytes() and base64.encodebytes()</a></li>
<li class="toctree-l2"><a class="reference internal" href="stdlib_incompatibilities.html#re-ascii">re.ASCII</a></li>
<li class="toctree-l2"><a class="reference internal" href="stdlib_incompatibilities.html#struct-pack">struct.pack()</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="older_interfaces.html">Older interfaces</a><ul>
<li class="toctree-l2"><a class="reference internal" href="older_interfaces.html#context-manager-for-import-hooks">Context-manager for import hooks</a></li>
<li class="toctree-l2"><a class="reference internal" href="older_interfaces.html#future-moves-interface"><code class="docutils literal"><span class="pre">future.moves</span></code> interface</a><ul>
<li class="toctree-l3"><a class="reference internal" href="older_interfaces.html#comparing-future-moves-and-six-moves">Comparing future.moves and six.moves</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="older_interfaces.html#import-and-from-import-functions"><code class="docutils literal"><span class="pre">import_</span></code> and <code class="docutils literal"><span class="pre">from_import</span></code> functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="older_interfaces.html#install-hooks-call">install_hooks() call</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="changelog.html">Changes in previous versions</a><ul>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#changes-in-version-0-13-1-2014-09-23">Changes in version 0.13.1 (2014-09-23)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#changes-in-version-0-13-2014-08-13">Changes in version 0.13 (2014-08-13)</a><ul>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#deprecations">Deprecations</a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#new-features">New features</a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#bug-fixes">Bug fixes</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#changes-in-version-0-12-4-2014-07-18">Changes in version 0.12.4 (2014-07-18)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#changes-in-version-0-12-3-2014-06-19">Changes in version 0.12.3 (2014-06-19)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#changes-in-version-0-12-2-2014-05-25">Changes in version 0.12.2 (2014-05-25)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#changes-in-version-0-12-1-2014-05-14">Changes in version 0.12.1 (2014-05-14)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#changes-in-version-0-12-0-2014-05-06">Changes in version 0.12.0 (2014-05-06)</a><ul>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#more-robust-standard-library-import-hooks">More robust standard-library import hooks</a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#newobject-base-object-defines-fallback-py2-compatible-special-methods"><code class="docutils literal"><span class="pre">newobject</span></code> base object defines fallback Py2-compatible special methods</a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#past-builtins-module-improved"><code class="docutils literal"><span class="pre">past.builtins</span></code> module improved</a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#surrogateescape-error-handler"><code class="docutils literal"><span class="pre">surrogateescape</span></code> error handler</a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#newlist-type"><code class="docutils literal"><span class="pre">newlist</span></code> type</a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#listvalues-and-listitems"><code class="docutils literal"><span class="pre">listvalues</span></code> and <code class="docutils literal"><span class="pre">listitems</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#tests">Tests</a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#refactoring-of-future-standard-library-future-backports">Refactoring of <code class="docutils literal"><span class="pre">future.standard_library.*</span></code> -> <code class="docutils literal"><span class="pre">future.backports</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#backported-http-server-and-urllib-modules">Backported <code class="docutils literal"><span class="pre">http.server</span></code> and <code class="docutils literal"><span class="pre">urllib</span></code> modules</a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#internal-refactoring">Internal refactoring</a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#id1">Bug fixes</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#changes-in-version-0-11-4-2014-05-25">Changes in version 0.11.4 (2014-05-25)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#changes-in-version-0-11-3-2014-02-27">Changes in version 0.11.3 (2014-02-27)</a><ul>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#improved-compatibility-with-requests">Improved compatibility with <code class="docutils literal"><span class="pre">requests</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#conversion-scripts-explicitly-install-import-hooks">Conversion scripts explicitly install import hooks</a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#futurize-script-no-longer-adds-unicode-literals-by-default"><code class="docutils literal"><span class="pre">futurize</span></code> script no longer adds <code class="docutils literal"><span class="pre">unicode_literals</span></code> by default</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#changes-in-version-0-11-2014-01-28">Changes in version 0.11 (2014-01-28)</a><ul>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#past-package"><code class="docutils literal"><span class="pre">past</span></code> package</a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#auto-translation-of-python-2-modules-upon-import">Auto-translation of Python 2 modules upon import</a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#separate-pasteurize-script">Separate <code class="docutils literal"><span class="pre">pasteurize</span></code> script</a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#pow">pow()</a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#input-no-longer-disabled-globally-on-py2">input() no longer disabled globally on Py2</a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#deprecated-feature-auto-installation-of-standard-library-import-hooks">Deprecated feature: auto-installation of standard-library import hooks</a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#internal-changes">Internal changes</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#changes-in-version-0-10-2-2014-01-11">Changes in version 0.10.2 (2014-01-11)</a><ul>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#new-context-manager-interface-to-standard-library-hooks">New context-manager interface to standard_library hooks</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#changes-in-version-0-10-0-2013-12-02">Changes in version 0.10.0 (2013-12-02)</a><ul>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#backported-dict-type">Backported <code class="docutils literal"><span class="pre">dict</span></code> type</a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#utility-functions-raise-and-exec">Utility functions <code class="docutils literal"><span class="pre">raise_</span></code> and <code class="docutils literal"><span class="pre">exec_</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#bugfixes">Bugfixes</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#changes-in-version-0-9-2013-11-06">Changes in version 0.9 (2013-11-06)</a><ul>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#isinstance-checks-are-supported-natively-with-backported-types"><code class="docutils literal"><span class="pre">isinstance</span></code> checks are supported natively with backported types</a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#futurize-minimal-imports-by-default"><code class="docutils literal"><span class="pre">futurize</span></code>: minimal imports by default</a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#looser-type-checking-for-the-backported-str-object">Looser type-checking for the backported <code class="docutils literal"><span class="pre">str</span></code> object</a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#suspend-hooks-context-manager-added-to-future-standard-library">suspend_hooks() context manager added to <code class="docutils literal"><span class="pre">future.standard_library</span></code></a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#changes-in-version-0-8-2013-10-28">Changes in version 0.8 (2013-10-28)</a><ul>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#python-2-6-support">Python 2.6 support</a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#unused-modules-removed">Unused modules removed</a></li>
<li class="toctree-l3"><a class="reference internal" href="changelog.html#isinstance-added-to-future-builtins-v0-8-2">isinstance() added to <code class="docutils literal"><span class="pre">future.builtins</span></code> (v0.8.2)</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#summary-of-all-changes">Summary of all changes</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="credits.html">Licensing and credits</a><ul>
<li class="toctree-l2"><a class="reference internal" href="credits.html#licence">Licence</a></li>
<li class="toctree-l2"><a class="reference internal" href="credits.html#sponsor">Sponsor</a></li>
<li class="toctree-l2"><a class="reference internal" href="credits.html#authors">Authors</a><ul>
<li class="toctree-l3"><a class="reference internal" href="credits.html#development-lead">Development Lead</a></li>
<li class="toctree-l3"><a class="reference internal" href="credits.html#patches">Patches</a></li>
<li class="toctree-l3"><a class="reference internal" href="credits.html#suggestions-and-feedback">Suggestions and Feedback</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="credits.html#other-credits">Other Credits</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="reference.html">API Reference (in progress)</a><ul>
<li class="toctree-l2"><a class="reference internal" href="reference.html#module-future.builtins">future.builtins Interface</a></li>
<li class="toctree-l2"><a class="reference internal" href="reference.html#module-future.types">Backported types from Python 3</a><ul>
<li class="toctree-l3"><a class="reference internal" href="reference.html#for-more-information">For more information:</a></li>
<li class="toctree-l3"><a class="reference internal" href="reference.html#range">range()</a></li>
<li class="toctree-l3"><a class="reference internal" href="reference.html#super">super()</a></li>
<li class="toctree-l3"><a class="reference internal" href="reference.html#round">round()</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="reference.html#module-future.standard_library">future.standard_library Interface</a><ul>
<li class="toctree-l3"><a class="reference internal" href="reference.html#limitations">Limitations</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="reference.html#module-future.utils">future.utils Interface</a></li>
<li class="toctree-l2"><a class="reference internal" href="reference.html#module-past.builtins">past.builtins Interface</a></li>
<li class="toctree-l2"><a class="reference internal" href="reference.html#module-past.types">Forward-ported types from Python 2</a></li>
</ul>
</li>
</ul>
</ul>
</li>
<li class="dropdown">
<a role="button"
id="dLabelLocalToc"
data-toggle="dropdown"
data-target="#"
href="#">Page <b class="caret"></b></a>
<ul class="dropdown-menu localtoc"
role="menu"
aria-labelledby="dLabelLocalToc"><ul>
<li><a class="reference internal" href="#">Automatic conversion to Py2/3</a><ul>
<li><a class="reference internal" href="#futurize-py2-to-py2-3"><code class="docutils literal"><span class="pre">futurize</span></code>: Py2 to Py2/3</a><ul>
<li><a class="reference internal" href="#stage-1-safe-fixes">Stage 1: “safe” fixes</a></li>
<li><a class="reference internal" href="#stage-2-py3-style-code-with-wrappers-for-py2">Stage 2: Py3-style code with wrappers for Py2</a></li>
<li><a class="reference internal" href="#separating-text-from-bytes">Separating text from bytes</a></li>
<li><a class="reference internal" href="#post-conversion">Post-conversion</a></li>
</ul>
</li>
<li><a class="reference internal" href="#futurize-quick-start-guide"><code class="docutils literal"><span class="pre">futurize</span></code> quick-start guide</a><ul>
<li><a class="reference internal" href="#step-0-setup">Step 0: setup</a></li>
<li><a class="reference internal" href="#step-1-modern-py2-code">Step 1: modern Py2 code</a><ul>
<li><a class="reference internal" href="#example-error">Example error</a></li>
</ul>
</li>
<li><a class="reference internal" href="#step-2-working-py3-code-that-still-supports-py2">Step 2: working Py3 code that still supports Py2</a></li>
</ul>
</li>
<li><a class="reference internal" href="#pasteurize-py3-to-py2-3"><code class="docutils literal"><span class="pre">pasteurize</span></code>: Py3 to Py2/3</a></li>
<li><a class="reference internal" href="#known-limitations">Known limitations</a></li>
</ul>
</li>
</ul>
</ul>
</li>
</ul>
<form class="navbar-form navbar-right" action="search.html" method="get">
<div class="form-group">
<input type="text" name="q" class="form-control" placeholder="Search" />
</div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-3">
<div id="sidebar" class="bs-sidenav" role="complementary"><!--<h3>Python-Future</h3>
<p>-->
<h4>Easy, clean, reliable Python 2/3 compatibility</h4>
<a href="http://python-future.org">Table of Contents</a>
<!--
</p>
<h3>Other Formats</h3>
<p>
You can download the documentation in other formats as well:
</p>
<ul>
<li><a href="http://jinja.pocoo.org/docs/jinja-docs.pdf">as PDF</a>
<li><a href="http://jinja.pocoo.org/docs/jinja-docs.zip">as zipped HTML</a>
</ul>
-->
<!--<h3>Useful Links</h3>
<ul>
<li><a href="http://pypi.python.org/pypi/future">on PyPI</a></li>
<li><a href="https://github.com/PythonCharmers/python-future">on GitHub</a></li>
</ul>
--><ul class="current">
<li class="toctree-l1"><a class="reference internal" href="whatsnew.html">What’s New</a></li>
<li class="toctree-l1"><a class="reference internal" href="overview.html">Overview: Easy, clean, reliable Python 2/3 compatibility</a></li>
<li class="toctree-l1"><a class="reference internal" href="quickstart.html">Quick-start guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="compatible_idioms.html">Cheat Sheet: Writing Python 2-3 compatible code</a></li>
<li class="toctree-l1"><a class="reference internal" href="imports.html">Imports</a></li>
<li class="toctree-l1"><a class="reference internal" href="what_else.html">What else you need to know</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="">Automatic conversion to Py2/3</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#futurize-py2-to-py2-3"><code class="docutils literal"><span class="pre">futurize</span></code>: Py2 to Py2/3</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#stage-1-safe-fixes">Stage 1: “safe” fixes</a></li>
<li class="toctree-l3"><a class="reference internal" href="#stage-2-py3-style-code-with-wrappers-for-py2">Stage 2: Py3-style code with wrappers for Py2</a></li>
<li class="toctree-l3"><a class="reference internal" href="#separating-text-from-bytes">Separating text from bytes</a></li>
<li class="toctree-l3"><a class="reference internal" href="#post-conversion">Post-conversion</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#futurize-quick-start-guide"><code class="docutils literal"><span class="pre">futurize</span></code> quick-start guide</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#step-0-setup">Step 0: setup</a></li>
<li class="toctree-l3"><a class="reference internal" href="#step-1-modern-py2-code">Step 1: modern Py2 code</a></li>
<li class="toctree-l3"><a class="reference internal" href="#step-2-working-py3-code-that-still-supports-py2">Step 2: working Py3 code that still supports Py2</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#pasteurize-py3-to-py2-3"><code class="docutils literal"><span class="pre">pasteurize</span></code>: Py3 to Py2/3</a></li>
<li class="toctree-l2"><a class="reference internal" href="#known-limitations">Known limitations</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="faq.html">Frequently Asked Questions (FAQ)</a></li>
<li class="toctree-l1"><a class="reference internal" href="stdlib_incompatibilities.html">Standard library incompatibilities</a></li>
<li class="toctree-l1"><a class="reference internal" href="older_interfaces.html">Older interfaces</a></li>
<li class="toctree-l1"><a class="reference internal" href="changelog.html">Changes in previous versions</a></li>
<li class="toctree-l1"><a class="reference internal" href="credits.html">Licensing and credits</a></li>
<li class="toctree-l1"><a class="reference internal" href="reference.html">API Reference (in progress)</a></li>
</ul>
</div>
</div>
<div class="col-md-9">
<div class="section" id="automatic-conversion-to-py2-3">
<span id="automatic-conversion"></span><h1>Automatic conversion to Py2/3<a class="headerlink" href="#automatic-conversion-to-py2-3" title="Permalink to this headline">¶</a></h1>
<p>The <code class="docutils literal"><span class="pre">future</span></code> source tree includes scripts called <code class="docutils literal"><span class="pre">futurize</span></code> and
<code class="docutils literal"><span class="pre">pasteurize</span></code> to aid in making Python 2 code or Python 3 code compatible with
both platforms (Py2/3) using the <code class="xref py py-mod docutils literal"><span class="pre">future</span></code> module. These are based on
<code class="docutils literal"><span class="pre">lib2to3</span></code> and use fixers from <code class="docutils literal"><span class="pre">2to3</span></code>, <code class="docutils literal"><span class="pre">3to2</span></code>, and <code class="docutils literal"><span class="pre">python-modernize</span></code>.</p>
<p><code class="docutils literal"><span class="pre">futurize</span></code> passes Python 2 code through all the appropriate fixers to turn it
into valid Python 3 code, and then adds <code class="docutils literal"><span class="pre">__future__</span></code> and <code class="docutils literal"><span class="pre">future</span></code> package
imports.</p>
<p>For conversions from Python 3 code to Py2/3, use the <code class="docutils literal"><span class="pre">pasteurize</span></code> script
instead. This converts Py3-only constructs (e.g. new metaclass syntax) and adds
<code class="docutils literal"><span class="pre">__future__</span></code> and <code class="docutils literal"><span class="pre">future</span></code> imports to the top of each module.</p>
<p>In both cases, the result should be relatively clean Py3-style code that runs
mostly unchanged on both Python 2 and Python 3.</p>
<div class="section" id="futurize-py2-to-py2-3">
<span id="forwards-conversion"></span><h2><code class="docutils literal"><span class="pre">futurize</span></code>: Py2 to Py2/3<a class="headerlink" href="#futurize-py2-to-py2-3" title="Permalink to this headline">¶</a></h2>
<p>The <code class="docutils literal"><span class="pre">futurize</span></code> script passes Python 2 code through all the appropriate fixers
to turn it into valid Python 3 code, and then adds <code class="docutils literal"><span class="pre">__future__</span></code> and
<code class="docutils literal"><span class="pre">future</span></code> package imports to re-enable compatibility with Python 2.</p>
<p>For example, running <code class="docutils literal"><span class="pre">futurize</span></code> turns this Python 2 code:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">ConfigParser</span> <span class="c"># Py2 module name</span>
<span class="k">class</span> <span class="nc">Upper</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">iterable</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_iter</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="n">iterable</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">next</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="c"># Py2-style iterator interface</span>
<span class="k">return</span> <span class="nb">next</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_iter</span><span class="p">)</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span>
<span class="n">itr</span> <span class="o">=</span> <span class="n">Upper</span><span class="p">(</span><span class="s">'hello'</span><span class="p">)</span>
<span class="k">print</span> <span class="nb">next</span><span class="p">(</span><span class="n">itr</span><span class="p">),</span>
<span class="k">for</span> <span class="n">letter</span> <span class="ow">in</span> <span class="n">itr</span><span class="p">:</span>
<span class="k">print</span> <span class="n">letter</span><span class="p">,</span> <span class="c"># Py2-style print statement</span>
</pre></div>
</div>
<p>into this code which runs on both Py2 and Py3:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
<span class="kn">from</span> <span class="nn">future</span> <span class="kn">import</span> <span class="n">standard_library</span>
<span class="n">standard_library</span><span class="o">.</span><span class="n">install_aliases</span><span class="p">()</span>
<span class="kn">from</span> <span class="nn">future.builtins</span> <span class="kn">import</span> <span class="nb">next</span>
<span class="kn">from</span> <span class="nn">future.builtins</span> <span class="kn">import</span> <span class="nb">object</span>
<span class="kn">import</span> <span class="nn">configparser</span> <span class="c"># Py3-style import</span>
<span class="k">class</span> <span class="nc">Upper</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">iterable</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_iter</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="n">iterable</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">__next__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="c"># Py3-style iterator interface</span>
<span class="k">return</span> <span class="nb">next</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_iter</span><span class="p">)</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span>
<span class="n">itr</span> <span class="o">=</span> <span class="n">Upper</span><span class="p">(</span><span class="s">'hello'</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="nb">next</span><span class="p">(</span><span class="n">itr</span><span class="p">),</span> <span class="n">end</span><span class="o">=</span><span class="s">' '</span><span class="p">)</span> <span class="c"># Py3-style print function</span>
<span class="k">for</span> <span class="n">letter</span> <span class="ow">in</span> <span class="n">itr</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="n">letter</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s">' '</span><span class="p">)</span>
</pre></div>
</div>
<p>To write out all the changes to your Python files that <code class="docutils literal"><span class="pre">futurize</span></code> suggests,
use the <code class="docutils literal"><span class="pre">-w</span></code> flag.</p>
<p>For complex projects, it is probably best to divide the porting into two stages.
Stage 1 is for “safe” changes that modernize the code but do not break Python
2.6 compatibility or introduce a depdendency on the <code class="docutils literal"><span class="pre">future</span></code> package. Stage 2
is to complete the process.</p>
<div class="section" id="stage-1-safe-fixes">
<span id="forwards-conversion-stage1"></span><h3>Stage 1: “safe” fixes<a class="headerlink" href="#stage-1-safe-fixes" title="Permalink to this headline">¶</a></h3>
<p>Run the first stage of the conversion process with:</p>
<div class="highlight-python"><div class="highlight"><pre>futurize --stage1 mypackage/*.py
</pre></div>
</div>
<p>or, if you are using zsh, recursively:</p>
<div class="highlight-python"><div class="highlight"><pre>futurize --stage1 mypackage/**/*.py
</pre></div>
</div>
<p>This applies fixes that modernize Python 2 code without changing the effect of
the code. With luck, this will not introduce any bugs into the code, or will at
least be trivial to fix. The changes are those that bring the Python code
up-to-date without breaking Py2 compatibility. The resulting code will be
modern Python 2.6-compatible code plus <code class="docutils literal"><span class="pre">__future__</span></code> imports from the
following set:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">absolute_import</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">division</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
</pre></div>
</div>
<p>Only those <code class="docutils literal"><span class="pre">__future__</span></code> imports deemed necessary will be added unless
the <code class="docutils literal"><span class="pre">--all-imports</span></code> command-line option is passed to <code class="docutils literal"><span class="pre">futurize</span></code>, in
which case they are all added.</p>
<p>The <code class="docutils literal"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">unicode_literals</span></code> declaration is not added
unless the <code class="docutils literal"><span class="pre">--unicode-literals</span></code> flag is passed to <code class="docutils literal"><span class="pre">futurize</span></code>.</p>
<p>The changes include:</p>
<div class="highlight-python"><div class="highlight"><pre>- except MyException, e:
+ except MyException as e:
- print >>stderr, "Blah"
+ from __future__ import print_function
+ print("Blah", stderr)
- class MyClass:
+ class MyClass(object):
- def next(self):
+ def __next__(self):
- if d.has_key(key):
+ if key in d:
</pre></div>
</div>
<p>Implicit relative imports fixed, e.g.:</p>
<div class="highlight-python"><div class="highlight"><pre>- import mymodule
+ from __future__ import absolute_import
+ from . import mymodule
</pre></div>
</div>
<p>Stage 1 does not add any imports from the <code class="docutils literal"><span class="pre">future</span></code> package. The output of
stage 1 will probably not (yet) run on Python 3.</p>
<p>The goal for this stage is to create most of the <code class="docutils literal"><span class="pre">diff</span></code> for the entire
porting process, but without introducing any bugs. It should be uncontroversial
and safe to apply to every Python 2 package. The subsequent patches introducing
Python 3 compatibility should then be shorter and easier to review.</p>
<p>The complete set of fixers applied by <code class="docutils literal"><span class="pre">futurize</span> <span class="pre">--stage1</span></code> is:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_apply</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_except</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_exitfunc</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_funcattrs</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_has_key</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_idioms</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_intern</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_isinstance</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_methodattrs</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_ne</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_numliterals</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_paren</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_reduce</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_renames</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_repr</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_standarderror</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_sys_exc</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_throw</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_tuple_params</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_types</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_ws_comma</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_xreadlines</span>
<span class="n">libfuturize</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_absolute_import</span>
<span class="n">libfuturize</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_next_call</span>
<span class="n">libfuturize</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_print_with_import</span>
<span class="n">libfuturize</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_raise</span>
</pre></div>
</div>
<p>The following fixers from <code class="docutils literal"><span class="pre">lib2to3</span></code> are not applied:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_import</span>
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">fix_absolute_import</span></code> fixer in <code class="docutils literal"><span class="pre">libfuturize.fixes</span></code> is applied instead of
<code class="docutils literal"><span class="pre">lib2to3.fixes.fix_import</span></code>. The new fixer both makes implicit relative
imports explicit and adds the declaration <code class="docutils literal"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span>
<span class="pre">absolute_import</span></code> at the top of each relevant module.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_next</span>
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">fix_next_call</span></code> fixer in <code class="docutils literal"><span class="pre">libfuturize.fixes</span></code> is applied instead of
<code class="docutils literal"><span class="pre">fix_next</span></code> in stage 1. The new fixer changes any <code class="docutils literal"><span class="pre">obj.next()</span></code> calls to
<code class="docutils literal"><span class="pre">next(obj)</span></code>, which is Py2/3 compatible, but doesn’t change any <code class="docutils literal"><span class="pre">next</span></code> method
names to <code class="docutils literal"><span class="pre">__next__</span></code>, which would break Py2 compatibility.</p>
<p><code class="docutils literal"><span class="pre">fix_next</span></code> is applied in stage 2.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_print</span>
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">fix_print_with_import</span></code> fixer in <code class="docutils literal"><span class="pre">libfuturize.fixes</span></code> changes the code to
use print as a function and also adds <code class="docutils literal"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span>
<span class="pre">print_function</span></code> to the top of modules using <code class="docutils literal"><span class="pre">print()</span></code>.</p>
<p>In addition, it avoids adding an extra set of parentheses if these already
exist. So <code class="docutils literal"><span class="pre">print(x)</span></code> does not become <code class="docutils literal"><span class="pre">print((x))</span></code>.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_raise</span>
</pre></div>
</div>
<p>This fixer translates code to use the Python 3-only <code class="docutils literal"><span class="pre">with_traceback()</span></code>
method on exceptions.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_set_literal</span>
</pre></div>
</div>
<p>This converts <code class="docutils literal"><span class="pre">set([1,</span> <span class="pre">2,</span> <span class="pre">3]</span></code>) to <code class="docutils literal"><span class="pre">{1,</span> <span class="pre">2,</span> <span class="pre">3}</span></code>, breaking Python 2.6 support.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_ws_comma</span>
</pre></div>
</div>
<p>This performs cosmetic changes. This is not applied by default because it
does not serve to improve Python 2/3 compatibility. (In some cases it may
also reduce readability: see issue #58.)</p>
</div>
<div class="section" id="stage-2-py3-style-code-with-wrappers-for-py2">
<span id="forwards-conversion-stage2"></span><h3>Stage 2: Py3-style code with wrappers for Py2<a class="headerlink" href="#stage-2-py3-style-code-with-wrappers-for-py2" title="Permalink to this headline">¶</a></h3>
<p>Run stage 2 of the conversion process with:</p>
<div class="highlight-python"><div class="highlight"><pre>futurize --stage2 myfolder/*.py
</pre></div>
</div>
<p>This stage adds a dependency on the <code class="docutils literal"><span class="pre">future</span></code> package. The goal for stage 2 is
to make further mostly safe changes to the Python 2 code to use Python 3-style
code that then still runs on Python 2 with the help of the appropriate builtins
and utilities in <code class="docutils literal"><span class="pre">future</span></code>.</p>
<p>For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">name</span> <span class="o">=</span> <span class="nb">raw_input</span><span class="p">(</span><span class="s">'What is your name?</span><span class="se">\n</span><span class="s">'</span><span class="p">)</span>
<span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">d</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
<span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="nb">basestring</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">MyClass</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__unicode__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="s">u'My object'</span>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">unicode</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">'utf-8'</span><span class="p">)</span>
</pre></div>
</div>
<p>would be converted by Stage 2 to this code:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">builtins</span> <span class="kn">import</span> <span class="nb">input</span>
<span class="kn">from</span> <span class="nn">builtins</span> <span class="kn">import</span> <span class="nb">str</span>
<span class="kn">from</span> <span class="nn">future.utils</span> <span class="kn">import</span> <span class="n">iteritems</span><span class="p">,</span> <span class="n">python_2_unicode_compatible</span>
<span class="n">name</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s">'What is your name?</span><span class="se">\n</span><span class="s">'</span><span class="p">)</span>
<span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">iteritems</span><span class="p">(</span><span class="n">d</span><span class="p">):</span>
<span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="p">(</span><span class="nb">str</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">))</span>
<span class="nd">@python_2_unicode_compatible</span>
<span class="k">class</span> <span class="nc">MyClass</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="s">u'My object'</span>
</pre></div>
</div>
<p>Stage 2 also renames standard-library imports to their Py3 names and adds these
two lines:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">future</span> <span class="kn">import</span> <span class="n">standard_library</span>
<span class="n">standard_library</span><span class="o">.</span><span class="n">install_aliases</span><span class="p">()</span>
</pre></div>
</div>
<p>For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">ConfigParser</span>
</pre></div>
</div>
<p>becomes:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">future</span> <span class="kn">import</span> <span class="n">standard_library</span>
<span class="n">standard_library</span><span class="o">.</span><span class="n">install_aliases</span><span class="p">()</span>
<span class="kn">import</span> <span class="nn">configparser</span>
</pre></div>
</div>
<p>The complete list of fixers applied in Stage 2 is:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_basestring</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_dict</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_exec</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_getcwdu</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_input</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_itertools</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_itertools_imports</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_filter</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_long</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_map</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_nonzero</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_operator</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_raw_input</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_zip</span>
<span class="n">libfuturize</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_cmp</span>
<span class="n">libfuturize</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_division</span>
<span class="n">libfuturize</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_execfile</span>
<span class="n">libfuturize</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_future_builtins</span>
<span class="n">libfuturize</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_future_standard_library</span>
<span class="n">libfuturize</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_future_standard_library_urllib</span>
<span class="n">libfuturize</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_metaclass</span>
<span class="n">libpasteurize</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_newstyle</span>
<span class="n">libfuturize</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_object</span>
<span class="n">libfuturize</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_unicode_keep_u</span>
<span class="n">libfuturize</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_xrange_with_import</span>
</pre></div>
</div>
<p>Not applied:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_buffer</span> <span class="c"># Perhaps not safe. Test this.</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_callable</span> <span class="c"># Not needed in Py3.2+</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_execfile</span> <span class="c"># Some problems: see issue #37.</span>
<span class="c"># We use the custom libfuturize.fixes.fix_execfile instead.</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_future</span> <span class="c"># Removing __future__ imports is bad for Py2 compatibility!</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_imports</span> <span class="c"># Called by libfuturize.fixes.fix_future_standard_library</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_imports2</span> <span class="c"># We don't handle this yet (dbm)</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_metaclass</span> <span class="c"># Causes SyntaxError in Py2! Use the one from ``six`` instead</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_unicode</span> <span class="c"># Strips off the u'' prefix, which removes a potentially</span>
<span class="c"># helpful source of information for disambiguating</span>
<span class="c"># unicode/byte strings.</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_urllib</span> <span class="c"># Included in libfuturize.fix_future_standard_library_urllib</span>
<span class="n">lib2to3</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_xrange</span> <span class="c"># Custom one because of a bug with Py3.3's lib2to3</span>
</pre></div>
</div>
<p>Fixes applied with the <code class="docutils literal"><span class="pre">futurize</span> <span class="pre">--conservative</span></code> option:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">libfuturize</span><span class="o">.</span><span class="n">fixes</span><span class="o">.</span><span class="n">fix_division_safe</span> <span class="c"># instead of libfuturize.fixes.fix_division.</span>
</pre></div>
</div>
</div>
<div class="section" id="separating-text-from-bytes">
<span id="forwards-conversion-text"></span><h3>Separating text from bytes<a class="headerlink" href="#separating-text-from-bytes" title="Permalink to this headline">¶</a></h3>
<p>After applying stage 2, the recommended step is to decide which of your Python
2 strings represent text and which represent binary data and to prefix all
string literals with either <code class="docutils literal"><span class="pre">b</span></code> or <code class="docutils literal"><span class="pre">u</span></code> accordingly. Furthermore, to ensure
that these types behave similarly on Python 2 as on Python 3, also wrap
byte-strings or text in the <code class="docutils literal"><span class="pre">bytes</span></code> and <code class="docutils literal"><span class="pre">str</span></code> types from <code class="docutils literal"><span class="pre">future</span></code>. For
example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">builtins</span> <span class="kn">import</span> <span class="nb">bytes</span><span class="p">,</span> <span class="nb">str</span>
<span class="n">b</span> <span class="o">=</span> <span class="nb">bytes</span><span class="p">(</span><span class="n">b</span><span class="s">'</span><span class="se">\x00</span><span class="s">ABCD'</span><span class="p">)</span>
<span class="n">s</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="s">u'This is normal text'</span><span class="p">)</span>
</pre></div>
</div>
<p>Any unadorned string literals will then represent native platform strings
(byte-strings on Py2, unicode strings on Py3).</p>
<p>An alternative is to pass the <code class="docutils literal"><span class="pre">--unicode-literals</span></code> flag:</p>
<div class="highlight-python"><div class="highlight"><pre>$ futurize --unicode-literals mypython2script.py
</pre></div>
</div>
<p>After running this, all string literals that were not explicitly marked up as
<code class="docutils literal"><span class="pre">b''</span></code> will mean text (Python 3 <code class="docutils literal"><span class="pre">str</span></code> or Python 2 <code class="docutils literal"><span class="pre">unicode</span></code>).</p>
</div>
<div class="section" id="post-conversion">
<span id="forwards-conversion-stage3"></span><h3>Post-conversion<a class="headerlink" href="#post-conversion" title="Permalink to this headline">¶</a></h3>
<p>After running <code class="docutils literal"><span class="pre">futurize</span></code>, we recommend first running your tests on Python 3 and making further code changes until they pass on Python 3.</p>
<p>The next step would be manually tweaking the code to re-enable Python 2
compatibility with the help of the <code class="docutils literal"><span class="pre">future</span></code> package. For example, you can add
the <code class="docutils literal"><span class="pre">@python_2_unicode_compatible</span></code> decorator to any classes that define custom
<code class="docutils literal"><span class="pre">__str__</span></code> methods. See <a class="reference internal" href="what_else.html#what-else"><span>What else you need to know</span></a> for more info.</p>
</div>
</div>
<div class="section" id="futurize-quick-start-guide">
<span id="futurize-cheatsheet"></span><h2><code class="docutils literal"><span class="pre">futurize</span></code> quick-start guide<a class="headerlink" href="#futurize-quick-start-guide" title="Permalink to this headline">¶</a></h2>
<p>How to convert Py2 code to Py2/3 code using <code class="docutils literal"><span class="pre">futurize</span></code>:</p>
<div class="section" id="step-0-setup">
<span id="porting-setup"></span><h3>Step 0: setup<a class="headerlink" href="#step-0-setup" title="Permalink to this headline">¶</a></h3>
<p>Step 0 goal: set up and see the tests passing on Python 2 and failing on Python 3.</p>
<ol class="loweralpha">
<li><p class="first">Clone the package from github/bitbucket. Optionally rename your repo to <code class="docutils literal"><span class="pre">package-future</span></code>. Examples: <code class="docutils literal"><span class="pre">reportlab-future</span></code>, <code class="docutils literal"><span class="pre">paramiko-future</span></code>, <code class="docutils literal"><span class="pre">mezzanine-future</span></code>.</p>
</li>
<li><p class="first">Create and activate a Python 2 conda environment or virtualenv. Install the package with <code class="docutils literal"><span class="pre">python</span> <span class="pre">setup.py</span> <span class="pre">install</span></code> and run its test suite on Py2.7 or Py2.6 (e.g. <code class="docutils literal"><span class="pre">python</span> <span class="pre">setup.py</span> <span class="pre">test</span></code> or <code class="docutils literal"><span class="pre">py.test</span></code> or <code class="docutils literal"><span class="pre">nosetests</span></code>)</p>
</li>
<li><p class="first">Optionally: if there is a <code class="docutils literal"><span class="pre">.travis.yml</span></code> file, add Python version 3.3 and remove any versions < 2.6.</p>
</li>
<li><p class="first">Install Python 3.3 with e.g. <code class="docutils literal"><span class="pre">sudo</span> <span class="pre">apt-get</span> <span class="pre">install</span> <span class="pre">python3</span></code>. On other platforms, an easy way is to use <a class="reference external" href="http://repo.continuum.io/miniconda/index.html">Miniconda</a>. Then e.g.:</p>
<div class="highlight-python"><div class="highlight"><pre>conda create -n py33 python=3.3 pip
</pre></div>
</div>
</li>
</ol>
</div>
<div class="section" id="step-1-modern-py2-code">
<span id="porting-step1"></span><h3>Step 1: modern Py2 code<a class="headerlink" href="#step-1-modern-py2-code" title="Permalink to this headline">¶</a></h3>
<p>The goal for this step is to modernize the Python 2 code without introducing any dependencies (on <code class="docutils literal"><span class="pre">future</span></code> or e.g. <code class="docutils literal"><span class="pre">six</span></code>) at this stage.</p>
<p><strong>1a</strong>. Install <code class="docutils literal"><span class="pre">future</span></code> into the virtualenv using:</p>
<div class="highlight-python"><div class="highlight"><pre>pip install future
</pre></div>
</div>
<p><strong>1b</strong>. Run <code class="docutils literal"><span class="pre">futurize</span> <span class="pre">--stage1</span> <span class="pre">-w</span> <span class="pre">*.py</span> <span class="pre">subdir1/*.py</span> <span class="pre">subdir2/*.py</span></code>. Note that with
recursive globbing in <code class="docutils literal"><span class="pre">bash</span></code> or <code class="docutils literal"><span class="pre">zsh</span></code>, you can apply stage 1 to all Python
source files recursively with:</p>
<div class="highlight-python"><div class="highlight"><pre>futurize --stage1 -w **/*.py
</pre></div>
</div>
<p><strong>1c</strong>. Commit all changes</p>
<p><strong>1d</strong>. Re-run the test suite on Py2 and fix any errors.</p>
<p>See <a class="reference internal" href="futurize.html#forwards-conversion-stage1"><span>Stage 1: “safe” fixes</span></a> for more info.</p>
<div class="section" id="example-error">
<h4>Example error<a class="headerlink" href="#example-error" title="Permalink to this headline">¶</a></h4>
<p>One relatively common error after conversion is:</p>
<div class="highlight-python"><div class="highlight"><pre>Traceback (most recent call last):
...
File "/home/user/Install/BleedingEdge/reportlab/tests/test_encrypt.py", line 19, in <module>
from .test_pdfencryption import parsedoc
ValueError: Attempted relative import in non-package
</pre></div>
</div>
<p>If you get this error, try adding an empty <code class="docutils literal"><span class="pre">__init__.py</span></code> file in the package
directory. (In this example, in the tests/ directory.) If this doesn’t help,
and if this message appears for all tests, they must be invoked differently
(from the cmd line or e.g. <code class="docutils literal"><span class="pre">setup.py</span></code>). The way to run a module inside a
package on Python 3, or on Python 2 with <code class="docutils literal"><span class="pre">absolute_import</span></code> in effect, is:</p>
<div class="highlight-python"><div class="highlight"><pre>python -m tests.test_platypus_xref
</pre></div>
</div>
<p>(For more info, see <a class="reference external" href="http://www.python.org/dev/peps/pep-0328/">PEP 328</a> and
the <a class="reference external" href="http://www.python.org/dev/peps/pep-0008/">PEP 8</a> section on absolute
imports.)</p>
</div>
</div>
<div class="section" id="step-2-working-py3-code-that-still-supports-py2">
<span id="porting-step2"></span><h3>Step 2: working Py3 code that still supports Py2<a class="headerlink" href="#step-2-working-py3-code-that-still-supports-py2" title="Permalink to this headline">¶</a></h3>
<p>The goal for this step is to get the tests passing first on Py3 and then on Py2
again with the help of the <code class="docutils literal"><span class="pre">future</span></code> package.</p>
<p><strong>2a</strong>. Run:</p>
<div class="highlight-python"><div class="highlight"><pre>futurize --stage2 myfolder1/*.py myfolder2/*.py
</pre></div>
</div>
<p>Or, using recursive globbing with <code class="docutils literal"><span class="pre">bash</span></code> or <code class="docutils literal"><span class="pre">zsh</span></code>, you can view the stage 2
changes to all Python source files recursively with:</p>
<div class="highlight-python"><div class="highlight"><pre>futurize --stage2 **/*.py
</pre></div>
</div>
<p>To apply the changes, add the <code class="docutils literal"><span class="pre">-w</span></code> argument.</p>
<p>This stage makes further conversions needed to support both Python 2 and 3.
These will likely require imports from <code class="docutils literal"><span class="pre">future</span></code> on Py2 (and sometimes on Py3),
such as:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">future</span> <span class="kn">import</span> <span class="n">standard_library</span>
<span class="n">standard_library</span><span class="o">.</span><span class="n">install_aliases</span><span class="p">()</span>
<span class="c"># ...</span>
<span class="kn">from</span> <span class="nn">builtins</span> <span class="kn">import</span> <span class="nb">bytes</span>
<span class="kn">from</span> <span class="nn">builtins</span> <span class="kn">import</span> <span class="nb">open</span>
<span class="kn">from</span> <span class="nn">future.utils</span> <span class="kn">import</span> <span class="n">with_metaclass</span>
</pre></div>
</div>
<p>Optionally, you can use the <code class="docutils literal"><span class="pre">--unicode-literals</span></code> flag to add this import to
the top of each module:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">unicode_literals</span>
</pre></div>
</div>
<p>All strings in the module would then be unicode on Py2 (as on Py3) unless
explicitly marked with a <code class="docutils literal"><span class="pre">b''</span></code> prefix.</p>
<p>If you would like <code class="docutils literal"><span class="pre">futurize</span></code> to import all the changed builtins to have their
Python 3 semantics on Python 2, invoke it like this:</p>
<div class="highlight-python"><div class="highlight"><pre>futurize --stage2 --all-imports myfolder/*.py
</pre></div>
</div>
<p><strong>2b</strong>. Re-run your tests on Py3 now. Make changes until your tests pass on Python 3.</p>
<p><strong>2c</strong>. Commit your changes! :)</p>
<p><strong>2d</strong>. Now run your tests on Python 2 and notice the errors. Add wrappers from
<code class="docutils literal"><span class="pre">future</span></code> to re-enable Python 2 compatibility. See the
<a class="reference internal" href="compatible_idioms.html#compatible-idioms"><span>Cheat Sheet: Writing Python 2-3 compatible code</span></a> cheat sheet and <a class="reference internal" href="what_else.html#what-else"><span>What else you need to know</span></a> for more info.</p>
<p>After each change, re-run the tests on Py3 and Py2 to ensure they pass on both.</p>
<p><strong>2e</strong>. You’re done! Celebrate! Push your code and announce to the world! Hashtags
#python3 #python-future.</p>
</div>
</div>
<div class="section" id="pasteurize-py3-to-py2-3">
<span id="backwards-conversion"></span><h2><code class="docutils literal"><span class="pre">pasteurize</span></code>: Py3 to Py2/3<a class="headerlink" href="#pasteurize-py3-to-py2-3" title="Permalink to this headline">¶</a></h2>
<p>Running <code class="docutils literal"><span class="pre">pasteurize</span> <span class="pre">-w</span> <span class="pre">mypy3module.py</span></code> turns this Python 3 code:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">configparser</span>
<span class="k">class</span> <span class="nc">Blah</span><span class="p">:</span>
<span class="k">pass</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Hello'</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
<p>into this code which runs on both Py2 and Py3:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
<span class="kn">from</span> <span class="nn">future</span> <span class="kn">import</span> <span class="n">standard_library</span>
<span class="n">standard_library</span><span class="o">.</span><span class="n">install_hooks</span><span class="p">()</span>
<span class="kn">import</span> <span class="nn">configparser</span>
<span class="k">class</span> <span class="nc">Blah</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">pass</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Hello'</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
<p>Notice that both <code class="docutils literal"><span class="pre">futurize</span></code> and <code class="docutils literal"><span class="pre">pasteurize</span></code> create explicit new-style
classes that inherit from <code class="docutils literal"><span class="pre">object</span></code> on both Python versions, and both
refer to stdlib modules (as well as builtins) under their Py3 names.</p>
<p><code class="docutils literal"><span class="pre">pasteurize</span></code> also handles the following Python 3 features:</p>
<ul class="simple">
<li>keyword-only arguments</li>
<li>metaclasses (using <a class="reference internal" href="reference.html#future.utils.with_metaclass" title="future.utils.with_metaclass"><code class="xref py py-func docutils literal"><span class="pre">with_metaclass()</span></code></a>)</li>
<li>extended tuple unpacking (PEP 3132)</li>
</ul>
<p>To handle function annotations (PEP 3107), see <a class="reference internal" href="func_annotations.html#func-annotations"><span>Function annotations</span></a>.</p>
</div>
<div class="section" id="known-limitations">
<span id="futurize-limitations"></span><h2>Known limitations<a class="headerlink" href="#known-limitations" title="Permalink to this headline">¶</a></h2>
<p><code class="docutils literal"><span class="pre">futurize</span></code> and <code class="docutils literal"><span class="pre">pasteurize</span></code> are useful to automate much of the
work of porting, particularly the boring repetitive text substitutions. They also
help to flag which parts of the code require attention.</p>
<p>Nevertheless, <code class="docutils literal"><span class="pre">futurize</span></code> and <code class="docutils literal"><span class="pre">pasteurize</span></code> are still incomplete and make
some mistakes, like 2to3, on which they are based. Please report bugs on
<a class="reference external" href="https://github.com/PythonCharmers/python-future/">GitHub</a>. Contributions to
the <code class="docutils literal"><span class="pre">lib2to3</span></code>-based fixers for <code class="docutils literal"><span class="pre">futurize</span></code> and <code class="docutils literal"><span class="pre">pasteurize</span></code> are
particularly welcome! Please see <a class="reference internal" href="faq.html#contributing"><span>Contributing</span></a>.</p>
<p><code class="docutils literal"><span class="pre">futurize</span></code> doesn’t currently make the following change automatically:</p>
<ol class="arabic">
<li><p class="first">Strings containing <code class="docutils literal"><span class="pre">\U</span></code> produce a <code class="docutils literal"><span class="pre">SyntaxError</span></code> on Python 3. An example is:</p>
<div class="highlight-python"><div class="highlight"><pre>s = 'C:\Users'.
</pre></div>
</div>
<p>Python 2 expands this to <code class="docutils literal"><span class="pre">s</span> <span class="pre">=</span> <span class="pre">'C:\\Users'</span></code>, but Python 3 requires a raw
prefix (<code class="docutils literal"><span class="pre">r'...'</span></code>). This also applies to multi-line strings (including
multi-line docstrings).</p>
</li>
</ol>
<p>Also see the tests in <code class="docutils literal"><span class="pre">future/tests/test_futurize.py</span></code> marked
<code class="docutils literal"><span class="pre">@expectedFailure</span></code> or <code class="docutils literal"><span class="pre">@skip</span></code> for known limitations.</p>
</div>
</div>
</div>
</div>
</div>
<footer class="footer">
<div class="container">
<p class="pull-right">
<a href="#">Back to top</a>
</p>
<p>
© Copyright 2013-2015, Python Charmers Pty Ltd, Australia.<br/>
</p>
</div>
</footer>
<div class="footer">
<script type="text/javascript">
(function() {
var ga = document.createElement('script');
ga.src = ('https:' == document.location.protocol ?
'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
ga.setAttribute('async', 'true');
document.documentElement.firstChild.appendChild(ga);
})();
</script>
</div>
</body>
</html>
|