{"id":1071640,"date":"2025-01-08T11:09:02","date_gmt":"2025-01-08T03:09:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1071640.html"},"modified":"2025-01-08T11:09:04","modified_gmt":"2025-01-08T03:09:04","slug":"python2%e5%a6%82%e4%bd%95%e5%8d%87%e7%ba%a7%e5%88%b03-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1071640.html","title":{"rendered":"python2\u5982\u4f55\u5347\u7ea7\u52303"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102103\/73dd3a27-1dd0-4748-b7fc-92bb818870bc.webp\" alt=\"python2\u5982\u4f55\u5347\u7ea7\u52303\" \/><\/p>\n<p><p> <strong>\u8981\u5c06Python 2\u5347\u7ea7\u5230Python 3\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u6b65\uff1a\u5b89\u88c5Python 3\u3001\u66f4\u65b0\u4ee3\u7801\u4ee5\u517c\u5bb9Python 3\u3001\u4f7f\u7528\u5de5\u5177\u81ea\u52a8\u8f6c\u6362\u4ee3\u7801\u3001\u6d4b\u8bd5\u548c\u8c03\u8bd5\u3002\u91cd\u70b9\u662f\u66f4\u65b0\u4ee3\u7801\u4ee5\u517c\u5bb9Python 3\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5Python 3<\/p>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5728\u7cfb\u7edf\u4e2d\u5b89\u88c5Python 3\u3002\u53ef\u4ee5\u4ecePython\u7684\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u9002\u7528\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u7684Python 3\u7248\u672c\u5b89\u88c5\u5305\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u786e\u4fddPython 3\u7684\u53ef\u6267\u884c\u6587\u4ef6\u8def\u5f84\u5df2\u7ecf\u6dfb\u52a0\u5230\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4e2d\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u76f4\u63a5\u4f7f\u7528<code>python3<\/code>\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u66f4\u65b0\u4ee3\u7801\u4ee5\u517c\u5bb9Python 3<\/p>\n<\/p>\n<ol>\n<li><strong>\u8bed\u6cd5\u5dee\u5f02<\/strong><\/li>\n<\/ol>\n<p><p>Python 2\u548cPython 3\u5728\u8bed\u6cd5\u4e0a\u6709\u4e00\u4e9b\u91cd\u8981\u7684\u5dee\u5f02\uff0c\u4f8b\u5982print\u8bed\u53e5\u3001\u6574\u6570\u9664\u6cd5\u7b49\u3002\u9700\u8981\u9010\u884c\u68c0\u67e5\u4ee3\u7801\uff0c\u786e\u4fdd\u6240\u6709\u8bed\u6cd5\u90fd\u517c\u5bb9Python 3\u3002\u4f8b\u5982\uff0cPython 2\u4e2d\u7684<code>print &quot;Hello&quot;<\/code>\u5728Python 3\u4e2d\u9700\u8981\u6539\u4e3a<code>print(&quot;Hello&quot;)<\/code>\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5e93\u7684\u53d8\u5316<\/strong><\/li>\n<\/ol>\n<p><p>\u4e00\u4e9bPython 2\u4e2d\u7684\u6807\u51c6\u5e93\u5728Python 3\u4e2d\u5df2\u7ecf\u88ab\u79fb\u9664\u6216\u66ff\u6362\u3002\u4f8b\u5982\uff0c<code>urllib2<\/code>\u5728Python 3\u4e2d\u88ab\u5206\u6210\u4e86<code>urllib.request<\/code>\u548c<code>urllib.error<\/code>\u3002\u9700\u8981\u6839\u636e\u5b98\u65b9\u6587\u6863\u6216\u4f7f\u7528\u81ea\u52a8\u5316\u5de5\u5177\u6765\u66f4\u65b0\u8fd9\u4e9b\u5e93\u7684\u5f15\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5de5\u5177\u81ea\u52a8\u8f6c\u6362\u4ee3\u7801<\/p>\n<\/p>\n<ol>\n<li><strong>2to3\u5de5\u5177<\/strong><\/li>\n<\/ol>\n<p><p>Python\u5b98\u65b9\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a<code>2to3<\/code>\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u81ea\u52a8\u5c06Python 2\u4ee3\u7801\u8f6c\u6362\u4e3aPython 3\u4ee3\u7801\u3002\u4f7f\u7528\u8be5\u5de5\u5177\u53ef\u4ee5\u8282\u7701\u5927\u91cf\u624b\u52a8\u4fee\u6539\u4ee3\u7801\u7684\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code>```sh<\/p>\n<p>2to3 -w your_script.py<\/p>\n<p>```<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u547d\u4ee4\u4f1a\u81ea\u52a8\u4fee\u6539<code>your_script.py<\/code>\u6587\u4ef6\uff0c\u5c06\u5176\u4e2d\u7684Python 2\u4ee3\u7801\u8f6c\u6362\u4e3aPython 3\u4ee3\u7801\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5176\u4ed6\u5de5\u5177<\/strong><\/li>\n<\/ol>\n<p><p>\u9664\u4e86<code>2to3<\/code>\uff0c\u8fd8\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u8fdb\u884c\u4ee3\u7801\u8f6c\u6362\u548c\u517c\u5bb9\u6027\u68c0\u67e5\uff0c\u4f8b\u5982<code>futurize<\/code>\u548c<code>modernize<\/code>\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u63d0\u4f9b\u989d\u5916\u7684\u529f\u80fd\u548c\u66f4\u7ec6\u81f4\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u6d4b\u8bd5\u548c\u8c03\u8bd5<\/p>\n<\/p>\n<p><p>\u8f6c\u6362\u5b8c\u6210\u540e\uff0c\u9700\u8981\u8fdb\u884c\u5168\u9762\u7684\u6d4b\u8bd5\u548c\u8c03\u8bd5\uff0c\u786e\u4fdd\u4ee3\u7801\u5728Python 3\u4e0b\u80fd\u591f\u6b63\u5e38\u8fd0\u884c\u3002\u5efa\u8bae\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\uff0c\u5e76\u4f7f\u7528\u6d4b\u8bd5\u6846\u67b6\uff08\u5982<code>unittest<\/code>\u6216<code>pytest<\/code>\uff09\u6765\u81ea\u52a8\u5316\u6d4b\u8bd5\u6d41\u7a0b\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5355\u5143\u6d4b\u8bd5<\/strong><\/li>\n<\/ol>\n<p><p>\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u53ef\u4ee5\u5e2e\u52a9\u5feb\u901f\u53d1\u73b0\u548c\u4fee\u590d\u4ee3\u7801\u4e2d\u7684\u95ee\u9898\u3002\u901a\u8fc7\u9010\u6b65\u8fd0\u884c\u6d4b\u8bd5\uff0c\u786e\u4fdd\u6bcf\u4e2a\u6a21\u5757\u90fd\u80fd\u5728Python 3\u4e0b\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8c03\u8bd5<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177\uff08\u5982<code>pdb<\/code>\uff09\u6765\u9010\u6b65\u68c0\u67e5\u4ee3\u7801\uff0c\u627e\u51fa\u5e76\u4fee\u590d\u6f5c\u5728\u7684\u517c\u5bb9\u6027\u95ee\u9898\u3002\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u5229\u7528\u65e5\u5fd7\u8bb0\u5f55\u548c\u65ad\u70b9\u6765\u8ddf\u8e2a\u4ee3\u7801\u6267\u884c\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5347\u7ea7Python\u7248\u672c\u662f\u4e00\u4e2a\u590d\u6742\u7684\u8fc7\u7a0b\uff0c\u9700\u8981\u4ed4\u7ec6\u68c0\u67e5\u548c\u4fee\u6539\u4ee3\u7801\uff0c\u4ee5\u786e\u4fdd\u517c\u5bb9\u6027\u548c\u7a33\u5b9a\u6027\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5Python 3<\/p>\n<\/p>\n<ol>\n<li><strong>\u4e0b\u8f7d\u548c\u5b89\u88c5<\/strong><\/li>\n<\/ol>\n<p><p>\u524d\u5f80<a href=\"https:\/\/www.python.org\/downloads\/\">Python\u5b98\u65b9\u7f51\u7ad9<\/a>\uff0c\u4e0b\u8f7d\u9002\u7528\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u7684\u6700\u65b0Python 3\u7248\u672c\u3002\u6309\u7167\u5b98\u7f51\u63d0\u4f9b\u7684\u5b89\u88c5\u6307\u5357\u8fdb\u884c\u5b89\u88c5\u3002\u5b89\u88c5\u8fc7\u7a0b\u4e2d\uff0c\u8bf7\u786e\u4fdd\u52fe\u9009\u201cAdd Python to PATH\u201d\u9009\u9879\uff0c\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528Python 3\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u9a8c\u8bc1\u5b89\u88c5<\/strong><\/li>\n<\/ol>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff0c\u9a8c\u8bc1Python 3\u662f\u5426\u5b89\u88c5\u6210\u529f\uff1a<\/p>\n<\/p>\n<p><pre><code>```sh<\/p>\n<p>python3 --version<\/p>\n<p>```<\/p>\n<p>\u5982\u679c\u8f93\u51fa\u7c7b\u4f3c\u4e8e`Python 3.x.x`\uff0c\u5219\u8868\u793a\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u66f4\u65b0\u4ee3\u7801\u4ee5\u517c\u5bb9Python 3<\/p>\n<\/p>\n<ol>\n<li><strong>\u8bed\u6cd5\u5dee\u5f02<\/strong><\/li>\n<\/ol>\n<p><p>Python 2\u548cPython 3\u5728\u8bed\u6cd5\u4e0a\u6709\u4e00\u4e9b\u663e\u8457\u7684\u5dee\u5f02\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u53d8\u5316\uff1a<\/p>\n<\/p>\n<p><pre><code>- &lt;strong&gt;print\u8bed\u53e5&lt;\/strong&gt;<\/p>\n<p>    \u5728Python 2\u4e2d\uff0c`print`\u662f\u4e00\u4e2a\u8bed\u53e5\uff0c\u800c\u5728Python 3\u4e2d\uff0c`print`\u662f\u4e00\u4e2a\u51fd\u6570\u3002\u56e0\u6b64\uff0c\u9700\u8981\u5c06`print &quot;Hello&quot;`\u6539\u4e3a`print(&quot;Hello&quot;)`\u3002<\/p>\n<p>- &lt;strong&gt;\u6574\u6570\u9664\u6cd5&lt;\/strong&gt;<\/p>\n<p>    \u5728Python 2\u4e2d\uff0c\u6574\u6570\u9664\u6cd5\u4f1a\u5411\u4e0b\u53d6\u6574\uff0c\u4f8b\u5982`5 \/ 2`\u7ed3\u679c\u4e3a`2`\u3002\u5728Python 3\u4e2d\uff0c\u6574\u6570\u9664\u6cd5\u4f1a\u8fd4\u56de\u6d6e\u70b9\u6570\uff0c\u4f8b\u5982`5 \/ 2`\u7ed3\u679c\u4e3a`2.5`\u3002\u5982\u679c\u9700\u8981\u6574\u6570\u9664\u6cd5\uff0c\u8bf7\u4f7f\u7528`\/\/`\u8fd0\u7b97\u7b26\u3002<\/p>\n<p>    ```python<\/p>\n<p>    # Python 2<\/p>\n<p>    result = 5 \/ 2  # \u7ed3\u679c\u4e3a2<\/p>\n<p>    # Python 3<\/p>\n<p>    result = 5 \/\/ 2  # \u7ed3\u679c\u4e3a2<\/p>\n<p>    ```<\/p>\n<p>- &lt;strong&gt;unicode\u548cstr\u7c7b\u578b&lt;\/strong&gt;<\/p>\n<p>    \u5728Python 2\u4e2d\uff0c`str`\u8868\u793a\u5b57\u8282\u4e32\uff0c`unicode`\u8868\u793aUnicode\u5b57\u7b26\u4e32\u3002\u5728Python 3\u4e2d\uff0c`str`\u8868\u793aUnicode\u5b57\u7b26\u4e32\uff0c`bytes`\u8868\u793a\u5b57\u8282\u4e32\u3002\u56e0\u6b64\uff0c\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n<p>    ```python<\/p>\n<p>    # Python 2<\/p>\n<p>    u_str = unicode(&#39;Hello&#39;, &#39;utf-8&#39;)<\/p>\n<p>    # Python 3<\/p>\n<p>    u_str = &#39;Hello&#39;<\/p>\n<p>    ```<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5e93\u7684\u53d8\u5316<\/strong><\/li>\n<\/ol>\n<p><p>\u4e00\u4e9bPython 2\u4e2d\u7684\u6807\u51c6\u5e93\u5728Python 3\u4e2d\u5df2\u7ecf\u88ab\u79fb\u9664\u6216\u66ff\u6362\u3002\u4f8b\u5982\uff0c<code>urllib2<\/code>\u5728Python 3\u4e2d\u88ab\u5206\u6210\u4e86<code>urllib.request<\/code>\u548c<code>urllib.error<\/code>\u3002\u9700\u8981\u6839\u636e\u5b98\u65b9\u6587\u6863\u6216\u4f7f\u7528\u81ea\u52a8\u5316\u5de5\u5177\u6765\u66f4\u65b0\u8fd9\u4e9b\u5e93\u7684\u5f15\u7528\u3002<\/p>\n<\/p>\n<p><pre><code>```python<\/p>\n<h2><strong>Python 2<\/strong><\/h2>\n<p>import urllib2<\/p>\n<p>response = urllib2.urlopen(&#39;http:\/\/example.com&#39;)<\/p>\n<h2><strong>Python 3<\/strong><\/h2>\n<p>import urllib.request<\/p>\n<p>response = urllib.request.urlopen(&#39;http:\/\/example.com&#39;)<\/p>\n<p>```<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5de5\u5177\u81ea\u52a8\u8f6c\u6362\u4ee3\u7801<\/p>\n<\/p>\n<ol>\n<li><strong>2to3\u5de5\u5177<\/strong><\/li>\n<\/ol>\n<p><p>Python\u5b98\u65b9\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a<code>2to3<\/code>\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u81ea\u52a8\u5c06Python 2\u4ee3\u7801\u8f6c\u6362\u4e3aPython 3\u4ee3\u7801\u3002\u4f7f\u7528\u8be5\u5de5\u5177\u53ef\u4ee5\u8282\u7701\u5927\u91cf\u624b\u52a8\u4fee\u6539\u4ee3\u7801\u7684\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code>```sh<\/p>\n<p>2to3 -w your_script.py<\/p>\n<p>```<\/p>\n<p>\u4e0a\u9762\u7684\u547d\u4ee4\u4f1a\u81ea\u52a8\u4fee\u6539`your_script.py`\u6587\u4ef6\uff0c\u5c06\u5176\u4e2d\u7684Python 2\u4ee3\u7801\u8f6c\u6362\u4e3aPython 3\u4ee3\u7801\u3002<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5176\u4ed6\u5de5\u5177<\/strong><\/li>\n<\/ol>\n<p><p>\u9664\u4e86<code>2to3<\/code>\uff0c\u8fd8\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u8fdb\u884c\u4ee3\u7801\u8f6c\u6362\u548c\u517c\u5bb9\u6027\u68c0\u67e5\uff0c\u4f8b\u5982<code>futurize<\/code>\u548c<code>modernize<\/code>\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u63d0\u4f9b\u989d\u5916\u7684\u529f\u80fd\u548c\u66f4\u7ec6\u81f4\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code>- &lt;strong&gt;futurize&lt;\/strong&gt;<\/p>\n<p>    `futurize`\u5de5\u5177\u53ef\u4ee5\u5c06Python 2\u4ee3\u7801\u8f6c\u6362\u4e3a\u540c\u65f6\u517c\u5bb9Python 2\u548cPython 3\u7684\u4ee3\u7801\u3002<\/p>\n<p>    ```sh<\/p>\n<p>    pip install future<\/p>\n<p>    futurize -w your_script.py<\/p>\n<p>    ```<\/p>\n<p>- &lt;strong&gt;modernize&lt;\/strong&gt;<\/p>\n<p>    `modernize`\u5de5\u5177\u53ef\u4ee5\u5c06Python 2\u4ee3\u7801\u8f6c\u6362\u4e3aPython 3\u4ee3\u7801\uff0c\u5e76\u4fdd\u6301\u5bf9Python 2\u7684\u517c\u5bb9\u6027\u3002<\/p>\n<p>    ```sh<\/p>\n<p>    pip install modernize<\/p>\n<p>    python-modernize -w your_script.py<\/p>\n<p>    ```<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u6d4b\u8bd5\u548c\u8c03\u8bd5<\/p>\n<\/p>\n<p><p>\u8f6c\u6362\u5b8c\u6210\u540e\uff0c\u9700\u8981\u8fdb\u884c\u5168\u9762\u7684\u6d4b\u8bd5\u548c\u8c03\u8bd5\uff0c\u786e\u4fdd\u4ee3\u7801\u5728Python 3\u4e0b\u80fd\u591f\u6b63\u5e38\u8fd0\u884c\u3002\u5efa\u8bae\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\uff0c\u5e76\u4f7f\u7528\u6d4b\u8bd5\u6846\u67b6\uff08\u5982<code>unittest<\/code>\u6216<code>pytest<\/code>\uff09\u6765\u81ea\u52a8\u5316\u6d4b\u8bd5\u6d41\u7a0b\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5355\u5143\u6d4b\u8bd5<\/strong><\/li>\n<\/ol>\n<p><p>\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u53ef\u4ee5\u5e2e\u52a9\u5feb\u901f\u53d1\u73b0\u548c\u4fee\u590d\u4ee3\u7801\u4e2d\u7684\u95ee\u9898\u3002\u901a\u8fc7\u9010\u6b65\u8fd0\u884c\u6d4b\u8bd5\uff0c\u786e\u4fdd\u6bcf\u4e2a\u6a21\u5757\u90fd\u80fd\u5728Python 3\u4e0b\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code>```python<\/p>\n<p>import unittest<\/p>\n<p>class TestExample(unittest.TestCase):<\/p>\n<p>    def test_addition(self):<\/p>\n<p>        self.assertEqual(1 + 1, 2)<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    unittest.main()<\/p>\n<p>```<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8c03\u8bd5<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177\uff08\u5982<code>pdb<\/code>\uff09\u6765\u9010\u6b65\u68c0\u67e5\u4ee3\u7801\uff0c\u627e\u51fa\u5e76\u4fee\u590d\u6f5c\u5728\u7684\u517c\u5bb9\u6027\u95ee\u9898\u3002\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u5229\u7528\u65e5\u5fd7\u8bb0\u5f55\u548c\u65ad\u70b9\u6765\u8ddf\u8e2a\u4ee3\u7801\u6267\u884c\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code>```python<\/p>\n<p>import pdb<\/p>\n<p>def example_function():<\/p>\n<p>    a = 1<\/p>\n<p>    b = 2<\/p>\n<p>    pdb.set_trace()<\/p>\n<p>    return a + b<\/p>\n<p>example_function()<\/p>\n<p>```<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5347\u7ea7Python\u7248\u672c\u662f\u4e00\u4e2a\u590d\u6742\u7684\u8fc7\u7a0b\uff0c\u9700\u8981\u4ed4\u7ec6\u68c0\u67e5\u548c\u4fee\u6539\u4ee3\u7801\uff0c\u4ee5\u786e\u4fdd\u517c\u5bb9\u6027\u548c\u7a33\u5b9a\u6027\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u9879\u76ee\u7ed3\u6784<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u5347\u7ea7\u8fc7\u7a0b\u4e2d\uff0c\u5efa\u8bae\u4fdd\u6301\u9879\u76ee\u7684\u826f\u597d\u7ed3\u6784\uff0c\u4fbf\u4e8e\u7ba1\u7406\u548c\u7ef4\u62a4\u3002\u786e\u4fdd\u4ee3\u7801\u3001\u6d4b\u8bd5\u548c\u914d\u7f6e\u6587\u4ef6\u5206\u79bb\uff0c\u4f7f\u7528\u865a\u62df\u73af\u5883\u7ba1\u7406\u4f9d\u8d56\u5173\u7cfb\u3002<\/p>\n<\/p>\n<p><pre><code>```sh<\/p>\n<p>project\/<\/p>\n<p>\u251c\u2500\u2500 src\/<\/p>\n<p>\u2502   \u251c\u2500\u2500 module1.py<\/p>\n<p>\u2502   \u2514\u2500\u2500 module2.py<\/p>\n<p>\u251c\u2500\u2500 tests\/<\/p>\n<p>\u2502   \u251c\u2500\u2500 test_module1.py<\/p>\n<p>\u2502   \u2514\u2500\u2500 test_module2.py<\/p>\n<p>\u251c\u2500\u2500 requirements.txt<\/p>\n<p>\u2514\u2500\u2500 README.md<\/p>\n<p>```<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f9d\u8d56\u7ba1\u7406<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u521b\u5efa<code>requirements.txt<\/code>\u6587\u4ef6\uff0c\u5217\u51fa\u6240\u6709\u4f9d\u8d56\u7684\u7b2c\u4e09\u65b9\u5e93\u3002\u4f7f\u7528\u865a\u62df\u73af\u5883\u7ba1\u7406\u4f9d\u8d56\u5173\u7cfb\uff0c\u53ef\u4ee5\u907f\u514d\u4e0e\u7cfb\u7edf\u73af\u5883\u7684\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><pre><code>```sh<\/p>\n<p>pip install virtualenv<\/p>\n<p>virtualenv venv<\/p>\n<p>source venv\/bin\/activate<\/p>\n<p>pip install -r requirements.txt<\/p>\n<p>```<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u7248\u672c\u63a7\u5236<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u8fdb\u884c\u4ee3\u7801\u8f6c\u6362\u548c\u5347\u7ea7\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u7248\u672c\u63a7\u5236\u7cfb\u7edf\uff08\u5982Git\uff09\u6765\u8ddf\u8e2a\u4ee3\u7801\u7684\u53d8\u5316\u3002\u53ef\u4ee5\u5728\u5347\u7ea7\u524d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5206\u652f\uff0c\u8fdb\u884c\u72ec\u7acb\u5f00\u53d1\u548c\u6d4b\u8bd5\uff0c\u786e\u4fdd\u4e3b\u5206\u652f\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<p><pre><code>```sh<\/p>\n<p>git checkout -b upgrade-to-python3<\/p>\n<p>```<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u6587\u6863\u66f4\u65b0<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u5347\u7ea7\u8fc7\u7a0b\u4e2d\uff0c\u53ca\u65f6\u66f4\u65b0\u9879\u76ee\u7684\u6587\u6863\uff0c\u8bb0\u5f55\u6240\u6709\u7684\u53d8\u5316\u548c\u6ce8\u610f\u4e8b\u9879\u3002\u53ef\u4ee5\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u521b\u5efa<code>CHANGELOG.md<\/code>\u6587\u4ef6\uff0c\u8bb0\u5f55\u6bcf\u4e2a\u7248\u672c\u7684\u53d8\u66f4\u5386\u53f2\u3002<\/p>\n<\/p>\n<p><pre><code>```markdown<\/p>\n<h2><strong>Changelog<\/strong><\/h2>\n<p>## [1.0.0] - 2023-10-15<\/p>\n<p>### Added<\/p>\n<p>- Initial release with Python 3 support.<\/p>\n<p>### Changed<\/p>\n<p>- Updated print statements to Python 3 syntax.<\/p>\n<p>- Replaced `urllib2` with `urllib.request`.<\/p>\n<p>```<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"5\">\n<li><strong>\u6301\u7eed\u96c6\u6210<\/strong><\/li>\n<\/ol>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u4ee3\u7801\u5728\u4e0d\u540c\u73af\u5883\u4e0b\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c\uff0c\u5efa\u8bae\u914d\u7f6e\u6301\u7eed\u96c6\u6210\uff08CI\uff09\u5de5\u5177\uff08\u5982Travis CI\u3001CircleCI\u6216GitHub Actions\uff09\uff0c\u81ea\u52a8\u5316\u6d4b\u8bd5\u548c\u90e8\u7f72\u6d41\u7a0b\u3002\u7f16\u5199CI\u914d\u7f6e\u6587\u4ef6\uff0c\u5b9a\u4e49\u6d4b\u8bd5\u548c\u6784\u5efa\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><pre><code>```yaml<\/p>\n<h2><strong>.travis.yml<\/strong><\/h2>\n<p>language: python<\/p>\n<p>python:<\/p>\n<p>  - &quot;3.8&quot;<\/p>\n<p>install:<\/p>\n<p>  - pip install -r requirements.txt<\/p>\n<p>script:<\/p>\n<p>  - pytest<\/p>\n<p>```<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u6ce8\u610f\u4e8b\u9879\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5c06Python 2\u9879\u76ee\u5347\u7ea7\u5230Python 3\uff0c\u5e76\u786e\u4fdd\u4ee3\u7801\u7684\u517c\u5bb9\u6027\u548c\u7a33\u5b9a\u6027\u3002\u5347\u7ea7\u8fc7\u7a0b\u4e2d\u9700\u8981\u8010\u5fc3\u548c\u7ec6\u81f4\u7684\u68c0\u67e5\uff0c\u4f46\u6700\u7ec8\u4f1a\u5e26\u6765\u66f4\u597d\u7684\u6027\u80fd\u548c\u66f4\u957f\u7684\u652f\u6301\u5468\u671f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5224\u65ad\u6211\u7684\u9879\u76ee\u662f\u5426\u9002\u5408\u5347\u7ea7\u5230Python 3\uff1f<\/strong><br \/>\u5728\u51b3\u5b9a\u5347\u7ea7\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u68c0\u67e5\u73b0\u6709\u4ee3\u7801\u662f\u5426\u4f9d\u8d56\u4e8ePython 2\u7279\u6709\u7684\u529f\u80fd\u6216\u5e93\u3002\u67e5\u770b\u9879\u76ee\u7684\u4f9d\u8d56\u9879\uff0c\u786e\u4fdd\u6240\u6709\u4f7f\u7528\u7684\u5e93\u5728Python 3\u4e2d\u90fd\u6709\u652f\u6301\u3002\u4f7f\u7528\u5de5\u5177\u5982<code>2to3<\/code>\u53ef\u4ee5\u5e2e\u52a9\u5206\u6790\u4ee3\u7801\u4e2d\u7684\u4e0d\u517c\u5bb9\u90e8\u5206\u3002\u6b64\u5916\uff0c\u8fd0\u884c\u6d4b\u8bd5\u7528\u4f8b\u5e76\u786e\u4fdd\u5b83\u4eec\u5728Python 2\u4e2d\u80fd\u6b63\u5e38\u901a\u8fc7\u4e5f\u5f88\u91cd\u8981\uff0c\u8fd9\u6837\u60a8\u5728\u5347\u7ea7\u540e\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u8bc6\u522b\u95ee\u9898\u3002<\/p>\n<p><strong>\u5728\u5347\u7ea7\u8fc7\u7a0b\u4e2d\u5982\u4f55\u5904\u7406Python 2\u4e0ePython 3\u7684\u517c\u5bb9\u6027\u95ee\u9898\uff1f<\/strong><br \/>\u4f7f\u7528<code>__future__<\/code>\u6a21\u5757\u53ef\u4ee5\u5728Python 2\u4e2d\u5f15\u5165Python 3\u7684\u4e00\u4e9b\u7279\u6027\uff0c\u6bd4\u5982<code>print_function<\/code>\u548c<code>division<\/code>\u3002\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u5728\u9010\u6b65\u8fc1\u79fb\u7684\u8fc7\u7a0b\u4e2d\uff0c\u786e\u4fdd\u4ee3\u7801\u5728\u4e24\u4e2a\u7248\u672c\u4e2d\u90fd\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002\u540c\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>six<\/code>\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u517c\u5bb9\u5c42\uff0c\u5e2e\u52a9\u60a8\u5728Python 2\u548cPython 3\u4e4b\u95f4\u65e0\u7f1d\u8fc7\u6e21\u3002<\/p>\n<p><strong>\u5347\u7ea7\u540e\u5982\u4f55\u9a8c\u8bc1\u6211\u7684\u4ee3\u7801\u5728Python 3\u4e2d\u6b63\u5e38\u8fd0\u884c\uff1f<\/strong><br \/>\u5728\u5b8c\u6210\u4ee3\u7801\u8fc1\u79fb\u540e\uff0c\u5efa\u8bae\u6267\u884c\u5168\u9762\u7684\u5355\u5143\u6d4b\u8bd5\u4ee5\u786e\u4fdd\u6240\u6709\u529f\u80fd\u6b63\u5e38\u3002\u53ef\u4ee5\u5229\u7528pytest\u6216unittest\u7b49\u6d4b\u8bd5\u6846\u67b6\u6765\u6267\u884c\u8fd9\u4e9b\u6d4b\u8bd5\u3002\u6b64\u5916\uff0c\u624b\u52a8\u6d4b\u8bd5\u5173\u952e\u529f\u80fd\u4e5f\u662f\u5fc5\u8981\u7684\uff0c\u7279\u522b\u662f\u90a3\u4e9b\u4e0e\u7528\u6237\u4ea4\u4e92\u7684\u90e8\u5206\u3002\u76d1\u63a7\u65e5\u5fd7\u548c\u9519\u8bef\u4fe1\u606f\u4e5f\u662f\u68c0\u9a8c\u4ee3\u7801\u5728\u65b0\u73af\u5883\u4e2d\u8868\u73b0\u7684\u91cd\u8981\u624b\u6bb5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06Python 2\u5347\u7ea7\u5230Python 3\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u6b65\uff1a\u5b89\u88c5Python 3\u3001\u66f4\u65b0\u4ee3\u7801\u4ee5\u517c\u5bb9Python [&hellip;]","protected":false},"author":3,"featured_media":1071647,"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\/1071640"}],"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=1071640"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1071640\/revisions"}],"predecessor-version":[{"id":1071648,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1071640\/revisions\/1071648"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1071647"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1071640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1071640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1071640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}