gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

317 lines
13KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  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,. 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 supported: see..." />
  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,. 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 supported: see..." />
  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.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="canonical" href="https://docs.python.org/3/howto/pyporting.html" />
  33. <style>
  34. @media only screen {
  35. table.full-width-table {
  36. width: 100%;
  37. }
  38. }
  39. </style>
  40. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  41. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  42. <script type="text/javascript" src="../_static/copybutton.js"></script>
  43. <script type="text/javascript" src="../_static/menu.js"></script>
  44. <script type="text/javascript" src="../_static/search-focus.js"></script>
  45. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  46. </head>
  47. <body>
  48. <div class="mobile-nav">
  49. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  50. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  51. <nav class="nav-content" role="navigation">
  52. <label for="menuToggler" class="toggler__label">
  53. <span></span>
  54. </label>
  55. <span class="nav-items-wrapper">
  56. <a href="https://www.python.org/" class="nav-logo">
  57. <img src="../_static/py.svg" alt="Python logo"/>
  58. </a>
  59. <span class="version_switcher_placeholder"></span>
  60. <form role="search" class="search" action="../search.html" method="get">
  61. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  62. <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>
  63. </svg>
  64. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  65. <input type="submit" value="Go"/>
  66. </form>
  67. </span>
  68. </nav>
  69. <div class="menu-wrapper">
  70. <nav class="menu" role="navigation" aria-label="main navigation">
  71. <div class="language_switcher_placeholder"></div>
  72. <label class="theme-selector-label">
  73. Theme
  74. <select class="theme-selector" oninput="activateTheme(this.value)">
  75. <option value="auto" selected>Auto</option>
  76. <option value="light">Light</option>
  77. <option value="dark">Dark</option>
  78. </select>
  79. </label>
  80. <div>
  81. <h3><a href="../contents.html">Table of Contents</a></h3>
  82. <ul>
  83. <li><a class="reference internal" href="#">How to port Python 2 Code to Python 3</a><ul>
  84. <li><a class="reference internal" href="#third-party-guides">Third-party guides</a></li>
  85. </ul>
  86. </li>
  87. </ul>
  88. </div>
  89. <div role="note" aria-label="source link">
  90. <h3>This Page</h3>
  91. <ul class="this-page-menu">
  92. <li><a href="../bugs.html">Report a Bug</a></li>
  93. <li>
  94. <a href="https://github.com/python/cpython/blob/main/Doc/howto/pyporting.rst"
  95. rel="nofollow">Show Source
  96. </a>
  97. </li>
  98. </ul>
  99. </div>
  100. </nav>
  101. </div>
  102. </div>
  103. <div class="related" role="navigation" aria-label="related navigation">
  104. <h3>Navigation</h3>
  105. <ul>
  106. <li class="right" style="margin-right: 10px">
  107. <a href="../genindex.html" title="General Index"
  108. accesskey="I">index</a></li>
  109. <li class="right" >
  110. <a href="../py-modindex.html" title="Python Module Index"
  111. >modules</a> |</li>
  112. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  113. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  114. <li class="switchers">
  115. <div class="language_switcher_placeholder"></div>
  116. <div class="version_switcher_placeholder"></div>
  117. </li>
  118. <li>
  119. </li>
  120. <li id="cpython-language-and-version">
  121. <a href="../index.html">3.12.3 Documentation</a> &#187;
  122. </li>
  123. <li class="nav-item nav-item-this"><a href="">How to port Python 2 Code to Python 3</a></li>
  124. <li class="right">
  125. <div class="inline-search" role="search">
  126. <form class="inline-search" action="../search.html" method="get">
  127. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  128. <input type="submit" value="Go" />
  129. </form>
  130. </div>
  131. |
  132. </li>
  133. <li class="right">
  134. <label class="theme-selector-label">
  135. Theme
  136. <select class="theme-selector" oninput="activateTheme(this.value)">
  137. <option value="auto" selected>Auto</option>
  138. <option value="light">Light</option>
  139. <option value="dark">Dark</option>
  140. </select>
  141. </label> |</li>
  142. </ul>
  143. </div>
  144. <div class="document">
  145. <div class="documentwrapper">
  146. <div class="bodywrapper">
  147. <div class="body" role="main">
  148. <section id="how-to-port-python-2-code-to-python-3">
  149. <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="Link to this heading">¶</a></h1>
  150. <dl class="field-list simple">
  151. <dt class="field-odd">author<span class="colon">:</span></dt>
  152. <dd class="field-odd"><p>Brett Cannon</p>
  153. </dd>
  154. </dl>
  155. <p>Python 2 reached its official end-of-life at the start of 2020. This means
  156. that no new bug reports, fixes, or changes will be made to Python 2 - it’s
  157. no longer supported: see <span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0373/"><strong>PEP 373</strong></a> and
  158. <a class="reference external" href="https://devguide.python.org/versions">status of Python versions</a>.</p>
  159. <p>If you are looking to port an extension module instead of pure Python code,
  160. 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>
  161. <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>
  162. <p>Since Python 3.13 the original porting guide was discontinued.
  163. You can find the old guide in the
  164. <a class="reference external" href="https://docs.python.org/3.12/howto/pyporting.html">archive</a>.</p>
  165. <section id="third-party-guides">
  166. <h2>Third-party guides<a class="headerlink" href="#third-party-guides" title="Link to this heading">¶</a></h2>
  167. <p>There are also multiple third-party guides that might be useful:</p>
  168. <ul class="simple">
  169. <li><p><a class="reference external" href="https://portingguide.readthedocs.io">Guide by Fedora</a></p></li>
  170. <li><p><a class="reference external" href="https://www.youtube.com/watch?v=JgIgEjASOlk">PyCon 2020 tutorial</a></p></li>
  171. <li><p><a class="reference external" href="https://www.digitalocean.com/community/tutorials/how-to-port-python-2-code-to-python-3">Guide by DigitalOcean</a></p></li>
  172. <li><p><a class="reference external" href="https://www.activestate.com/blog/how-to-migrate-python-2-applications-to-python-3">Guide by ActiveState</a></p></li>
  173. </ul>
  174. </section>
  175. </section>
  176. <div class="clearer"></div>
  177. </div>
  178. </div>
  179. </div>
  180. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  181. <div class="sphinxsidebarwrapper">
  182. <div>
  183. <h3><a href="../contents.html">Table of Contents</a></h3>
  184. <ul>
  185. <li><a class="reference internal" href="#">How to port Python 2 Code to Python 3</a><ul>
  186. <li><a class="reference internal" href="#third-party-guides">Third-party guides</a></li>
  187. </ul>
  188. </li>
  189. </ul>
  190. </div>
  191. <div role="note" aria-label="source link">
  192. <h3>This Page</h3>
  193. <ul class="this-page-menu">
  194. <li><a href="../bugs.html">Report a Bug</a></li>
  195. <li>
  196. <a href="https://github.com/python/cpython/blob/main/Doc/howto/pyporting.rst"
  197. rel="nofollow">Show Source
  198. </a>
  199. </li>
  200. </ul>
  201. </div>
  202. </div>
  203. <div id="sidebarbutton" title="Collapse sidebar">
  204. <span>«</span>
  205. </div>
  206. </div>
  207. <div class="clearer"></div>
  208. </div>
  209. <div class="related" role="navigation" aria-label="related navigation">
  210. <h3>Navigation</h3>
  211. <ul>
  212. <li class="right" style="margin-right: 10px">
  213. <a href="../genindex.html" title="General Index"
  214. >index</a></li>
  215. <li class="right" >
  216. <a href="../py-modindex.html" title="Python Module Index"
  217. >modules</a> |</li>
  218. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  219. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  220. <li class="switchers">
  221. <div class="language_switcher_placeholder"></div>
  222. <div class="version_switcher_placeholder"></div>
  223. </li>
  224. <li>
  225. </li>
  226. <li id="cpython-language-and-version">
  227. <a href="../index.html">3.12.3 Documentation</a> &#187;
  228. </li>
  229. <li class="nav-item nav-item-this"><a href="">How to port Python 2 Code to Python 3</a></li>
  230. <li class="right">
  231. <div class="inline-search" role="search">
  232. <form class="inline-search" action="../search.html" method="get">
  233. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  234. <input type="submit" value="Go" />
  235. </form>
  236. </div>
  237. |
  238. </li>
  239. <li class="right">
  240. <label class="theme-selector-label">
  241. Theme
  242. <select class="theme-selector" oninput="activateTheme(this.value)">
  243. <option value="auto" selected>Auto</option>
  244. <option value="light">Light</option>
  245. <option value="dark">Dark</option>
  246. </select>
  247. </label> |</li>
  248. </ul>
  249. </div>
  250. <div class="footer">
  251. &copy;
  252. <a href="../copyright.html">
  253. Copyright
  254. </a>
  255. 2001-2024, Python Software Foundation.
  256. <br />
  257. This page is licensed under the Python Software Foundation License Version 2.
  258. <br />
  259. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  260. <br />
  261. See <a href="/license.html">History and License</a> for more information.<br />
  262. <br />
  263. The Python Software Foundation is a non-profit corporation.
  264. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  265. <br />
  266. <br />
  267. Last updated on Apr 09, 2024 (13:47 UTC).
  268. <a href="/bugs.html">Found a bug</a>?
  269. <br />
  270. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  271. </div>
  272. </body>
  273. </html>
上海开阖软件有限公司 沪ICP备12045867号-1