pyporting.html 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
  6. <meta property="og:title" content="How to port Python 2 Code to Python 3" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/howto/pyporting.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="author, Brett Cannon,. Abstract: Python 2 reached its official end-of-life at the start of 2020. This means that no new bug reports, fixes, or changes will be made to Python 2 - it’s no longer supp..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="author, Brett Cannon,. Abstract: Python 2 reached its official end-of-life at the start of 2020. This means that no new bug reports, fixes, or changes will be made to Python 2 - it’s no longer supp..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>How to port Python 2 Code to Python 3 &#8212; Python 3.12.0 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?digest=b37c26da2f7529d09fe70b41c4b2133fe4931a90" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css" />
  21. <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
  22. <script src="../_static/jquery.js"></script>
  23. <script src="../_static/underscore.js"></script>
  24. <script src="../_static/doctools.js"></script>
  25. <script src="../_static/sidebar.js"></script>
  26. <link rel="search" type="application/opensearchdescription+xml"
  27. title="Search within Python 3.12.0 documentation"
  28. href="../_static/opensearch.xml"/>
  29. <link rel="author" title="About these documents" href="../about.html" />
  30. <link rel="index" title="Index" href="../genindex.html" />
  31. <link rel="search" title="Search" href="../search.html" />
  32. <link rel="copyright" title="Copyright" href="../copyright.html" />
  33. <link rel="next" title="Porting Extension Modules to Python 3" href="cporting.html" />
  34. <link rel="prev" title="Python HOWTOs" href="index.html" />
  35. <link rel="canonical" href="https://docs.python.org/3/howto/pyporting.html" />
  36. <style>
  37. @media only screen {
  38. table.full-width-table {
  39. width: 100%;
  40. }
  41. }
  42. </style>
  43. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  44. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  45. <script type="text/javascript" src="../_static/copybutton.js"></script>
  46. <script type="text/javascript" src="../_static/menu.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#">How to port Python 2 Code to Python 3</a><ul>
  86. <li><a class="reference internal" href="#the-short-explanation">The Short Explanation</a></li>
  87. <li><a class="reference internal" href="#details">Details</a><ul>
  88. <li><a class="reference internal" href="#different-versions-of-python-2">Different versions of Python 2</a></li>
  89. <li><a class="reference internal" href="#make-sure-you-specify-the-proper-version-support-in-your-setup-py-file">Make sure you specify the proper version support in your <code class="docutils literal notranslate"><span class="pre">setup.py</span></code> file</a></li>
  90. <li><a class="reference internal" href="#have-good-test-coverage">Have good test coverage</a></li>
  91. <li><a class="reference internal" href="#be-aware-of-the-differences-between-python-2-and-3">Be aware of the differences between Python 2 and 3</a></li>
  92. <li><a class="reference internal" href="#update-your-code">Update your code</a><ul>
  93. <li><a class="reference internal" href="#division">Division</a></li>
  94. <li><a class="reference internal" href="#text-versus-binary-data">Text versus binary data</a></li>
  95. <li><a class="reference internal" href="#use-feature-detection-instead-of-version-detection">Use feature detection instead of version detection</a></li>
  96. </ul>
  97. </li>
  98. <li><a class="reference internal" href="#prevent-compatibility-regressions">Prevent compatibility regressions</a></li>
  99. <li><a class="reference internal" href="#check-which-dependencies-block-your-transition">Check which dependencies block your transition</a></li>
  100. <li><a class="reference internal" href="#update-your-setup-py-file-to-denote-python-3-compatibility">Update your <code class="docutils literal notranslate"><span class="pre">setup.py</span></code> file to denote Python 3 compatibility</a></li>
  101. <li><a class="reference internal" href="#use-continuous-integration-to-stay-compatible">Use continuous integration to stay compatible</a></li>
  102. <li><a class="reference internal" href="#consider-using-optional-static-type-checking">Consider using optional static type checking</a></li>
  103. </ul>
  104. </li>
  105. </ul>
  106. </li>
  107. </ul>
  108. </div>
  109. <div>
  110. <h4>Previous topic</h4>
  111. <p class="topless"><a href="index.html"
  112. title="previous chapter">Python HOWTOs</a></p>
  113. </div>
  114. <div>
  115. <h4>Next topic</h4>
  116. <p class="topless"><a href="cporting.html"
  117. title="next chapter">Porting Extension Modules to Python 3</a></p>
  118. </div>
  119. <div role="note" aria-label="source link">
  120. <h3>This Page</h3>
  121. <ul class="this-page-menu">
  122. <li><a href="../bugs.html">Report a Bug</a></li>
  123. <li>
  124. <a href="https://github.com/python/cpython/blob/main/Doc/howto/pyporting.rst"
  125. rel="nofollow">Show Source
  126. </a>
  127. </li>
  128. </ul>
  129. </div>
  130. </nav>
  131. </div>
  132. </div>
  133. <div class="related" role="navigation" aria-label="related navigation">
  134. <h3>Navigation</h3>
  135. <ul>
  136. <li class="right" style="margin-right: 10px">
  137. <a href="../genindex.html" title="General Index"
  138. accesskey="I">index</a></li>
  139. <li class="right" >
  140. <a href="../py-modindex.html" title="Python Module Index"
  141. >modules</a> |</li>
  142. <li class="right" >
  143. <a href="cporting.html" title="Porting Extension Modules to Python 3"
  144. accesskey="N">next</a> |</li>
  145. <li class="right" >
  146. <a href="index.html" title="Python HOWTOs"
  147. accesskey="P">previous</a> |</li>
  148. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  149. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  150. <li class="switchers">
  151. <div class="language_switcher_placeholder"></div>
  152. <div class="version_switcher_placeholder"></div>
  153. </li>
  154. <li>
  155. </li>
  156. <li id="cpython-language-and-version">
  157. <a href="../index.html">3.12.0 Documentation</a> &#187;
  158. </li>
  159. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python HOWTOs</a> &#187;</li>
  160. <li class="nav-item nav-item-this"><a href="">How to port Python 2 Code to Python 3</a></li>
  161. <li class="right">
  162. <div class="inline-search" role="search">
  163. <form class="inline-search" action="../search.html" method="get">
  164. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  165. <input type="submit" value="Go" />
  166. </form>
  167. </div>
  168. |
  169. </li>
  170. <li class="right">
  171. <label class="theme-selector-label">
  172. Theme
  173. <select class="theme-selector" oninput="activateTheme(this.value)">
  174. <option value="auto" selected>Auto</option>
  175. <option value="light">Light</option>
  176. <option value="dark">Dark</option>
  177. </select>
  178. </label> |</li>
  179. </ul>
  180. </div>
  181. <div class="document">
  182. <div class="documentwrapper">
  183. <div class="bodywrapper">
  184. <div class="body" role="main">
  185. <section id="how-to-port-python-2-code-to-python-3">
  186. <span id="pyporting-howto"></span><h1>How to port Python 2 Code to Python 3<a class="headerlink" href="#how-to-port-python-2-code-to-python-3" title="Permalink to this headline">¶</a></h1>
  187. <dl class="field-list simple">
  188. <dt class="field-odd">author</dt>
  189. <dd class="field-odd"><p>Brett Cannon</p>
  190. </dd>
  191. </dl>
  192. <div class="topic">
  193. <p class="topic-title">Abstract</p>
  194. <p>Python 2 reached its official end-of-life at the start of 2020. This means
  195. that no new bug reports, fixes, or changes will be made to Python 2 - it’s
  196. no longer supported.</p>
  197. <p>This guide is intended to provide you with a path to Python 3 for your
  198. code, that includes compatibility with Python 2 as a first step.</p>
  199. <p>If you are looking to port an extension module instead of pure Python code,
  200. please see <a class="reference internal" href="cporting.html#cporting-howto"><span class="std std-ref">Porting Extension Modules to Python 3</span></a>.</p>
  201. <p>The archived <a class="reference external" href="https://mail.python.org/pipermail/python-porting/">python-porting</a> mailing list may contain some useful guidance.</p>
  202. </div>
  203. <section id="the-short-explanation">
  204. <h2>The Short Explanation<a class="headerlink" href="#the-short-explanation" title="Permalink to this headline">¶</a></h2>
  205. <p>To achieve Python 2/3 compatibility in a single code base, the basic steps
  206. are:</p>
  207. <ol class="arabic simple">
  208. <li><p>Only worry about supporting Python 2.7</p></li>
  209. <li><p>Make sure you have good test coverage (<a class="reference external" href="https://pypi.org/project/coverage">coverage.py</a> can help;
  210. <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">pip</span> <span class="pre">install</span> <span class="pre">coverage</span></code>)</p></li>
  211. <li><p>Learn the differences between Python 2 and 3</p></li>
  212. <li><p>Use <a class="reference external" href="https://python-future.org/automatic_conversion.html">Futurize</a> (or <a class="reference external" href="https://python-modernize.readthedocs.io/">Modernize</a>) to update your code (e.g. <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">pip</span> <span class="pre">install</span> <span class="pre">future</span></code>)</p></li>
  213. <li><p>Use <a class="reference external" href="https://pypi.org/project/pylint">Pylint</a> to help make sure you don’t regress on your Python 3 support
  214. (<code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">pip</span> <span class="pre">install</span> <span class="pre">pylint</span></code>)</p></li>
  215. <li><p>Use <a class="reference external" href="https://pypi.org/project/caniusepython3">caniusepython3</a> to find out which of your dependencies are blocking your
  216. use of Python 3 (<code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">pip</span> <span class="pre">install</span> <span class="pre">caniusepython3</span></code>)</p></li>
  217. <li><p>Once your dependencies are no longer blocking you, use continuous integration
  218. to make sure you stay compatible with Python 2 and 3 (<a class="reference external" href="https://pypi.org/project/tox">tox</a> can help test
  219. against multiple versions of Python; <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">pip</span> <span class="pre">install</span> <span class="pre">tox</span></code>)</p></li>
  220. <li><p>Consider using optional static type checking to make sure your type usage
  221. works in both Python 2 and 3 (e.g. use <a class="reference external" href="https://mypy-lang.org/">mypy</a> to check your typing under both
  222. Python 2 and Python 3; <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">pip</span> <span class="pre">install</span> <span class="pre">mypy</span></code>).</p></li>
  223. </ol>
  224. <div class="admonition note">
  225. <p class="admonition-title">Note</p>
  226. <p>Note: Using <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">pip</span> <span class="pre">install</span></code> guarantees that the <code class="docutils literal notranslate"><span class="pre">pip</span></code> you invoke
  227. is the one installed for the Python currently in use, whether it be
  228. a system-wide <code class="docutils literal notranslate"><span class="pre">pip</span></code> or one installed within a
  229. <a class="reference internal" href="../tutorial/venv.html#tut-venv"><span class="std std-ref">virtual environment</span></a>.</p>
  230. </div>
  231. </section>
  232. <section id="details">
  233. <h2>Details<a class="headerlink" href="#details" title="Permalink to this headline">¶</a></h2>
  234. <p>Even if other factors - say, dependencies over which you have no control -
  235. still require you to support Python 2, that does not prevent you taking the
  236. step of including Python 3 support.</p>
  237. <p>Most changes required to support Python 3 lead to cleaner code using newer
  238. practices even in Python 2 code.</p>
  239. <section id="different-versions-of-python-2">
  240. <h3>Different versions of Python 2<a class="headerlink" href="#different-versions-of-python-2" title="Permalink to this headline">¶</a></h3>
  241. <p>Ideally, your code should be compatible with Python 2.7, which was the
  242. last supported version of Python 2.</p>
  243. <p>Some of the tools mentioned in this guide will not work with Python 2.6.</p>
  244. <p>If absolutely necessary, the <a class="reference external" href="https://pypi.org/project/six">six</a> project can help you support Python 2.5 and
  245. 3 simultaneously. Do realize, though, that nearly all the projects listed in
  246. this guide will not be available to you.</p>
  247. <p>If you are able to skip Python 2.5 and older, the required changes to your
  248. code will be minimal. At worst you will have to use a function instead of a
  249. method in some instances or have to import a function instead of using a
  250. built-in one.</p>
  251. </section>
  252. <section id="make-sure-you-specify-the-proper-version-support-in-your-setup-py-file">
  253. <h3>Make sure you specify the proper version support in your <code class="docutils literal notranslate"><span class="pre">setup.py</span></code> file<a class="headerlink" href="#make-sure-you-specify-the-proper-version-support-in-your-setup-py-file" title="Permalink to this headline">¶</a></h3>
  254. <p>In your <code class="docutils literal notranslate"><span class="pre">setup.py</span></code> file you should have the proper <a class="reference external" href="https://pypi.org/classifiers">trove classifier</a>
  255. specifying what versions of Python you support. As your project does not support
  256. Python 3 yet you should at least have
  257. <code class="docutils literal notranslate"><span class="pre">Programming</span> <span class="pre">Language</span> <span class="pre">::</span> <span class="pre">Python</span> <span class="pre">::</span> <span class="pre">2</span> <span class="pre">::</span> <span class="pre">Only</span></code> specified. Ideally you should
  258. also specify each major/minor version of Python that you do support, e.g.
  259. <code class="docutils literal notranslate"><span class="pre">Programming</span> <span class="pre">Language</span> <span class="pre">::</span> <span class="pre">Python</span> <span class="pre">::</span> <span class="pre">2.7</span></code>.</p>
  260. </section>
  261. <section id="have-good-test-coverage">
  262. <h3>Have good test coverage<a class="headerlink" href="#have-good-test-coverage" title="Permalink to this headline">¶</a></h3>
  263. <p>Once you have your code supporting the oldest version of Python 2 you want it
  264. to, you will want to make sure your test suite has good coverage. A good rule of
  265. thumb is that if you want to be confident enough in your test suite that any
  266. failures that appear after having tools rewrite your code are actual bugs in the
  267. tools and not in your code. If you want a number to aim for, try to get over 80%
  268. coverage (and don’t feel bad if you find it hard to get better than 90%
  269. coverage). If you don’t already have a tool to measure test coverage then
  270. <a class="reference external" href="https://pypi.org/project/coverage">coverage.py</a> is recommended.</p>
  271. </section>
  272. <section id="be-aware-of-the-differences-between-python-2-and-3">
  273. <h3>Be aware of the differences between Python 2 and 3<a class="headerlink" href="#be-aware-of-the-differences-between-python-2-and-3" title="Permalink to this headline">¶</a></h3>
  274. <p>Once you have your code well-tested you are ready to begin porting your code to
  275. Python 3! But to fully understand how your code is going to change and what
  276. you want to look out for while you code, you will want to learn what changes
  277. Python 3 makes in terms of Python 2.</p>
  278. <p>Some resources for understanding the differences and their implications for you
  279. code:</p>
  280. <ul class="simple">
  281. <li><p>the <a class="reference internal" href="../whatsnew/index.html#whatsnew-index"><span class="std std-ref">“What’s New”</span></a> doc for each release of Python 3</p></li>
  282. <li><p>the <a class="reference external" href="http://python3porting.com/">Porting to Python 3</a> book (which is free online)</p></li>
  283. <li><p>the handy <a class="reference external" href="https://python-future.org/compatible_idioms.html">cheat sheet</a> from the Python-Future project.</p></li>
  284. </ul>
  285. </section>
  286. <section id="update-your-code">
  287. <h3>Update your code<a class="headerlink" href="#update-your-code" title="Permalink to this headline">¶</a></h3>
  288. <p>There are tools available that can port your code automatically.</p>
  289. <p><a class="reference external" href="https://python-future.org/automatic_conversion.html">Futurize</a> does its best to make Python 3 idioms and practices exist in Python
  290. 2, e.g. backporting the <code class="docutils literal notranslate"><span class="pre">bytes</span></code> type from Python 3 so that you have
  291. semantic parity between the major versions of Python. This is the better
  292. approach for most cases.</p>
  293. <p><a class="reference external" href="https://python-modernize.readthedocs.io/">Modernize</a>, on the other hand, is more conservative and targets a Python 2/3
  294. subset of Python, directly relying on <a class="reference external" href="https://pypi.org/project/six">six</a> to help provide compatibility.</p>
  295. <p>A good approach is to run the tool over your test suite first and visually
  296. inspect the diff to make sure the transformation is accurate. After you have
  297. transformed your test suite and verified that all the tests still pass as
  298. expected, then you can transform your application code knowing that any tests
  299. which fail is a translation failure.</p>
  300. <p>Unfortunately the tools can’t automate everything to make your code work under
  301. Python 3, and you will also need to read the tools’ documentation in case some
  302. options you need are turned off by default.</p>
  303. <p>Key issues to be aware of and check for:</p>
  304. <section id="division">
  305. <h4>Division<a class="headerlink" href="#division" title="Permalink to this headline">¶</a></h4>
  306. <p>In Python 3, <code class="docutils literal notranslate"><span class="pre">5</span> <span class="pre">/</span> <span class="pre">2</span> <span class="pre">==</span> <span class="pre">2.5</span></code> and not <code class="docutils literal notranslate"><span class="pre">2</span></code> as it was in Python 2; all
  307. division between <code class="docutils literal notranslate"><span class="pre">int</span></code> values result in a <code class="docutils literal notranslate"><span class="pre">float</span></code>. This change has
  308. actually been planned since Python 2.2 which was released in 2002. Since then
  309. users have been encouraged to add <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">division</span></code> to any
  310. and all files which use the <code class="docutils literal notranslate"><span class="pre">/</span></code> and <code class="docutils literal notranslate"><span class="pre">//</span></code> operators or to be running the
  311. interpreter with the <code class="docutils literal notranslate"><span class="pre">-Q</span></code> flag. If you have not been doing this then you
  312. will need to go through your code and do two things:</p>
  313. <ol class="arabic simple">
  314. <li><p>Add <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">division</span></code> to your files</p></li>
  315. <li><p>Update any division operator as necessary to either use <code class="docutils literal notranslate"><span class="pre">//</span></code> to use floor
  316. division or continue using <code class="docutils literal notranslate"><span class="pre">/</span></code> and expect a float</p></li>
  317. </ol>
  318. <p>The reason that <code class="docutils literal notranslate"><span class="pre">/</span></code> isn’t simply translated to <code class="docutils literal notranslate"><span class="pre">//</span></code> automatically is that if
  319. an object defines a <code class="docutils literal notranslate"><span class="pre">__truediv__</span></code> method but not <code class="docutils literal notranslate"><span class="pre">__floordiv__</span></code> then your
  320. code would begin to fail (e.g. a user-defined class that uses <code class="docutils literal notranslate"><span class="pre">/</span></code> to
  321. signify some operation but not <code class="docutils literal notranslate"><span class="pre">//</span></code> for the same thing or at all).</p>
  322. </section>
  323. <section id="text-versus-binary-data">
  324. <h4>Text versus binary data<a class="headerlink" href="#text-versus-binary-data" title="Permalink to this headline">¶</a></h4>
  325. <p>In Python 2 you could use the <code class="docutils literal notranslate"><span class="pre">str</span></code> type for both text and binary data.
  326. Unfortunately this confluence of two different concepts could lead to brittle
  327. code which sometimes worked for either kind of data, sometimes not. It also
  328. could lead to confusing APIs if people didn’t explicitly state that something
  329. that accepted <code class="docutils literal notranslate"><span class="pre">str</span></code> accepted either text or binary data instead of one
  330. specific type. This complicated the situation especially for anyone supporting
  331. multiple languages as APIs wouldn’t bother explicitly supporting <code class="docutils literal notranslate"><span class="pre">unicode</span></code>
  332. when they claimed text data support.</p>
  333. <p>Python 3 made text and binary data distinct types that cannot simply be mixed
  334. together. For any code that deals only with text or only binary data, this
  335. separation doesn’t pose an issue. But for code that has to deal with both, it
  336. does mean you might have to now care about when you are using text compared
  337. to binary data, which is why this cannot be entirely automated.</p>
  338. <p>Decide which APIs take text and which take binary (it is <strong>highly</strong> recommended
  339. you don’t design APIs that can take both due to the difficulty of keeping the
  340. code working; as stated earlier it is difficult to do well). In Python 2 this
  341. means making sure the APIs that take text can work with <code class="docutils literal notranslate"><span class="pre">unicode</span></code> and those
  342. that work with binary data work with the <code class="docutils literal notranslate"><span class="pre">bytes</span></code> type from Python 3
  343. (which is a subset of <code class="docutils literal notranslate"><span class="pre">str</span></code> in Python 2 and acts as an alias for <code class="docutils literal notranslate"><span class="pre">bytes</span></code>
  344. type in Python 2). Usually the biggest issue is realizing which methods exist
  345. on which types in Python 2 and 3 simultaneously (for text that’s <code class="docutils literal notranslate"><span class="pre">unicode</span></code>
  346. in Python 2 and <code class="docutils literal notranslate"><span class="pre">str</span></code> in Python 3, for binary that’s <code class="docutils literal notranslate"><span class="pre">str</span></code>/<code class="docutils literal notranslate"><span class="pre">bytes</span></code> in
  347. Python 2 and <code class="docutils literal notranslate"><span class="pre">bytes</span></code> in Python 3).</p>
  348. <p>The following table lists the <strong>unique</strong> methods of each data type across
  349. Python 2 and 3 (e.g., the <code class="docutils literal notranslate"><span class="pre">decode()</span></code> method is usable on the equivalent binary
  350. data type in either Python 2 or 3, but it can’t be used by the textual data
  351. type consistently between Python 2 and 3 because <code class="docutils literal notranslate"><span class="pre">str</span></code> in Python 3 doesn’t
  352. have the method). Do note that as of Python 3.5 the <code class="docutils literal notranslate"><span class="pre">__mod__</span></code> method was
  353. added to the bytes type.</p>
  354. <table class="docutils align-default">
  355. <colgroup>
  356. <col style="width: 53%" />
  357. <col style="width: 47%" />
  358. </colgroup>
  359. <tbody>
  360. <tr class="row-odd"><td><p><strong>Text data</strong></p></td>
  361. <td><p><strong>Binary data</strong></p></td>
  362. </tr>
  363. <tr class="row-even"><td><p></p></td>
  364. <td><p>decode</p></td>
  365. </tr>
  366. <tr class="row-odd"><td><p>encode</p></td>
  367. <td></td>
  368. </tr>
  369. <tr class="row-even"><td><p>format</p></td>
  370. <td></td>
  371. </tr>
  372. <tr class="row-odd"><td><p>isdecimal</p></td>
  373. <td></td>
  374. </tr>
  375. <tr class="row-even"><td><p>isnumeric</p></td>
  376. <td></td>
  377. </tr>
  378. </tbody>
  379. </table>
  380. <p>Making the distinction easier to handle can be accomplished by encoding and
  381. decoding between binary data and text at the edge of your code. This means that
  382. when you receive text in binary data, you should immediately decode it. And if
  383. your code needs to send text as binary data then encode it as late as possible.
  384. This allows your code to work with only text internally and thus eliminates
  385. having to keep track of what type of data you are working with.</p>
  386. <p>The next issue is making sure you know whether the string literals in your code
  387. represent text or binary data. You should add a <code class="docutils literal notranslate"><span class="pre">b</span></code> prefix to any
  388. literal that presents binary data. For text you should add a <code class="docutils literal notranslate"><span class="pre">u</span></code> prefix to
  389. the text literal. (There is a <a class="reference internal" href="../library/__future__.html#module-__future__" title="__future__: Future statement definitions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code></a> import to force all unspecified
  390. literals to be Unicode, but usage has shown it isn’t as effective as adding a
  391. <code class="docutils literal notranslate"><span class="pre">b</span></code> or <code class="docutils literal notranslate"><span class="pre">u</span></code> prefix to all literals explicitly)</p>
  392. <p>You also need to be careful about opening files. Possibly you have not always
  393. bothered to add the <code class="docutils literal notranslate"><span class="pre">b</span></code> mode when opening a binary file (e.g., <code class="docutils literal notranslate"><span class="pre">rb</span></code> for
  394. binary reading). Under Python 3, binary files and text files are clearly
  395. distinct and mutually incompatible; see the <a class="reference internal" href="../library/io.html#module-io" title="io: Core tools for working with streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code></a> module for details.
  396. Therefore, you <strong>must</strong> make a decision of whether a file will be used for
  397. binary access (allowing binary data to be read and/or written) or textual access
  398. (allowing text data to be read and/or written). You should also use <a class="reference internal" href="../library/io.html#io.open" title="io.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">io.open()</span></code></a>
  399. for opening files instead of the built-in <a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> function as the <a class="reference internal" href="../library/io.html#module-io" title="io: Core tools for working with streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code></a>
  400. module is consistent from Python 2 to 3 while the built-in <a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> function
  401. is not (in Python 3 it’s actually <a class="reference internal" href="../library/io.html#io.open" title="io.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">io.open()</span></code></a>). Do not bother with the
  402. outdated practice of using <a class="reference internal" href="../library/codecs.html#codecs.open" title="codecs.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">codecs.open()</span></code></a> as that’s only necessary for
  403. keeping compatibility with Python 2.5.</p>
  404. <p>The constructors of both <code class="docutils literal notranslate"><span class="pre">str</span></code> and <code class="docutils literal notranslate"><span class="pre">bytes</span></code> have different semantics for the
  405. same arguments between Python 2 and 3. Passing an integer to <code class="docutils literal notranslate"><span class="pre">bytes</span></code> in Python 2
  406. will give you the string representation of the integer: <code class="docutils literal notranslate"><span class="pre">bytes(3)</span> <span class="pre">==</span> <span class="pre">'3'</span></code>.
  407. But in Python 3, an integer argument to <code class="docutils literal notranslate"><span class="pre">bytes</span></code> will give you a bytes object
  408. as long as the integer specified, filled with null bytes:
  409. <code class="docutils literal notranslate"><span class="pre">bytes(3)</span> <span class="pre">==</span> <span class="pre">b'\x00\x00\x00'</span></code>. A similar worry is necessary when passing a
  410. bytes object to <code class="docutils literal notranslate"><span class="pre">str</span></code>. In Python 2 you just get the bytes object back:
  411. <code class="docutils literal notranslate"><span class="pre">str(b'3')</span> <span class="pre">==</span> <span class="pre">b'3'</span></code>. But in Python 3 you get the string representation of the
  412. bytes object: <code class="docutils literal notranslate"><span class="pre">str(b'3')</span> <span class="pre">==</span> <span class="pre">&quot;b'3'&quot;</span></code>.</p>
  413. <p>Finally, the indexing of binary data requires careful handling (slicing does
  414. <strong>not</strong> require any special handling). In Python 2,
  415. <code class="docutils literal notranslate"><span class="pre">b'123'[1]</span> <span class="pre">==</span> <span class="pre">b'2'</span></code> while in Python 3 <code class="docutils literal notranslate"><span class="pre">b'123'[1]</span> <span class="pre">==</span> <span class="pre">50</span></code>. Because binary data
  416. is simply a collection of binary numbers, Python 3 returns the integer value for
  417. the byte you index on. But in Python 2 because <code class="docutils literal notranslate"><span class="pre">bytes</span> <span class="pre">==</span> <span class="pre">str</span></code>, indexing
  418. returns a one-item slice of bytes. The <a class="reference external" href="https://pypi.org/project/six">six</a> project has a function
  419. named <code class="docutils literal notranslate"><span class="pre">six.indexbytes()</span></code> which will return an integer like in Python 3:
  420. <code class="docutils literal notranslate"><span class="pre">six.indexbytes(b'123',</span> <span class="pre">1)</span></code>.</p>
  421. <p>To summarize:</p>
  422. <ol class="arabic simple">
  423. <li><p>Decide which of your APIs take text and which take binary data</p></li>
  424. <li><p>Make sure that your code that works with text also works with <code class="docutils literal notranslate"><span class="pre">unicode</span></code> and
  425. code for binary data works with <code class="docutils literal notranslate"><span class="pre">bytes</span></code> in Python 2 (see the table above
  426. for what methods you cannot use for each type)</p></li>
  427. <li><p>Mark all binary literals with a <code class="docutils literal notranslate"><span class="pre">b</span></code> prefix, textual literals with a <code class="docutils literal notranslate"><span class="pre">u</span></code>
  428. prefix</p></li>
  429. <li><p>Decode binary data to text as soon as possible, encode text as binary data as
  430. late as possible</p></li>
  431. <li><p>Open files using <a class="reference internal" href="../library/io.html#io.open" title="io.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">io.open()</span></code></a> and make sure to specify the <code class="docutils literal notranslate"><span class="pre">b</span></code> mode when
  432. appropriate</p></li>
  433. <li><p>Be careful when indexing into binary data</p></li>
  434. </ol>
  435. </section>
  436. <section id="use-feature-detection-instead-of-version-detection">
  437. <h4>Use feature detection instead of version detection<a class="headerlink" href="#use-feature-detection-instead-of-version-detection" title="Permalink to this headline">¶</a></h4>
  438. <p>Inevitably you will have code that has to choose what to do based on what
  439. version of Python is running. The best way to do this is with feature detection
  440. of whether the version of Python you’re running under supports what you need.
  441. If for some reason that doesn’t work then you should make the version check be
  442. against Python 2 and not Python 3. To help explain this, let’s look at an
  443. example.</p>
  444. <p>Let’s pretend that you need access to a feature of <a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> that
  445. is available in Python’s standard library since Python 3.3 and available for
  446. Python 2 through <a class="reference external" href="https://pypi.org/project/importlib2">importlib2</a> on PyPI. You might be tempted to write code to
  447. access e.g. the <a class="reference internal" href="../library/importlib.html#module-importlib.abc" title="importlib.abc: Abstract base classes related to import"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.abc</span></code></a> module by doing the following:</p>
  448. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sys</span>
  449. <span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
  450. <span class="kn">from</span> <span class="nn">importlib</span> <span class="kn">import</span> <span class="n">abc</span>
  451. <span class="k">else</span><span class="p">:</span>
  452. <span class="kn">from</span> <span class="nn">importlib2</span> <span class="kn">import</span> <span class="n">abc</span>
  453. </pre></div>
  454. </div>
  455. <p>The problem with this code is what happens when Python 4 comes out? It would
  456. be better to treat Python 2 as the exceptional case instead of Python 3 and
  457. assume that future Python versions will be more compatible with Python 3 than
  458. Python 2:</p>
  459. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sys</span>
  460. <span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">:</span>
  461. <span class="kn">from</span> <span class="nn">importlib</span> <span class="kn">import</span> <span class="n">abc</span>
  462. <span class="k">else</span><span class="p">:</span>
  463. <span class="kn">from</span> <span class="nn">importlib2</span> <span class="kn">import</span> <span class="n">abc</span>
  464. </pre></div>
  465. </div>
  466. <p>The best solution, though, is to do no version detection at all and instead rely
  467. on feature detection. That avoids any potential issues of getting the version
  468. detection wrong and helps keep you future-compatible:</p>
  469. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span>
  470. <span class="kn">from</span> <span class="nn">importlib</span> <span class="kn">import</span> <span class="n">abc</span>
  471. <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
  472. <span class="kn">from</span> <span class="nn">importlib2</span> <span class="kn">import</span> <span class="n">abc</span>
  473. </pre></div>
  474. </div>
  475. </section>
  476. </section>
  477. <section id="prevent-compatibility-regressions">
  478. <h3>Prevent compatibility regressions<a class="headerlink" href="#prevent-compatibility-regressions" title="Permalink to this headline">¶</a></h3>
  479. <p>Once you have fully translated your code to be compatible with Python 3, you
  480. will want to make sure your code doesn’t regress and stop working under
  481. Python 3. This is especially true if you have a dependency which is blocking you
  482. from actually running under Python 3 at the moment.</p>
  483. <p>To help with staying compatible, any new modules you create should have
  484. at least the following block of code at the top of it:</p>
  485. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">absolute_import</span>
  486. <span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">division</span>
  487. <span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
  488. </pre></div>
  489. </div>
  490. <p>You can also run Python 2 with the <code class="docutils literal notranslate"><span class="pre">-3</span></code> flag to be warned about various
  491. compatibility issues your code triggers during execution. If you turn warnings
  492. into errors with <code class="docutils literal notranslate"><span class="pre">-Werror</span></code> then you can make sure that you don’t accidentally
  493. miss a warning.</p>
  494. <p>You can also use the <a class="reference external" href="https://pypi.org/project/pylint">Pylint</a> project and its <code class="docutils literal notranslate"><span class="pre">--py3k</span></code> flag to lint your code
  495. to receive warnings when your code begins to deviate from Python 3
  496. compatibility. This also prevents you from having to run <a class="reference external" href="https://python-modernize.readthedocs.io/">Modernize</a> or <a class="reference external" href="https://python-future.org/automatic_conversion.html">Futurize</a>
  497. over your code regularly to catch compatibility regressions. This does require
  498. you only support Python 2.7 and Python 3.4 or newer as that is Pylint’s
  499. minimum Python version support.</p>
  500. </section>
  501. <section id="check-which-dependencies-block-your-transition">
  502. <h3>Check which dependencies block your transition<a class="headerlink" href="#check-which-dependencies-block-your-transition" title="Permalink to this headline">¶</a></h3>
  503. <p><strong>After</strong> you have made your code compatible with Python 3 you should begin to
  504. care about whether your dependencies have also been ported. The <a class="reference external" href="https://pypi.org/project/caniusepython3">caniusepython3</a>
  505. project was created to help you determine which projects
  506. – directly or indirectly – are blocking you from supporting Python 3. There
  507. is both a command-line tool as well as a web interface at
  508. <a class="reference external" href="https://caniusepython3.com">https://caniusepython3.com</a>.</p>
  509. <p>The project also provides code which you can integrate into your test suite so
  510. that you will have a failing test when you no longer have dependencies blocking
  511. you from using Python 3. This allows you to avoid having to manually check your
  512. dependencies and to be notified quickly when you can start running on Python 3.</p>
  513. </section>
  514. <section id="update-your-setup-py-file-to-denote-python-3-compatibility">
  515. <h3>Update your <code class="docutils literal notranslate"><span class="pre">setup.py</span></code> file to denote Python 3 compatibility<a class="headerlink" href="#update-your-setup-py-file-to-denote-python-3-compatibility" title="Permalink to this headline">¶</a></h3>
  516. <p>Once your code works under Python 3, you should update the classifiers in
  517. your <code class="docutils literal notranslate"><span class="pre">setup.py</span></code> to contain <code class="docutils literal notranslate"><span class="pre">Programming</span> <span class="pre">Language</span> <span class="pre">::</span> <span class="pre">Python</span> <span class="pre">::</span> <span class="pre">3</span></code> and to not
  518. specify sole Python 2 support. This will tell anyone using your code that you
  519. support Python 2 <strong>and</strong> 3. Ideally you will also want to add classifiers for
  520. each major/minor version of Python you now support.</p>
  521. </section>
  522. <section id="use-continuous-integration-to-stay-compatible">
  523. <h3>Use continuous integration to stay compatible<a class="headerlink" href="#use-continuous-integration-to-stay-compatible" title="Permalink to this headline">¶</a></h3>
  524. <p>Once you are able to fully run under Python 3 you will want to make sure your
  525. code always works under both Python 2 and 3. Probably the best tool for running
  526. your tests under multiple Python interpreters is <a class="reference external" href="https://pypi.org/project/tox">tox</a>. You can then integrate
  527. tox with your continuous integration system so that you never accidentally break
  528. Python 2 or 3 support.</p>
  529. <p>You may also want to use the <code class="docutils literal notranslate"><span class="pre">-bb</span></code> flag with the Python 3 interpreter to
  530. trigger an exception when you are comparing bytes to strings or bytes to an int
  531. (the latter is available starting in Python 3.5). By default type-differing
  532. comparisons simply return <code class="docutils literal notranslate"><span class="pre">False</span></code>, but if you made a mistake in your
  533. separation of text/binary data handling or indexing on bytes you wouldn’t easily
  534. find the mistake. This flag will raise an exception when these kinds of
  535. comparisons occur, making the mistake much easier to track down.</p>
  536. </section>
  537. <section id="consider-using-optional-static-type-checking">
  538. <h3>Consider using optional static type checking<a class="headerlink" href="#consider-using-optional-static-type-checking" title="Permalink to this headline">¶</a></h3>
  539. <p>Another way to help port your code is to use a static type checker like
  540. <a class="reference external" href="https://mypy-lang.org/">mypy</a> or <a class="reference external" href="https://github.com/google/pytype">pytype</a> on your code. These tools can be used to analyze your code as
  541. if it’s being run under Python 2, then you can run the tool a second time as if
  542. your code is running under Python 3. By running a static type checker twice like
  543. this you can discover if you’re e.g. misusing binary data type in one version
  544. of Python compared to another. If you add optional type hints to your code you
  545. can also explicitly state whether your APIs use textual or binary data, helping
  546. to make sure everything functions as expected in both versions of Python.</p>
  547. </section>
  548. </section>
  549. </section>
  550. <div class="clearer"></div>
  551. </div>
  552. </div>
  553. </div>
  554. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  555. <div class="sphinxsidebarwrapper">
  556. <div>
  557. <h3><a href="../contents.html">Table of Contents</a></h3>
  558. <ul>
  559. <li><a class="reference internal" href="#">How to port Python 2 Code to Python 3</a><ul>
  560. <li><a class="reference internal" href="#the-short-explanation">The Short Explanation</a></li>
  561. <li><a class="reference internal" href="#details">Details</a><ul>
  562. <li><a class="reference internal" href="#different-versions-of-python-2">Different versions of Python 2</a></li>
  563. <li><a class="reference internal" href="#make-sure-you-specify-the-proper-version-support-in-your-setup-py-file">Make sure you specify the proper version support in your <code class="docutils literal notranslate"><span class="pre">setup.py</span></code> file</a></li>
  564. <li><a class="reference internal" href="#have-good-test-coverage">Have good test coverage</a></li>
  565. <li><a class="reference internal" href="#be-aware-of-the-differences-between-python-2-and-3">Be aware of the differences between Python 2 and 3</a></li>
  566. <li><a class="reference internal" href="#update-your-code">Update your code</a><ul>
  567. <li><a class="reference internal" href="#division">Division</a></li>
  568. <li><a class="reference internal" href="#text-versus-binary-data">Text versus binary data</a></li>
  569. <li><a class="reference internal" href="#use-feature-detection-instead-of-version-detection">Use feature detection instead of version detection</a></li>
  570. </ul>
  571. </li>
  572. <li><a class="reference internal" href="#prevent-compatibility-regressions">Prevent compatibility regressions</a></li>
  573. <li><a class="reference internal" href="#check-which-dependencies-block-your-transition">Check which dependencies block your transition</a></li>
  574. <li><a class="reference internal" href="#update-your-setup-py-file-to-denote-python-3-compatibility">Update your <code class="docutils literal notranslate"><span class="pre">setup.py</span></code> file to denote Python 3 compatibility</a></li>
  575. <li><a class="reference internal" href="#use-continuous-integration-to-stay-compatible">Use continuous integration to stay compatible</a></li>
  576. <li><a class="reference internal" href="#consider-using-optional-static-type-checking">Consider using optional static type checking</a></li>
  577. </ul>
  578. </li>
  579. </ul>
  580. </li>
  581. </ul>
  582. </div>
  583. <div>
  584. <h4>Previous topic</h4>
  585. <p class="topless"><a href="index.html"
  586. title="previous chapter">Python HOWTOs</a></p>
  587. </div>
  588. <div>
  589. <h4>Next topic</h4>
  590. <p class="topless"><a href="cporting.html"
  591. title="next chapter">Porting Extension Modules to Python 3</a></p>
  592. </div>
  593. <div role="note" aria-label="source link">
  594. <h3>This Page</h3>
  595. <ul class="this-page-menu">
  596. <li><a href="../bugs.html">Report a Bug</a></li>
  597. <li>
  598. <a href="https://github.com/python/cpython/blob/main/Doc/howto/pyporting.rst"
  599. rel="nofollow">Show Source
  600. </a>
  601. </li>
  602. </ul>
  603. </div>
  604. </div>
  605. </div>
  606. <div class="clearer"></div>
  607. </div>
  608. <div class="related" role="navigation" aria-label="related navigation">
  609. <h3>Navigation</h3>
  610. <ul>
  611. <li class="right" style="margin-right: 10px">
  612. <a href="../genindex.html" title="General Index"
  613. >index</a></li>
  614. <li class="right" >
  615. <a href="../py-modindex.html" title="Python Module Index"
  616. >modules</a> |</li>
  617. <li class="right" >
  618. <a href="cporting.html" title="Porting Extension Modules to Python 3"
  619. >next</a> |</li>
  620. <li class="right" >
  621. <a href="index.html" title="Python HOWTOs"
  622. >previous</a> |</li>
  623. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  624. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  625. <li class="switchers">
  626. <div class="language_switcher_placeholder"></div>
  627. <div class="version_switcher_placeholder"></div>
  628. </li>
  629. <li>
  630. </li>
  631. <li id="cpython-language-and-version">
  632. <a href="../index.html">3.12.0 Documentation</a> &#187;
  633. </li>
  634. <li class="nav-item nav-item-1"><a href="index.html" >Python HOWTOs</a> &#187;</li>
  635. <li class="nav-item nav-item-this"><a href="">How to port Python 2 Code to Python 3</a></li>
  636. <li class="right">
  637. <div class="inline-search" role="search">
  638. <form class="inline-search" action="../search.html" method="get">
  639. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  640. <input type="submit" value="Go" />
  641. </form>
  642. </div>
  643. |
  644. </li>
  645. <li class="right">
  646. <label class="theme-selector-label">
  647. Theme
  648. <select class="theme-selector" oninput="activateTheme(this.value)">
  649. <option value="auto" selected>Auto</option>
  650. <option value="light">Light</option>
  651. <option value="dark">Dark</option>
  652. </select>
  653. </label> |</li>
  654. </ul>
  655. </div>
  656. <div class="footer">
  657. &copy; <a href="../copyright.html">Copyright</a> 2001-2023, Python Software Foundation.
  658. <br />
  659. This page is licensed under the Python Software Foundation License Version 2.
  660. <br />
  661. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  662. <br />
  663. See <a href="/license.html">History and License</a> for more information.<br />
  664. <br />
  665. The Python Software Foundation is a non-profit corporation.
  666. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  667. <br />
  668. <br />
  669. Last updated on Oct 02, 2023.
  670. <a href="/bugs.html">Found a bug</a>?
  671. <br />
  672. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
  673. </div>
  674. </body>
  675. </html>