{"id":1141629,"date":"2025-01-08T22:32:48","date_gmt":"2025-01-08T14:32:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1141629.html"},"modified":"2025-01-08T22:32:52","modified_gmt":"2025-01-08T14:32:52","slug":"%e5%a6%82%e4%bd%95%e7%bb%99python2%e6%8c%87%e5%ae%9a%e7%bc%96%e8%af%91%e5%99%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1141629.html","title":{"rendered":"\u5982\u4f55\u7ed9python2\u6307\u5b9a\u7f16\u8bd1\u5668"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104323\/44d890c9-42e3-4b52-ab5d-2819ba2f26ef.webp\" alt=\"\u5982\u4f55\u7ed9python2\u6307\u5b9a\u7f16\u8bd1\u5668\" \/><\/p>\n<p><p> <strong>\u7ed9Python 2\u6307\u5b9a\u7f16\u8bd1\u5668\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u4e2a\uff1a\u4f7f\u7528\u865a\u62df\u73af\u5883\u3001\u4f7f\u7528Shebang\u3001\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u865a\u62df\u73af\u5883<\/strong>\u662f\u6700\u5e38\u89c1\u4e14\u63a8\u8350\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u865a\u62df\u73af\u5883\u5141\u8bb8\u4f60\u5728\u540c\u4e00\u7cfb\u7edf\u4e0a\u521b\u5efa\u591a\u4e2a\u72ec\u7acb\u7684Python\u73af\u5883\uff0c\u6bcf\u4e2a\u73af\u5883\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684Python\u7248\u672c\u548c\u5305\u3002\u8fd9\u6837\uff0c\u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a\u9879\u76ee\u4e2d\u4f7f\u7528Python 2\uff0c\u5728\u53e6\u4e00\u4e2a\u9879\u76ee\u4e2d\u4f7f\u7528Python 3\uff0c\u800c\u4e0d\u5fc5\u62c5\u5fc3\u7248\u672c\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883<\/h3>\n<\/p>\n<p><h4>\u5b89\u88c5Virtualenv<\/h4>\n<\/p>\n<p><p>\u8981\u4f7f\u7528\u865a\u62df\u73af\u5883\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u4e00\u4e2a\u540d\u4e3a<code>virtualenv<\/code>\u7684\u5de5\u5177\u3002\u8fd9\u4e2a\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u4f60\u521b\u5efa\u72ec\u7acb\u7684Python\u73af\u5883\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install virtualenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u521b\u5efa\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u4e00\u4e2a\u865a\u62df\u73af\u5883\uff0c\u5e76\u6307\u5b9aPython\u7f16\u8bd1\u5668\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">virtualenv -p \/usr\/bin\/python2.7 myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u547d\u4ee4\u4e2d\uff0c<code>\/usr\/bin\/python2.7<\/code>\u662fPython 2\u7f16\u8bd1\u5668\u7684\u8def\u5f84\uff0c<code>myenv<\/code>\u662f\u865a\u62df\u73af\u5883\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><h4>\u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u5b8c\u6210\u540e\uff0c\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">source myenv\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6fc0\u6d3b\u540e\uff0c\u4f60\u53ef\u4ee5\u5728\u865a\u62df\u73af\u5883\u4e2d\u5b89\u88c5\u4f60\u9700\u8981\u7684\u5305\uff0c\u800c\u4e0d\u4f1a\u5f71\u54cd\u5168\u5c40Python\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h4>\u9000\u51fa\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5b8c\u865a\u62df\u73af\u5883\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u9000\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">deactivate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Shebang<\/h3>\n<\/p>\n<p><h4>\u4ec0\u4e48\u662fShebang<\/h4>\n<\/p>\n<p><p>Shebang\u662f\u6307\u5728\u811a\u672c\u6587\u4ef6\u7684\u7b2c\u4e00\u884c\u4e2d\u6307\u5b9a\u89e3\u91ca\u5668\u7684\u65b9\u5f0f\u3002\u8fd9\u79cd\u65b9\u6cd5\u5e38\u7528\u4e8eUnix\u548c\u7c7bUnix\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<p><h4>\u5728\u811a\u672c\u4e2d\u4f7f\u7528Shebang<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5728Python\u811a\u672c\u7684\u7b2c\u4e00\u884c\u4e2d\u6dfb\u52a0Shebang\u884c\uff0c\u4ee5\u6307\u5b9a\u4f7f\u7528Python 2\u7f16\u8bd1\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">#!\/usr\/bin\/env python2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u811a\u672c\u6587\u4ef6\u5728\u6267\u884c\u65f6\u4f7f\u7528\u6307\u5b9a\u7684Python\u7248\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><h4>\u4e34\u65f6\u4fee\u6539\u73af\u5883\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4e34\u65f6\u4fee\u6539\u73af\u5883\u53d8\u91cf\uff0c\u4ee5\u6307\u5b9a\u4f7f\u7528Python 2\u7f16\u8bd1\u5668\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export PATH=\/usr\/bin\/python2:$PATH<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u5f53\u524d\u4f1a\u8bdd\u4e2d\u6709\u6548\uff0c\u4f46\u5173\u95ed\u7ec8\u7aef\u540e\u4f1a\u5931\u6548\u3002<\/p>\n<\/p>\n<p><h4>\u6c38\u4e45\u4fee\u6539\u73af\u5883\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u8981\u6c38\u4e45\u4fee\u6539\u73af\u5883\u53d8\u91cf\uff0c\u53ef\u4ee5\u7f16\u8f91\u4f60\u7684shell\u914d\u7f6e\u6587\u4ef6\uff08\u5982<code>.bashrc<\/code>\u3001<code>.bash_profile<\/code>\u6216<code>.zshrc<\/code>\uff09\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export PATH=\/usr\/bin\/python2:$PATH<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4fdd\u5b58\u5e76\u91cd\u65b0\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">source ~\/.bashrc<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c\u4f60\u6bcf\u6b21\u6253\u5f00\u7ec8\u7aef\u65f6\u90fd\u4f1a\u4f7f\u7528\u6307\u5b9a\u7684Python 2\u7f16\u8bd1\u5668\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u65e0\u8bba\u4f60\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c<strong>\u4f7f\u7528\u865a\u62df\u73af\u5883<\/strong>\u90fd\u662f\u6700\u63a8\u8350\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u6307\u5b9aPython\u7248\u672c\uff0c\u8fd8\u53ef\u4ee5\u7ba1\u7406\u9879\u76ee\u7684\u4f9d\u8d56\u5305\u3002<strong>Shebang<\/strong>\u65b9\u6cd5\u9002\u7528\u4e8e\u5355\u4e2a\u811a\u672c\u6587\u4ef6\uff0c\u800c<strong>\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/strong>\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u9891\u7e41\u5207\u6362Python\u7248\u672c\u7684\u60c5\u51b5\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u4e0d\u540c\u9879\u76ee\u4e2d\u4f7f\u7528\u4e0d\u540c\u7684Python\u7248\u672c\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u517c\u5bb9\u6027\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python 2\u4e2d\u9009\u62e9\u7279\u5b9a\u7684\u7f16\u8bd1\u5668\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python 2\u65f6\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u6307\u5b9a\u7279\u5b9a\u7684\u7f16\u8bd1\u5668\u4ee5\u7f16\u8bd1\u6269\u5c55\u6a21\u5757\u6216\u8fd0\u884c\u67d0\u4e9b\u4ee3\u7801\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<code>CC<\/code>\u6765\u6307\u5b9a\u7f16\u8bd1\u5668\u3002\u4f8b\u5982\uff0c\u5728Unix\/Linux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4<code>export CC=gcc<\/code>\u6765\u8bbe\u7f6eGCC\u4f5c\u4e3a\u7f16\u8bd1\u5668\u3002\u786e\u4fdd\u5728\u8fd0\u884cPython\u7a0b\u5e8f\u4e4b\u524d\u6267\u884c\u6b64\u547d\u4ee4\u3002<\/p>\n<p><strong>\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u5982\u4f55\u6307\u5b9aPython 2\u7684\u7f16\u8bd1\u5668\uff1f<\/strong><br \/>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>PATH<\/code>\u73af\u5883\u53d8\u91cf\u6765\u6307\u5b9a\u4f7f\u7528\u7684\u7f16\u8bd1\u5668\u3002\u5b89\u88c5Visual Studio\u540e\uff0c\u786e\u4fdd<code>cl.exe<\/code>\u7684\u8def\u5f84\u88ab\u6dfb\u52a0\u5230<code>PATH<\/code>\u4e2d\u3002\u5bf9\u4e8eUnix\/Linux\u7cfb\u7edf\uff0c\u901a\u8fc7\u5728\u7ec8\u7aef\u4e2d\u8bbe\u7f6e<code>CC<\/code>\u73af\u5883\u53d8\u91cf\u6765\u9009\u62e9\u7f16\u8bd1\u5668\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>CC=clang python setup.py build<\/code>\u6765\u4f7f\u7528Clang\u7f16\u8bd1\u5668\u3002<\/p>\n<p><strong>\u7f16\u8bd1\u5668\u9009\u62e9\u5bf9Python 2\u9879\u76ee\u6709\u4ec0\u4e48\u5f71\u54cd\uff1f<\/strong><br \/>\u9009\u62e9\u9002\u5408\u7684\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u5f71\u54cd\u4ee3\u7801\u7684\u6027\u80fd\u548c\u517c\u5bb9\u6027\u3002\u4e0d\u540c\u7684\u7f16\u8bd1\u5668\u53ef\u80fd\u4f18\u5316\u4ee3\u7801\u7684\u65b9\u5f0f\u4e0d\u540c\uff0c\u5bfc\u81f4\u6027\u80fd\u5dee\u5f02\u3002\u6b64\u5916\uff0c\u67d0\u4e9bPython\u6269\u5c55\u53ef\u80fd\u53ea\u4e0e\u7279\u5b9a\u7684\u7f16\u8bd1\u5668\u517c\u5bb9\u3002\u56e0\u6b64\uff0c\u786e\u4fdd\u9009\u62e9\u7684\u7f16\u8bd1\u5668\u4e0e\u60a8\u7684\u9879\u76ee\u9700\u6c42\u76f8\u7b26\uff0c\u4ee5\u907f\u514d\u6f5c\u5728\u7684\u8fd0\u884c\u65f6\u9519\u8bef\u6216\u6027\u80fd\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7ed9Python 2\u6307\u5b9a\u7f16\u8bd1\u5668\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u4e2a\uff1a\u4f7f\u7528\u865a\u62df\u73af\u5883\u3001\u4f7f\u7528Shebang\u3001\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u3002 \u4f7f\u7528\u865a\u62df\u73af\u5883\u662f [&hellip;]","protected":false},"author":3,"featured_media":1141638,"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\/1141629"}],"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=1141629"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141629\/revisions"}],"predecessor-version":[{"id":1141644,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141629\/revisions\/1141644"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1141638"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1141629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1141629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1141629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}