{"id":947990,"date":"2024-12-26T23:59:24","date_gmt":"2024-12-26T15:59:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/947990.html"},"modified":"2024-12-26T23:59:26","modified_gmt":"2024-12-26T15:59:26","slug":"python3%e5%a6%82%e4%bd%95%e5%85%bc%e5%ae%b9python2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/947990.html","title":{"rendered":"python3\u5982\u4f55\u517c\u5bb9python2"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083532\/c2bb7dc6-6ed1-4ef6-abb1-3a29c1d7774e.webp\" alt=\"python3\u5982\u4f55\u517c\u5bb9python2\" \/><\/p>\n<p><p> <strong>Python 3\u517c\u5bb9Python 2\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528__future__\u6a21\u5757\u3001\u4f7f\u7528\u73b0\u4ee3\u5316\u7684\u7f16\u7801\u5b9e\u8df5\u3001\u5229\u7528\u5de5\u5177\u59822to3\u548csix\u5e93\u3001\u907f\u514d\u4f7f\u7528Python 2\u4e2d\u7684\u8fc7\u65f6\u7279\u6027\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528__future__\u6a21\u5757\u662f\u4e00\u4e2a\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9Python 3\u4ee3\u7801\u5728Python 2\u4e2d\u8fd0\u884c\u3002\u901a\u8fc7\u5728\u4ee3\u7801\u7684\u5f00\u5934\u5bfc\u5165__future__\u4e2d\u7684\u7279\u6027\uff0c\u5982<code>from __future__ import print_function<\/code>\uff0c\u53ef\u4ee5\u4f7fPython 2\u63a5\u53d7Python 3\u7684\u8bed\u6cd5\u3002\u4f8b\u5982\uff0cPython 3\u4e2d\u7684<code>print()<\/code>\u51fd\u6570\u5728Python 2\u4e2d\u662f\u4e00\u4e2a\u8bed\u53e5\uff0c\u901a\u8fc7\u5bfc\u5165<code>print_function<\/code>\uff0c\u53ef\u4ee5\u5728Python 2\u4e2d\u4f7f\u7528Python 3\u7684<code>print()<\/code>\u51fd\u6570\u8bed\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5728\u8be6\u7ec6\u63a2\u8ba8__future__\u6a21\u5757\u4e4b\u524d\uff0c\u4e86\u89e3Python 2\u548cPython 3\u4e4b\u95f4\u7684\u4e3b\u8981\u5dee\u5f02\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002Python 3\u5f15\u5165\u4e86\u8bb8\u591a\u6539\u8fdb\u548c\u53d8\u5316\uff0c\u8fd9\u4e9b\u53d8\u5316\u4f7f\u5f97\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u5f97\u5230\u4e86\u63d0\u5347\uff0c\u4f46\u540c\u65f6\u4e5f\u9020\u6210\u4e86\u4e00\u5b9a\u7684\u517c\u5bb9\u6027\u95ee\u9898\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\uff0c\u6211\u4eec\u9700\u8981\u4e86\u89e3\u5e76\u5e94\u7528\u5404\u79cd\u65b9\u6cd5\uff0c\u4ee5\u786e\u4fdd\u6211\u4eec\u7684\u4ee3\u7801\u80fd\u591f\u5728\u4e24\u4e2a\u7248\u672c\u4e2d\u6d41\u7545\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001__FUTURE__\u6a21\u5757<\/h3>\n<\/p>\n<p><p>__future__\u6a21\u5757\u662fPython\u4e2d\u7684\u4e00\u4e2a\u7279\u6b8a\u6a21\u5757\uff0c\u7528\u4e8e\u5728Python 2\u4e2d\u542f\u7528Python 3\u7684\u7279\u6027\u3002\u901a\u8fc7\u5bfc\u5165__future__\u4e2d\u7684\u7279\u6027\uff0c\u53ef\u4ee5\u4f7fPython 2\u63a5\u53d7\u67d0\u4e9bPython 3\u7684\u8bed\u6cd5\u548c\u529f\u80fd\uff0c\u8fd9\u5bf9\u4e8e\u7f16\u5199\u517c\u5bb9\u6027\u4ee3\u7801\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<p><h4>1. __future__\u6a21\u5757\u7684\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u5728Python 2\u4e2d\u4f7f\u7528__future__\u6a21\u5757\u7684\u7279\u6027\uff0c\u53ef\u4ee5\u9010\u6b65\u5f15\u5165Python 3\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff0cPython 3\u4e2d\u7684<code>print()<\/code>\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u800c\u5728Python 2\u4e2d\u662f\u4e00\u4e2a\u8bed\u53e5\u3002\u901a\u8fc7\u5bfc\u5165<code>print_function<\/code>\uff0c\u53ef\u4ee5\u5728Python 2\u4e2d\u4f7f\u7528Python 3\u7684<code>print()<\/code>\u51fd\u6570\u8bed\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from __future__ import print_function<\/p>\n<p>print(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u7279\u6027\u4f7f\u5f97\u4f60\u5728Python 2\u4e2d\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u79fb\u690d\u6027\u3002<\/p>\n<\/p>\n<p><h4>2. \u5e38\u7528\u7684__future__\u7279\u6027<\/h4>\n<\/p>\n<p><p>\u9664\u4e86<code>print_function<\/code>\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u5e38\u7528\u7684__future__\u7279\u6027\u53ef\u4ee5\u5e2e\u52a9\u63d0\u5347\u517c\u5bb9\u6027\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>division<\/strong>: \u5728Python 3\u4e2d\uff0c\u9664\u6cd5\u64cd\u4f5c\u7b26<code>\/<\/code>\u603b\u662f\u6267\u884c\u6d6e\u70b9\u9664\u6cd5\uff0c\u800c<code>\/\/<\/code>\u6267\u884c\u6574\u6570\u9664\u6cd5\u3002\u5728Python 2\u4e2d\uff0c<code>\/<\/code>\u6267\u884c\u6574\u6570\u9664\u6cd5\u3002\u901a\u8fc7<code>from __future__ import division<\/code>\uff0c\u53ef\u4ee5\u4f7fPython 2\u7684<code>\/<\/code>\u884c\u4e3a\u4e0ePython 3\u4e00\u81f4\u3002<\/li>\n<li><strong>unicode_literals<\/strong>: Python 3\u9ed8\u8ba4\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\uff0c\u800cPython 2\u9ed8\u8ba4\u4f7f\u7528ASCII\u3002\u901a\u8fc7<code>from __future__ import unicode_literals<\/code>\uff0c\u53ef\u4ee5\u4f7fPython 2\u4e2d\u6240\u6709\u5b57\u7b26\u4e32\u6587\u5b57\u6210\u4e3aUnicode\u3002<\/li>\n<li><strong>absolute_import<\/strong>: \u5728Python 3\u4e2d\uff0cimport\u8bed\u53e5\u9ed8\u8ba4\u662f\u7edd\u5bf9\u5bfc\u5165\uff0c\u800c\u4e0d\u662f\u76f8\u5bf9\u5bfc\u5165\u3002\u4f7f\u7528<code>from __future__ import absolute_import<\/code>\u53ef\u4ee5\u4f7fPython 2\u4e2d\u7684import\u884c\u4e3a\u4e0ePython 3\u4e00\u81f4\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u73b0\u4ee3\u5316\u7684\u7f16\u7801\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8Python 2\u548c3\u7684\u517c\u5bb9\u6027\uff0c\u91c7\u7528\u73b0\u4ee3\u5316\u7684\u7f16\u7801\u5b9e\u8df5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u8fd9\u4e0d\u4ec5\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u8fd8\u80fd\u786e\u4fdd\u4ee3\u7801\u5728\u4e24\u4e2a\u7248\u672c\u4e2d\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b57\u7b26\u4e32\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728Python 3\u4e2d\uff0c\u6240\u6709\u5b57\u7b26\u4e32\u9ed8\u8ba4\u662fUnicode\uff0c\u800c\u5728Python 2\u4e2d\uff0c\u5b57\u7b26\u4e32\u9ed8\u8ba4\u662f\u5b57\u8282\u4e32\u3002\u5728\u7f16\u5199\u517c\u5bb9\u4ee3\u7801\u65f6\uff0c\u5e94\u59cb\u7ec8\u663e\u5f0f\u5730\u4f7f\u7528<code>str<\/code>\u548c<code>unicode<\/code>\uff08\u5728Python 2\u4e2d\uff09\u6216<code>str<\/code>\uff08\u5728Python 3\u4e2d\uff09\u6765\u533a\u5206\u8fd9\u4e24\u79cd\u5b57\u7b26\u4e32\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Python 2<\/p>\n<p>s = u&quot;Hello, Unicode&quot;<\/p>\n<h2><strong>Python 3<\/strong><\/h2>\n<p>s = &quot;Hello, Unicode&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u5b57\u7b26\u4e32\u5904\u7406\u5728\u4e24\u4e2a\u7248\u672c\u4e2d\u90fd\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u8fed\u4ee3\u5668\u548c\u751f\u6210\u5668<\/h4>\n<\/p>\n<p><p>Python 3\u4e2d\u7684\u8bb8\u591a\u5185\u7f6e\u51fd\u6570\u8fd4\u56de\u8fed\u4ee3\u5668\u800c\u4e0d\u662f\u5217\u8868\uff0c\u8fd9\u6709\u52a9\u4e8e\u63d0\u9ad8\u6027\u80fd\u3002\u5728Python 2\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>itertools<\/code>\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff0c<code>range()<\/code>\u5728Python 3\u4e2d\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u800c\u5728Python 2\u4e2d\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\u3002\u53ef\u4ee5\u4f7f\u7528<code>xrange()<\/code>\u5728Python 2\u4e2d\u83b7\u5f97\u7c7b\u4f3c\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Python 2<\/p>\n<p>for i in xrange(10):<\/p>\n<p>    print(i)<\/p>\n<h2><strong>Python 3<\/strong><\/h2>\n<p>for i in range(10):<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5f02\u5e38\u5904\u7406<\/h4>\n<\/p>\n<p><p>Python 3\u5bf9\u5f02\u5e38\u5904\u7406\u8bed\u6cd5\u8fdb\u884c\u4e86\u4fee\u6539\u3002\u5728\u517c\u5bb9\u4ee3\u7801\u4e2d\uff0c\u5e94\u4f7f\u7528Python 3\u7684\u5f02\u5e38\u5904\u7406\u8bed\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801<\/p>\n<p>    pass<\/p>\n<p>except SomeException as e:<\/p>\n<p>    # \u5904\u7406\u5f02\u5e38<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u8bed\u6cd5\u5728Python 2\u548c3\u4e2d\u90fd\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5229\u7528\u5de5\u5177\u59822to3\u548csix\u5e93<\/h3>\n<\/p>\n<p><p>\u5de5\u5177\u548c\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u8f7b\u677e\u5730\u5b9e\u73b0Python 2\u548c3\u7684\u517c\u5bb9\u6027\u30022to3\u5de5\u5177\u548csix\u5e93\u662f\u5176\u4e2d\u6700\u5e38\u7528\u7684\u4e24\u4e2a\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h4>1. 2to3\u5de5\u5177<\/h4>\n<\/p>\n<p><p>2to3\u662fPython\u81ea\u5e26\u7684\u4e00\u4e2a\u5de5\u5177\uff0c\u7528\u4e8e\u5c06Python 2\u4ee3\u7801\u8f6c\u6362\u4e3aPython 3\u4ee3\u7801\u3002\u5b83\u901a\u8fc7\u9759\u6001\u5206\u6790\u4ee3\u7801\u5e76\u5e94\u7528\u4e00\u7cfb\u5217\u7684\u8f6c\u6362\u89c4\u5219\uff0c\u81ea\u52a8\u5904\u7406\u5927\u90e8\u5206\u7684\u8bed\u6cd5\u548c\u5e93\u5dee\u5f02\u3002\u4f7f\u75282to3\u5de5\u5177\u53ef\u4ee5\u5feb\u901f\u5c06\u73b0\u6709\u7684Python 2\u4ee3\u7801\u8fc1\u79fb\u5230Python 3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">2to3 -w my_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6267\u884c\u4e0a\u8ff0\u547d\u4ee4\u540e\uff0c2to3\u4f1a\u5206\u6790<code>my_script.py<\/code>\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aPython 3\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>2. six\u5e93<\/h4>\n<\/p>\n<p><p>six\u662f\u4e00\u4e2aPython\u5e93\uff0c\u4e13\u95e8\u4e3a\u7f16\u5199\u517c\u5bb9Python 2\u548c3\u7684\u4ee3\u7801\u63d0\u4f9b\u652f\u6301\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5b9e\u7528\u51fd\u6570\u548c\u5de5\u5177\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u5904\u7406\u7248\u672c\u5dee\u5f02\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0csix\u5e93\u63d0\u4f9b\u4e86<code>six.moves<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u5728\u4e24\u4e2a\u7248\u672c\u4e4b\u95f4\u7edf\u4e00\u4f7f\u7528\u67d0\u4e9b\u6a21\u5757\u7684\u5bfc\u5165\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from six.moves import range<\/p>\n<p>for i in range(10):<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>six\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u51fd\u6570\uff0c\u5982<code>six.string_types<\/code>\u7528\u4e8e\u5904\u7406\u5b57\u7b26\u4e32\u7c7b\u578b\u5dee\u5f02\uff0c<code>six.iteritems()<\/code>\u7528\u4e8e\u8fed\u4ee3\u5b57\u5178\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u907f\u514d\u4f7f\u7528Python 2\u4e2d\u7684\u8fc7\u65f6\u7279\u6027<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u517c\u5bb9\u6027\uff0c\u5f00\u53d1\u8005\u5e94\u907f\u514d\u4f7f\u7528Python 2\u4e2d\u5df2\u7ecf\u88ab\u5f03\u7528\u6216\u4e0d\u63a8\u8350\u7684\u7279\u6027\u3002\u8fd9\u4e0d\u4ec5\u6709\u52a9\u4e8e\u4ee3\u7801\u7684\u53ef\u79fb\u690d\u6027\uff0c\u8fd8\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h4>1. \u4e0d\u8981\u4f7f\u7528\u65e7\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/h4>\n<\/p>\n<p><p>Python 2\u4e2d\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u4f7f\u7528<code>%<\/code>\u7b26\u53f7\uff0c\u4f46\u8fd9\u79cd\u65b9\u5f0f\u5728Python 3\u4e2d\u5df2\u4e0d\u518d\u63a8\u8350\u3002\u5f00\u53d1\u8005\u5e94\u4f7f\u7528<code>str.format()<\/code>\u65b9\u6cd5\u6216Python 3.6\u5f15\u5165\u7684f\u5b57\u7b26\u4e32\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff09\u6765\u8fdb\u884c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Python 2 \u548c 3<\/p>\n<p>name = &quot;World&quot;<\/p>\n<p>greeting = &quot;Hello, {}!&quot;.format(name)<\/p>\n<h2><strong>Python 3.6+<\/strong><\/h2>\n<p>greeting = f&quot;Hello, {name}!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u907f\u514d\u4f7f\u7528\u65e7\u5f0f\u7c7b<\/h4>\n<\/p>\n<p><p>Python 2\u652f\u6301\u4e24\u79cd\u7c7b\u5b9a\u4e49\u65b9\u5f0f\uff1a\u65e7\u5f0f\u7c7b\u548c\u65b0\u5f0f\u7c7b\u3002\u65e7\u5f0f\u7c7b\u5728Python 3\u4e2d\u5df2\u88ab\u79fb\u9664\u3002\u5f00\u53d1\u8005\u5e94\u59cb\u7ec8\u4f7f\u7528\u65b0\u5f0f\u7c7b\uff0c\u5373\u7ee7\u627f\u81ea<code>object<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Python 2 \u548c 3<\/p>\n<p>class MyClass(object):<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528\u65b0\u7684\u8f93\u5165\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5728Python 2\u4e2d\uff0c<code>input()<\/code>\u51fd\u6570\u4f1a\u6267\u884c\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u4f5c\u4e3aPython\u4ee3\u7801\uff0c\u8fd9\u5b58\u5728\u5b89\u5168\u98ce\u9669\u3002\u5728\u517c\u5bb9\u4ee3\u7801\u4e2d\uff0c\u5e94\u4f7f\u7528<code>raw_input()<\/code>\uff08Python 2\uff09\u548c<code>input()<\/code>\uff08Python 3\uff09\u6765\u8fdb\u884c\u8f93\u5165\uff0c\u6216\u8005\u4f7f\u7528six\u5e93\u4e2d\u7684<code>six.moves.input()<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u517c\u5bb9Python 2\u548c3\u7684\u4ee3\u7801\u5f00\u53d1\u9700\u8981\u4ed4\u7ec6\u89c4\u5212\u548c\u6267\u884c\u3002\u901a\u8fc7\u4f7f\u7528__future__\u6a21\u5757\u3001\u73b0\u4ee3\u5316\u7684\u7f16\u7801\u5b9e\u8df5\u3001\u5de5\u5177\u59822to3\u548csix\u5e93\uff0c\u4ee5\u53ca\u907f\u514d\u4f7f\u7528\u8fc7\u65f6\u7279\u6027\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u7f16\u5199\u51fa\u5728\u4e24\u4e2a\u7248\u672c\u4e2d\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c\u7684\u4ee3\u7801\u3002\u8fd9\u4e0d\u4ec5\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u79fb\u690d\u6027\uff0c\u8fd8\u80fd\u786e\u4fdd\u4ee3\u7801\u7684\u957f\u671f\u53ef\u7ef4\u62a4\u6027\u3002\u5728\u672a\u6765\u7684\u5f00\u53d1\u4e2d\uff0c\u968f\u7740Python 2\u7684\u9010\u6e10\u6dd8\u6c70\uff0c\u5f00\u53d1\u8005\u5e94\u5c3d\u91cf\u5411Python 3\u8fc7\u6e21\uff0c\u4ee5\u5145\u5206\u5229\u7528\u5176\u5e26\u6765\u7684\u5404\u79cd\u6539\u8fdb\u548c\u65b0\u7279\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728\u9879\u76ee\u4e2d\u5b9e\u73b0Python 2\u548cPython 3\u7684\u517c\u5bb9\u6027\uff1f<\/strong><br \/>\u5728\u9879\u76ee\u4e2d\u5b9e\u73b0Python 2\u548cPython 3\u7684\u517c\u5bb9\u6027\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>__future__<\/code>\u6a21\u5757\u6765\u5f15\u5165Python 3\u7684\u7279\u6027\uff0c\u540c\u65f6\u907f\u514d\u4f7f\u7528Python 2\u7279\u6709\u7684\u8bed\u6cd5\u3002\u786e\u4fdd\u4ee3\u7801\u4e2d\u4f7f\u7528\u7684\u662f\u4e24\u4e2a\u7248\u672c\u90fd\u652f\u6301\u7684\u6807\u51c6\u5e93\u548c\u8bed\u6cd5\uff0c\u5e76\u8003\u8651\u4f7f\u7528<code>six<\/code>\u6216<code>future<\/code>\u7b49\u5e93\u6765\u5904\u7406\u8de8\u7248\u672c\u7684\u5dee\u5f02\u3002\u6b64\u5916\uff0c\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u6765\u9a8c\u8bc1\u4ee3\u7801\u5728\u4e24\u4e2a\u7248\u672c\u4e0b\u7684\u884c\u4e3a\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<p><strong>\u5728Python 2\u548cPython 3\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u7684\u5dee\u5f02\uff1f<\/strong><br \/>\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u5728Python 2\u548cPython 3\u4e2d\u6709\u663e\u8457\u7684\u5dee\u5f02\u3002\u5728Python 2\u4e2d\uff0c\u5b57\u7b26\u4e32\u9ed8\u8ba4\u662f\u5b57\u8282\u7c7b\u578b\uff0c\u800c\u5728Python 3\u4e2d\uff0c\u5b57\u7b26\u4e32\u662fUnicode\u3002\u4e3a\u4e86\u517c\u5bb9\u6027\uff0c\u5efa\u8bae\u5728\u4ee3\u7801\u4e2d\u660e\u786e\u4f7f\u7528<code>str<\/code>\u548c<code>bytes<\/code>\u7c7b\u578b\u3002\u5bf9\u4e8e\u5b57\u7b26\u4e32\u7684\u5904\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528<code>.encode()<\/code>\u548c<code>.decode()<\/code>\u65b9\u6cd5\u6765\u8f6c\u6362\u5b83\u4eec\uff0c\u786e\u4fdd\u5728\u5904\u7406\u6587\u672c\u6570\u636e\u65f6\u80fd\u591f\u6b63\u786e\u5904\u7406\u5b57\u7b26\u7f16\u7801\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u68c0\u6d4bPython\u4ee3\u7801\u7684\u517c\u5bb9\u6027\u95ee\u9898\uff1f<\/strong><br \/>\u6709\u591a\u79cd\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u68c0\u6d4b\u548c\u89e3\u51b3Python\u4ee3\u7801\u7684\u517c\u5bb9\u6027\u95ee\u9898\u3002<code>2to3<\/code>\u662f\u4e00\u4e2a\u5b98\u65b9\u63d0\u4f9b\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u81ea\u52a8\u5c06Python 2\u4ee3\u7801\u8f6c\u6362\u4e3aPython 3\u4ee3\u7801\u3002<code>futurize<\/code>\u548c<code>modernize<\/code>\u662f\u5176\u4ed6\u4e24\u4e2a\u5e38\u7528\u7684\u5de5\u5177\uff0c\u5b83\u4eec\u53ef\u4ee5\u5e2e\u52a9\u5728\u4fdd\u6301Python 2\u517c\u5bb9\u6027\u7684\u540c\u65f6\uff0c\u4f7f\u7528Python 3\u7684\u7279\u6027\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>pytest<\/code>\u7b49\u6d4b\u8bd5\u6846\u67b6\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\uff0c\u80fd\u591f\u786e\u4fdd\u4ee3\u7801\u5728\u4e24\u79cd\u7248\u672c\u4e0b\u5747\u80fd\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python 3\u517c\u5bb9Python 2\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528__future__\u6a21\u5757\u3001\u4f7f\u7528\u73b0\u4ee3\u5316\u7684\u7f16\u7801\u5b9e\u8df5\u3001\u5229\u7528\u5de5\u5177\u5982 [&hellip;]","protected":false},"author":3,"featured_media":947993,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/947990"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=947990"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/947990\/revisions"}],"predecessor-version":[{"id":947994,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/947990\/revisions\/947994"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/947993"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=947990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=947990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=947990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}