{"id":1016472,"date":"2024-12-27T12:16:46","date_gmt":"2024-12-27T04:16:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1016472.html"},"modified":"2024-12-27T12:16:48","modified_gmt":"2024-12-27T04:16:48","slug":"%e5%a6%82%e4%bd%95%e5%ae%89%e8%a3%85%e4%bd%bf%e7%94%a8python-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1016472.html","title":{"rendered":"\u5982\u4f55\u5b89\u88c5\u4f7f\u7528python 2"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100015\/0c06470f-6ed6-45c7-8fdf-037b468f9412.webp\" alt=\"\u5982\u4f55\u5b89\u88c5\u4f7f\u7528python 2\" \/><\/p>\n<p><p> <strong>\u5b89\u88c5\u548c\u4f7f\u7528Python 2\u9700\u8981\u8003\u8651\u51e0\u4e2a\u5173\u952e\u6b65\u9aa4\uff1a\u5b89\u88c5Python 2\u89e3\u91ca\u5668\u3001\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u3001\u5b89\u88c5\u5305\u7ba1\u7406\u5de5\u5177pip\u3001\u521b\u5efa\u548c\u7ba1\u7406\u865a\u62df\u73af\u5883\u3002<\/strong> \u5728\u8fd9\u4e9b\u6b65\u9aa4\u4e2d\uff0c\u786e\u4fdd\u5b89\u88c5pip\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u4f60\u5b89\u88c5\u548c\u7ba1\u7406Python 2\u7684\u7b2c\u4e09\u65b9\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5Python 2\u89e3\u91ca\u5668<\/h3>\n<\/p>\n<p><p>Python 2\u5df2\u7ecf\u505c\u6b62\u5b98\u65b9\u652f\u6301\uff0c\u4f46\u4ecd\u53ef\u4ee5\u5728\u5f88\u591a\u5730\u65b9\u4e0b\u8f7d\u548c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4e0b\u8f7dPython 2\uff1a<\/strong><br \/>\u524d\u5f80Python\u7684\u5b98\u65b9\u7f51\u7ad9\uff08<a href=\"https:\/\/www.python.org\/downloads\/release\/python-2718\/%EF%BC%89%E4%B8%8B%E8%BD%BDPython\">https:\/\/www.python.org\/downloads\/release\/python-2718\/\uff09\u4e0b\u8f7dPython<\/a> 2.7.18\uff0c\u8fd9\u662f\u6700\u540e\u4e00\u4e2aPython 2\u7684\u5b98\u65b9\u7248\u672c\u3002\u9009\u62e9\u4e0e\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u517c\u5bb9\u7684\u5b89\u88c5\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5Python 2\uff1a<\/strong><br \/>\u53cc\u51fb\u4e0b\u8f7d\u7684\u5b89\u88c5\u6587\u4ef6\uff0c\u6309\u7167\u5b89\u88c5\u5411\u5bfc\u8fdb\u884c\u5b89\u88c5\u3002\u5728\u5b89\u88c5\u8fc7\u7a0b\u4e2d\uff0c\u786e\u4fdd\u9009\u4e2d\u201cAdd Python to PATH\u201d\u9009\u9879\uff0c\u8fd9\u5c06\u81ea\u52a8\u8bbe\u7f6e\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5728Windows\u4e2d\uff0c\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u6709\u52a9\u4e8e\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8f7b\u677e\u8bbf\u95eePython\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6253\u5f00\u73af\u5883\u53d8\u91cf\u8bbe\u7f6e\uff1a<\/strong><br \/>\u53f3\u952e\u70b9\u51fb\u201c\u6b64\u7535\u8111\u201d\u6216\u201c\u6211\u7684\u7535\u8111\u201d\uff0c\u9009\u62e9\u201c\u5c5e\u6027\u201d\uff0c\u7136\u540e\u70b9\u51fb\u201c\u9ad8\u7ea7\u7cfb\u7edf\u8bbe\u7f6e\u201d\uff0c\u518d\u70b9\u51fb\u201c\u73af\u5883\u53d8\u91cf\u201d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u8f91\u7cfb\u7edf\u53d8\u91cf\uff1a<\/strong><br \/>\u5728\u201c\u7cfb\u7edf\u53d8\u91cf\u201d\u4e2d\u627e\u5230\u201cPath\u201d\u53d8\u91cf\uff0c\u70b9\u51fb\u201c\u7f16\u8f91\u201d\uff0c\u7136\u540e\u5c06Python 2\u7684\u5b89\u88c5\u8def\u5f84\uff08\u4f8b\u5982C:\\Python27\uff09\u6dfb\u52a0\u5230\u8be5\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u5b89\u88c5pip\u5305\u7ba1\u7406\u5de5\u5177<\/h3>\n<\/p>\n<p><p>pip\u662fPython\u7684\u5305\u7ba1\u7406\u7cfb\u7edf\uff0c\u7528\u4e8e\u5b89\u88c5\u548c\u7ba1\u7406Python\u5305\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4e0b\u8f7dget-pip.py\uff1a<\/strong><br \/>\u4ece<a href=\"https:\/\/bootstrap.pypa.io\/pip\/2.7\/get-pip.py%E4%B8%8B%E8%BD%BDget-pip.py%E6%96%87%E4%BB%B6%E3%80%82\">https:\/\/bootstrap.pypa.io\/pip\/2.7\/get-pip.py\u4e0b\u8f7dget-pip.py\u6587\u4ef6\u3002<\/a><\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5pip\uff1a<\/strong><br \/>\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\uff0c\u5bfc\u822a\u5230\u4e0b\u8f7dget-pip.py\u7684\u76ee\u5f55\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python get-pip.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u5b89\u88c5pip\u5de5\u5177\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u521b\u5efa\u548c\u7ba1\u7406\u865a\u62df\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u865a\u62df\u73af\u5883\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u9879\u76ee\u4e4b\u95f4\u9694\u79bb\u4f9d\u8d56\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5virtualenv\uff1a<\/strong><br \/>\u4f7f\u7528pip\u5b89\u88c5virtualenv\u5de5\u5177\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install virtualenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u865a\u62df\u73af\u5883\uff1a<\/strong><br \/>\u5bfc\u822a\u5230\u4f60\u7684\u9879\u76ee\u76ee\u5f55\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">virtualenv venv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u201cvenv\u201d\u662f\u865a\u62df\u73af\u5883\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a<\/strong><br \/>\u5728Windows\u4e0a\uff0c\u8fd0\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">venv\\Scripts\\activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728macOS\u6216Linux\u4e0a\uff0c\u8fd0\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">source venv\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u865a\u62df\u73af\u5883\uff1a<\/strong><br \/>\u73b0\u5728\uff0c\u4f60\u53ef\u4ee5\u5b89\u88c5\u9879\u76ee\u6240\u9700\u7684\u5305\u800c\u4e0d\u5f71\u54cd\u5168\u5c40Python\u73af\u5883\u3002\u4f8b\u5982\uff0c\u5b89\u88c5requests\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u5f00\u53d1Python 2\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u865a\u62df\u73af\u5883\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5f00\u53d1Python 2\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efaPython\u811a\u672c\uff1a<\/strong><br \/>\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\u521b\u5efaPython\u811a\u672c\u6587\u4ef6\uff0c\u4f8b\u5982hello.py\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199\u4ee3\u7801\uff1a<\/strong><br \/>\u5728hello.py\u4e2d\u7f16\u5199Python 2\u4ee3\u7801\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print &quot;Hello, World!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884cPython\u811a\u672c\uff1a<\/strong><br \/>\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\uff0c\u5bfc\u822a\u5230\u811a\u672c\u6240\u5728\u7684\u76ee\u5f55\uff0c\u8fd0\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python hello.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u8fc1\u79fb\u5230Python 3\u7684\u8003\u8651<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u4f60\u53ef\u4ee5\u7ee7\u7eed\u4f7f\u7528Python 2\uff0c\u4f46\u5efa\u8bae\u8003\u8651\u8fc1\u79fb\u5230Python 3\uff0c\u56e0\u4e3a\u5b83\u5e26\u6765\u4e86\u8bb8\u591a\u6539\u8fdb\u548c\u65b0\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u68c0\u67e5\u517c\u5bb9\u6027\uff1a<\/strong><br \/>\u4f7f\u7528\u5de5\u5177\u5982<code>caniusepython3<\/code>\u6765\u68c0\u67e5\u4f60\u7684\u9879\u76ee\u662f\u5426\u53ef\u4ee5\u8fc1\u79fb\u5230Python 3\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u75282to3\u5de5\u5177\uff1a<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5de5\u5177\u53ef\u4ee5\u81ea\u52a8\u5c06Python 2\u4ee3\u7801\u8f6c\u6362\u4e3aPython 3\u4ee3\u7801\u3002\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u8f6c\u6362\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">2to3 -w your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6d4b\u8bd5\u548c\u9a8c\u8bc1\uff1a<\/strong><br \/>\u8f6c\u6362\u540e\uff0c\u4ed4\u7ec6\u6d4b\u8bd5\u4f60\u7684\u4ee3\u7801\u4ee5\u786e\u4fdd\u5176\u5728Python 3\u4e2d\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e03\u3001\u7ba1\u7406Python 2\u7684\u4f9d\u8d56<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e2a\u590d\u6742\u7684\u9879\u76ee\uff0c\u7ba1\u7406\u4f9d\u8d56\u5173\u7cfb\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efarequirements.txt\u6587\u4ef6\uff1a<\/strong><br \/>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u751f\u6210\u5f53\u524d\u73af\u5883\u4e2d\u7684\u6240\u6709\u5305\u7684\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip freeze &gt; requirements.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5\u4f9d\u8d56\uff1a<\/strong><br \/>\u5728\u65b0\u7684\u73af\u5883\u4e2d\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5requirements.txt\u4e2d\u5217\u51fa\u7684\u6240\u6709\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install -r requirements.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1Python 2\u5df2\u7ecf\u505c\u6b62\u5b98\u65b9\u652f\u6301\uff0c\u4f46\u5b83\u5728\u4e00\u4e9b\u9057\u7559\u7cfb\u7edf\u4e2d\u4ecd\u7136\u662f\u5fc5\u9700\u7684\u3002\u901a\u8fc7\u672c\u6587\u4e2d\u8be6\u7ec6\u7684\u5b89\u88c5\u548c\u4f7f\u7528\u6307\u5bfc\uff0c\u4f60\u53ef\u4ee5\u7ee7\u7eed\u5728\u8fd9\u4e9b\u7cfb\u7edf\u4e0a\u5f00\u53d1\u548c\u7ef4\u62a4Python 2\u5e94\u7528\u3002\u5373\u4fbf\u5982\u6b64\uff0c\u5efa\u8bae\u5728\u6761\u4ef6\u5141\u8bb8\u7684\u60c5\u51b5\u4e0b\uff0c\u5c3d\u5feb\u8fc1\u79fb\u5230Python 3\u4ee5\u5229\u7528\u5176\u66f4\u5148\u8fdb\u7684\u7279\u6027\u548c\u6301\u7eed\u7684\u793e\u533a\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5b89\u88c5Python 2\u9700\u8981\u54ea\u4e9b\u7cfb\u7edf\u8981\u6c42\uff1f<\/strong><br \/>\u5728\u5b89\u88c5Python 2\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u652f\u6301\u8be5\u7248\u672c\u3002Python 2\u53ef\u4ee5\u5728Windows\u3001macOS\u548cLinux\u4e0a\u8fd0\u884c\u3002\u5bf9\u4e8eWindows\u7528\u6237\uff0c\u5efa\u8bae\u4f7f\u7528Windows 7\u53ca\u4ee5\u4e0a\u7248\u672c\u3002macOS\u7528\u6237\u9700\u786e\u4fdd\u7cfb\u7edf\u7248\u672c\u517c\u5bb9\uff0c\u800cLinux\u7528\u6237\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u8f7b\u677e\u5b89\u88c5\u3002<\/p>\n<p><strong>\u5728Windows\u4e0a\u5b89\u88c5Python 2\u7684\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728Windows\u4e0a\uff0c\u8bbf\u95eePython\u5b98\u65b9\u7f51\u7ad9\uff0c\u4e0b\u8f7d\u9002\u5408\u4f60\u7cfb\u7edf\u67b6\u6784\uff0832\u4f4d\u621664\u4f4d\uff09\u7684Python 2\u5b89\u88c5\u7a0b\u5e8f\u3002\u53cc\u51fb\u4e0b\u8f7d\u7684\u6587\u4ef6\uff0c\u6309\u7167\u63d0\u793a\u8fdb\u884c\u5b89\u88c5\u3002\u5728\u5b89\u88c5\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u9009\u62e9\u5c06Python\u6dfb\u52a0\u5230\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\uff0c\u4ee5\u4fbf\u5728\u547d\u4ee4\u884c\u4e2d\u76f4\u63a5\u4f7f\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python 2\u4e2d\u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93\uff1f<\/strong><br \/>\u8981\u5728Python 2\u4e2d\u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93\uff0c\u901a\u5e38\u4f7f\u7528<code>pip<\/code>\u5de5\u5177\u3002\u786e\u4fdd\u5728\u5b89\u88c5Python\u65f6\u9009\u62e9\u4e86pip\u9009\u9879\u3002\u5b89\u88c5\u540e\uff0c\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528\u547d\u4ee4<code>pip install package_name<\/code>\u6765\u5b89\u88c5\u6240\u9700\u7684\u5e93\u3002\u4f8b\u5982\uff0c\u5b89\u88c5Requests\u5e93\u7684\u547d\u4ee4\u662f<code>pip install requests<\/code>\u3002\u5bf9\u4e8e\u67d0\u4e9b\u5e93\uff0c\u53ef\u80fd\u9700\u8981\u7279\u5b9a\u7248\u672c\u7684Python\uff0c\u786e\u4fdd\u5728\u5b89\u88c5\u524d\u786e\u8ba4\u5e93\u7684\u517c\u5bb9\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5b89\u88c5\u548c\u4f7f\u7528Python 2\u9700\u8981\u8003\u8651\u51e0\u4e2a\u5173\u952e\u6b65\u9aa4\uff1a\u5b89\u88c5Python 2\u89e3\u91ca\u5668\u3001\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u3001\u5b89\u88c5\u5305\u7ba1\u7406\u5de5\u5177pip [&hellip;]","protected":false},"author":3,"featured_media":1016475,"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\/1016472"}],"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=1016472"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1016472\/revisions"}],"predecessor-version":[{"id":1016476,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1016472\/revisions\/1016476"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1016475"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1016472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1016472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1016472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}