{"id":1012619,"date":"2024-12-27T11:40:44","date_gmt":"2024-12-27T03:40:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1012619.html"},"modified":"2024-12-27T11:40:49","modified_gmt":"2024-12-27T03:40:49","slug":"%e5%a6%82%e4%bd%95%e6%9b%b4%e6%94%b9python%e9%bb%98%e8%ae%a4%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1012619.html","title":{"rendered":"\u5982\u4f55\u66f4\u6539python\u9ed8\u8ba4\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090334\/aad046e1-5f66-42f7-b067-db9ced1abe8b.webp\" alt=\"\u5982\u4f55\u66f4\u6539python\u9ed8\u8ba4\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u66f4\u6539Python\u9ed8\u8ba4\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\uff1a\u4fee\u6539\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883\u3001\u901a\u8fc7\u811a\u672c\u6307\u5b9a\u8def\u5f84\u3002\u5176\u4e2d\uff0c\u4fee\u6539\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5b83\u80fd\u786e\u4fdd\u5728\u4efb\u4f55\u5730\u65b9\u8fd0\u884cPython\u65f6\uff0c\u4f7f\u7528\u7684\u90fd\u662f\u6307\u5b9a\u7684\u7248\u672c\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4fee\u6539\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5b83\u80fd\u786e\u4fdd\u5728\u4efb\u4f55\u5730\u65b9\u8fd0\u884cPython\u65f6\uff0c\u4f7f\u7528\u7684\u90fd\u662f\u6307\u5b9a\u7684\u7248\u672c\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u4f60\u9700\u8981\u627e\u5230Python\u7684\u5b89\u88c5\u8def\u5f84\uff0c\u7136\u540e\u5c06\u8be5\u8def\u5f84\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u4e2d\u3002\u8fd9\u6837\uff0c\u6bcf\u6b21\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>python<\/code>\u65f6\uff0c\u7cfb\u7edf\u5c31\u4f1a\u81ea\u52a8\u4f7f\u7528\u8fd9\u4e2a\u8def\u5f84\u4e0b\u7684Python\u7248\u672c\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u90a3\u4e9b\u5728\u8ba1\u7b97\u673a\u4e0a\u5b89\u88c5\u4e86\u591a\u4e2aPython\u7248\u672c\u7684\u7528\u6237\uff0c\u56e0\u4e3a\u5b83\u80fd\u5e2e\u52a9\u4ed6\u4eec\u8f7b\u677e\u5207\u6362\u9ed8\u8ba4\u4f7f\u7528\u7684Python\u7248\u672c\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4fee\u6539\u7cfb\u7edf\u73af\u5883\u53d8\u91cf<\/p>\n<\/p>\n<p><p>\u4fee\u6539\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u662f\u66f4\u6539Python\u9ed8\u8ba4\u6587\u4ef6\u7684\u6700\u76f4\u63a5\u65b9\u6cd5\u3002\u65e0\u8bba\u662f\u5728Windows\u8fd8\u662fLinux\u7cfb\u7edf\u4e2d\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u90fd\u80fd\u6709\u6548\u5730\u6307\u5b9a\u9ed8\u8ba4\u7684Python\u7248\u672c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Windows\u7cfb\u7edf<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9996\u5148\uff0c\u627e\u5230Python\u7684\u5b89\u88c5\u8def\u5f84\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0cPython\u4f1a\u5b89\u88c5\u5728<code>C:\\PythonXX<\/code>\uff08XX\u4e3a\u7248\u672c\u53f7\uff09\u76ee\u5f55\u4e0b\u3002<\/li>\n<li>\u7136\u540e\uff0c\u53f3\u952e\u70b9\u51fb\u201c\u6b64\u7535\u8111\u201d\u6216\u201c\u6211\u7684\u7535\u8111\u201d\uff0c\u9009\u62e9\u201c\u5c5e\u6027\u201d\u3002<\/li>\n<li>\u5728\u5f39\u51fa\u7684\u7a97\u53e3\u4e2d\uff0c\u70b9\u51fb\u5de6\u4fa7\u7684\u201c\u9ad8\u7ea7\u7cfb\u7edf\u8bbe\u7f6e\u201d\uff0c\u7136\u540e\u9009\u62e9\u201c\u73af\u5883\u53d8\u91cf\u201d\u3002<\/li>\n<li>\u5728\u201c\u7cfb\u7edf\u53d8\u91cf\u201d\u4e2d\uff0c\u627e\u5230\u53d8\u91cf\u540d\u4e3a<code>Path<\/code>\u7684\u6761\u76ee\uff0c\u9009\u4e2d\u5e76\u70b9\u51fb\u201c\u7f16\u8f91\u201d\u3002<\/li>\n<li>\u5728\u5f39\u51fa\u7684\u7f16\u8f91\u6846\u4e2d\uff0c\u5c06Python\u7684\u5b89\u88c5\u8def\u5f84\u6dfb\u52a0\u5230\u53d8\u91cf\u503c\u4e2d\uff0c\u786e\u4fdd\u5176\u4f4d\u4e8e\u8def\u5f84\u5217\u8868\u7684\u6700\u524d\u9762\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>Linux\u7cfb\u7edf<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00\u7ec8\u7aef\uff0c\u8f93\u5165<code>which python<\/code>\u6216<code>whereis python<\/code>\u547d\u4ee4\u4ee5\u627e\u5230\u5f53\u524d\u9ed8\u8ba4Python\u7684\u8def\u5f84\u3002<\/li>\n<li>\u7f16\u8f91<code>.bashrc<\/code>\u6587\u4ef6\uff1a\u4f7f\u7528\u547d\u4ee4<code>nano ~\/.bashrc<\/code>\u6216<code>vim ~\/.bashrc<\/code>\u6253\u5f00\u6587\u4ef6\u3002<\/li>\n<li>\u5728\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\u4ee5\u4e0b\u884c\uff0c\u5c06<code>\/path\/to\/your\/python<\/code>\u66ff\u6362\u4e3a\u4f60\u5e0c\u671b\u8bbe\u7f6e\u7684Python\u8def\u5f84\uff1a\n<pre><code>export PATH=&quot;\/path\/to\/your\/python:$PATH&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u4fdd\u5b58\u5e76\u5173\u95ed\u6587\u4ef6\uff0c\u7136\u540e\u8fd0\u884c<code>source ~\/.bashrc<\/code>\u4ee5\u5e94\u7528\u66f4\u6539\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u865a\u62df\u73af\u5883\u4e0d\u4ec5\u53ef\u4ee5\u66f4\u6539Python\u9ed8\u8ba4\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u4e3a\u4e0d\u540c\u7684\u9879\u76ee\u4f7f\u7528\u4e0d\u540c\u7684Python\u7248\u672c\u548c\u4f9d\u8d56\u5e93\u3002\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u7075\u6d3b\u548c\u5b89\u5168\u7684\u65b9\u5f0f\uff0c\u7279\u522b\u9002\u5408\u5f00\u53d1\u73af\u5883\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u865a\u62df\u73af\u5883<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5b89\u88c5<code>virtualenv<\/code>\u5de5\u5177\uff1a\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8f93\u5165<code>pip install virtualenv<\/code>\u3002<\/li>\n<li>\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u521b\u5efa\u865a\u62df\u73af\u5883\uff1a\u4f7f\u7528\u547d\u4ee4<code>virtualenv venv<\/code>\uff08<code>venv<\/code>\u4e3a\u865a\u62df\u73af\u5883\u7684\u540d\u79f0\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\uff09\u3002<\/li>\n<li>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a\n<ul>\n<li>Windows\u7cfb\u7edf\uff1a\u8fd0\u884c<code>venv\\Scripts\\activate<\/code>\u3002<\/li>\n<li>Linux\u6216Mac\u7cfb\u7edf\uff1a\u8fd0\u884c<code>source venv\/bin\/activate<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u865a\u62df\u73af\u5883<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u6fc0\u6d3b\u540e\uff0c\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u524d\u4f1a\u51fa\u73b0\u865a\u62df\u73af\u5883\u7684\u540d\u79f0\uff0c\u8868\u793a\u5df2\u7ecf\u6210\u529f\u8fdb\u5165\u865a\u62df\u73af\u5883\u3002<\/li>\n<li>\u5728\u865a\u62df\u73af\u5883\u4e2d\u5b89\u88c5\u7684\u6240\u6709\u5e93\u548c\u5305\u90fd\u662f\u9694\u79bb\u7684\uff0c\u4e0d\u4f1a\u5f71\u54cd\u5230\u7cfb\u7edf\u7684Python\u73af\u5883\u3002<\/li>\n<li>\u9000\u51fa\u865a\u62df\u73af\u5883\uff1a\u8f93\u5165<code>deactivate<\/code>\u547d\u4ee4\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u901a\u8fc7\u811a\u672c\u6307\u5b9a\u8def\u5f84<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u5728\u811a\u672c\u4e2d\u6307\u5b9aPython\u7684\u8def\u5f84\uff0c\u4ee5\u786e\u4fdd\u811a\u672c\u4f7f\u7528\u6b63\u786e\u7684Python\u7248\u672c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5728Unix\/Linux\u7cfb\u7edf\u4e2d<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5728\u811a\u672c\u6587\u4ef6\u7684\u7b2c\u4e00\u884c\u6dfb\u52a0<code>shebang<\/code>\u884c\uff0c\u4f8b\u5982\uff1a\n<pre><code>#!\/usr\/bin\/env python3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p>\u6216\u8005\u76f4\u63a5\u6307\u5b9aPython\u8def\u5f84\uff1a<\/p>\n<pre><code>#!\/usr\/bin\/python3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5728Windows\u7cfb\u7edf\u4e2d<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528\u6279\u5904\u7406\u6587\u4ef6\uff08<code>.bat<\/code>\uff09\u6216PowerShell\u811a\u672c\uff0c\u5728\u5176\u4e2d\u6307\u5b9aPython\u7684\u8def\u5f84\u3002<\/li>\n<li>\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u6279\u5904\u7406\u6587\u4ef6<code>run_python.bat<\/code>\uff1a\n<pre><code>@echo off<\/p>\n<p>&quot;C:\\Python39\\python.exe&quot; %*<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0a\u65b9\u6cd5\u5206\u522b\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u548c\u9700\u6c42\u3002\u4fee\u6539\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u9002\u7528\u4e8e\u5168\u5c40\u66f4\u6539\uff0c\u865a\u62df\u73af\u5883\u9002\u5408\u9879\u76ee\u7ea7\u522b\u7684\u63a7\u5236\uff0c\u800c\u811a\u672c\u6307\u5b9a\u8def\u5f84\u5219\u9002\u7528\u4e8e\u7279\u5b9a\u7684\u6267\u884c\u73af\u5883\u3002\u6839\u636e\u81ea\u5df1\u7684\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u786e\u4fddPython\u9879\u76ee\u7684\u987a\u5229\u8fdb\u884c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u66f4\u6539\u9ed8\u8ba4\u6587\u4ef6\u8def\u5f84\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u6765\u66f4\u6539\u9ed8\u8ba4\u6587\u4ef6\u8def\u5f84\u3002\u901a\u8fc7\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u6216\u5728\u4ee3\u7801\u4e2d\u6307\u5b9a\u6587\u4ef6\u8def\u5f84\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u9ed8\u8ba4\u6587\u4ef6\u4f4d\u7f6e\u7684\u4fee\u6539\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.chdir(&#39;\u65b0\u8def\u5f84&#39;)<\/code>\u6765\u6539\u53d8\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>open(&#39;\u6587\u4ef6\u540d&#39;, &#39;\u6a21\u5f0f&#39;)<\/code>\u65f6\uff0c\u76f4\u63a5\u6307\u5b9a\u5b8c\u6574\u8def\u5f84\u4e5f\u53ef\u4ee5\u907f\u514d\u4f7f\u7528\u9ed8\u8ba4\u8def\u5f84\u3002<\/p>\n<p><strong>\u66f4\u6539Python\u811a\u672c\u7684\u9ed8\u8ba4\u914d\u7f6e\u6587\u4ef6\u6709\u4ec0\u4e48\u5f71\u54cd\uff1f<\/strong><br \/>\u66f4\u6539Python\u811a\u672c\u7684\u9ed8\u8ba4\u914d\u7f6e\u6587\u4ef6\u53ef\u80fd\u4f1a\u5f71\u54cd\u7a0b\u5e8f\u7684\u8fd0\u884c\u548c\u6570\u636e\u5904\u7406\u65b9\u5f0f\u3002\u5982\u679c\u914d\u7f6e\u6587\u4ef6\u5305\u542b\u91cd\u8981\u7684\u53c2\u6570\u6216\u8bbe\u7f6e\uff0c\u786e\u4fdd\u65b0\u7684\u914d\u7f6e\u6587\u4ef6\u4e0e\u7a0b\u5e8f\u903b\u8f91\u76f8\u7b26\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u4e0d\u6b63\u786e\u7684\u914d\u7f6e\u6587\u4ef6\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u9519\u8bef\u6216\u6570\u636e\u4e22\u5931\uff0c\u56e0\u6b64\u5728\u8fdb\u884c\u66f4\u6539\u65f6\u52a1\u5fc5\u4ed4\u7ec6\u68c0\u67e5\u3002<\/p>\n<p><strong>\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u5982\u4f55\u66f4\u6539Python\u7684\u9ed8\u8ba4\u6587\u4ef6\u8bbe\u7f6e\uff1f<\/strong><br \/>\u5728Windows\u3001macOS\u548cLinux\u4e2d\uff0c\u66f4\u6539Python\u9ed8\u8ba4\u6587\u4ef6\u8bbe\u7f6e\u7684\u65b9\u6cd5\u7565\u6709\u4e0d\u540c\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u4f7f\u7528<code>os.path<\/code>\u548c\u76f8\u5173\u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u5904\u7406\u6587\u4ef6\u8def\u5f84\u7684\u8de8\u5e73\u53f0\u95ee\u9898\u3002\u5728Windows\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u53cd\u659c\u6760\uff08\\\uff09\uff0c\u800c\u5728Unix\u7cfb\u7edf\u4e2d\u5219\u4f7f\u7528\u6b63\u659c\u6760\uff08\/\uff09\u3002\u786e\u4fdd\u4f7f\u7528\u9002\u5408\u60a8\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\u683c\u5f0f\uff0c\u4ee5\u907f\u514d\u6587\u4ef6\u627e\u4e0d\u5230\u7684\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u66f4\u6539Python\u9ed8\u8ba4\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\uff1a\u4fee\u6539\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883\u3001\u901a\u8fc7\u811a\u672c\u6307\u5b9a\u8def\u5f84\u3002\u5176\u4e2d\uff0c\u4fee\u6539\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u662f\u6700 [&hellip;]","protected":false},"author":3,"featured_media":1012637,"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\/1012619"}],"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=1012619"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1012619\/revisions"}],"predecessor-version":[{"id":1012641,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1012619\/revisions\/1012641"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1012637"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1012619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1012619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1012619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}