{"id":980260,"date":"2024-12-27T06:53:06","date_gmt":"2024-12-26T22:53:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/980260.html"},"modified":"2024-12-27T06:53:09","modified_gmt":"2024-12-26T22:53:09","slug":"python%e5%a6%82%e4%bd%95%e6%9b%b4%e6%8d%a2%e8%a7%a3%e9%87%8a%e5%99%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/980260.html","title":{"rendered":"python\u5982\u4f55\u66f4\u6362\u89e3\u91ca\u5668"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205734\/f6c95310-fc3c-4174-bc67-4f930d9b5d0a.webp\" alt=\"python\u5982\u4f55\u66f4\u6362\u89e3\u91ca\u5668\" \/><\/p>\n<p><p> \u66f4\u6362Python\u89e3\u91ca\u5668\u53ef\u4ee5\u901a\u8fc7<strong>\u4fee\u6539\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883\u3001\u901a\u8fc7IDE\u8fdb\u884c\u8bbe\u7f6e<\/strong>\u6765\u5b9e\u73b0\uff0c\u5176\u4e2d\u4f7f\u7528\u865a\u62df\u73af\u5883\u662f\u6700\u4e3a\u63a8\u8350\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u865a\u62df\u73af\u5883\uff0c\u53ef\u4ee5\u5728\u540c\u4e00\u53f0\u673a\u5668\u4e0a\u5b89\u88c5\u591a\u4e2aPython\u7248\u672c\uff0c\u5e76\u4e14\u4e3a\u4e0d\u540c\u7684\u9879\u76ee\u9009\u62e9\u4e0d\u540c\u7684\u89e3\u91ca\u5668\uff0c\u4ece\u800c\u907f\u514d\u7248\u672c\u51b2\u7a81\u7684\u95ee\u9898\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u865a\u62df\u73af\u5883\u7ba1\u7406Python\u89e3\u91ca\u5668\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4fee\u6539\u7cfb\u7edf\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><h4>1. \u68c0\u67e5\u5f53\u524dPython\u7248\u672c<\/h4>\n<\/p>\n<p><p>\u5728\u66f4\u6539Python\u89e3\u91ca\u5668\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u786e\u8ba4\u5f53\u524d\u7cfb\u7edf\u6b63\u5728\u4f7f\u7528\u7684Python\u7248\u672c\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u67e5\u770b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6839\u636e\u8fd4\u56de\u7684\u7ed3\u679c\uff0c\u60a8\u53ef\u4ee5\u4e86\u89e3\u5f53\u524d\u4f7f\u7528\u7684Python\u89e3\u91ca\u5668\u7248\u672c\u3002<\/p>\n<\/p>\n<p><h4>2. \u5b89\u88c5\u65b0\u7684Python\u7248\u672c<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u66f4\u6362\u5230\u4e00\u4e2a\u65b0\u7684Python\u7248\u672c\uff0c\u9996\u5148\u9700\u8981\u4ecePython\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u8be5\u7248\u672c\u7684\u5b89\u88c5\u5305\u5e76\u8fdb\u884c\u5b89\u88c5\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3.x --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\uff08\u5176\u4e2d<code>3.x<\/code>\u66ff\u6362\u4e3a\u60a8\u5b89\u88c5\u7684\u5177\u4f53\u7248\u672c\u53f7\uff09<\/p>\n<\/p>\n<p><h4>3. \u4fee\u6539\u73af\u5883\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u4fee\u6539\u73af\u5883\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<ul>\n<li>\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>\u70b9\u51fb\u201c\u9ad8\u7ea7\u7cfb\u7edf\u8bbe\u7f6e\u201d\u3002<\/li>\n<li>\u5728\u201c\u7cfb\u7edf\u5c5e\u6027\u201d\u7a97\u53e3\u4e2d\uff0c\u70b9\u51fb\u201c\u73af\u5883\u53d8\u91cf\u201d\u6309\u94ae\u3002<\/li>\n<li>\u5728\u201c\u7cfb\u7edf\u53d8\u91cf\u201d\u4e2d\uff0c\u627e\u5230<code>Path<\/code>\u53d8\u91cf\uff0c\u53cc\u51fb\u6253\u5f00\u3002<\/li>\n<li>\u5c06\u65b0\u5b89\u88c5\u7684Python\u8def\u5f84\u6dfb\u52a0\u5230\u53d8\u91cf\u503c\u4e2d\uff0c\u5e76\u786e\u4fdd\u5176\u5728\u65e7\u7248\u672cPython\u8def\u5f84\u4e4b\u524d\u3002<\/li>\n<\/ul>\n<p><p>\u5728Mac\u6216Linux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539<code>~\/.bashrc<\/code>\u6216<code>~\/.bash_profile<\/code>\u6587\u4ef6\u6765\u66f4\u65b0\u73af\u5883\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export PATH=&quot;\/usr\/local\/bin\/python3.x:$PATH&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4fdd\u5b58\u6587\u4ef6\u540e\uff0c\u901a\u8fc7\u547d\u4ee4<code>source ~\/.bashrc<\/code>\u6216<code>source ~\/.bash_profile<\/code>\u4f7f\u66f4\u6539\u751f\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883<\/h3>\n<\/p>\n<p><h4>1. \u521b\u5efa\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>Python\u81ea\u5e26\u7684<code>venv<\/code>\u6a21\u5757\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u865a\u62df\u73af\u5883\u3002\u9996\u5148\uff0c\u9009\u62e9\u9879\u76ee\u76ee\u5f55\uff0c\u7136\u540e\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3.x -m venv myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\uff08\u5176\u4e2d<code>3.x<\/code>\u66ff\u6362\u4e3a\u60a8\u9700\u8981\u7684Python\u7248\u672c\uff0c<code>myenv<\/code>\u4e3a\u865a\u62df\u73af\u5883\u540d\u79f0\uff09<\/p>\n<\/p>\n<p><h4>2. \u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u865a\u62df\u73af\u5883\u540e\uff0c\u9700\u8981\u6fc0\u6d3b\u5b83\u624d\u80fd\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5728Windows\u4e0a\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-bash\">myenv\\Scripts\\activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u5728Mac\u6216Linux\u4e0a\uff1a<\/li>\n<\/ul>\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\u547d\u4ee4\u884c\u63d0\u793a\u7b26\u5c06\u4f1a\u6539\u53d8\uff0c\u4ee5\u663e\u793a\u5f53\u524d\u6b63\u5728\u4f7f\u7528\u7684\u865a\u62df\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h4>3. \u5b89\u88c5\u9879\u76ee\u6240\u9700\u7684\u5305<\/h4>\n<\/p>\n<p><p>\u5728\u6fc0\u6d3b\u7684\u865a\u62df\u73af\u5883\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\u6765\u5b89\u88c5\u9879\u76ee\u6240\u9700\u7684Python\u5305\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u7684\u5305\u4ec5\u4f1a\u5f71\u54cd\u5f53\u524d\u865a\u62df\u73af\u5883\uff0c\u4e0d\u4f1a\u5bf9\u5168\u5c40Python\u73af\u5883\u9020\u6210\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><h4>4. \u9000\u51fa\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u5de5\u4f5c\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u9000\u51fa\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">deactivate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7IDE\u8fdb\u884c\u8bbe\u7f6e<\/h3>\n<\/p>\n<p><p>\u8bb8\u591a\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5982PyCharm\u3001VSCode\u7b49\u90fd\u652f\u6301\u9009\u62e9\u548c\u5207\u6362Python\u89e3\u91ca\u5668\u3002\u4e0b\u9762\u4ee5PyCharm\u4e3a\u4f8b\u8bf4\u660e\uff1a<\/p>\n<\/p>\n<p><h4>1. \u5728PyCharm\u4e2d\u8bbe\u7f6ePython\u89e3\u91ca\u5668<\/h4>\n<\/p>\n<ul>\n<li>\u6253\u5f00PyCharm\uff0c\u8fdb\u5165\u9879\u76ee\u8bbe\u7f6e\u3002<\/li>\n<li>\u9009\u62e9\u201cProject: <project_name>\u201d &gt; \u201cPython Interpreter\u201d\u3002<\/li>\n<li>\u70b9\u51fb\u53f3\u4fa7\u7684\u9f7f\u8f6e\u56fe\u6807\uff0c\u9009\u62e9\u201cAdd&#8230;\u201d\uff0c\u7136\u540e\u9009\u62e9\u201cVirtualenv Environment\u201d\u3002<\/li>\n<li>\u5728\u201cBase interpreter\u201d\u4e2d\u9009\u62e9\u6240\u9700\u7684Python\u7248\u672c\u3002<\/li>\n<\/ul>\n<p><h4>2. \u8fd0\u884c\u548c\u8c03\u8bd5<\/h4>\n<\/p>\n<p><p>\u5728PyCharm\u4e2d\u8bbe\u7f6e\u597d\u89e3\u91ca\u5668\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u8be5\u89e3\u91ca\u5668\u6765\u8fd0\u884c\u548c\u8c03\u8bd5\u9879\u76ee\u4e2d\u7684Python\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u66f4\u6362Python\u89e3\u91ca\u5668\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u5176\u4e2d\u4f7f\u7528\u865a\u62df\u73af\u5883\u662f\u6700\u4e3a\u7075\u6d3b\u548c\u63a8\u8350\u7684\u65b9\u5f0f\u3002\u901a\u8fc7\u865a\u62df\u73af\u5883\uff0c\u60a8\u53ef\u4ee5\u5728\u540c\u4e00\u53f0\u673a\u5668\u4e0a\u8f7b\u677e\u7ba1\u7406\u591a\u4e2aPython\u7248\u672c\uff0c\u5e76\u4e3a\u4e0d\u540c\u7684\u9879\u76ee\u914d\u7f6e\u4e0d\u540c\u7684\u89e3\u91ca\u5668\u3002\u65e0\u8bba\u662f\u901a\u8fc7\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u3001\u865a\u62df\u73af\u5883\u8fd8\u662fIDE\u8bbe\u7f6e\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u5c06\u5927\u5927\u63d0\u9ad8\u60a8\u7684\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u9879\u76ee\u4e2d\u9009\u62e9\u4e0d\u540c\u7684\u89e3\u91ca\u5668\uff1f<\/strong><br \/>\u5728Python\u9879\u76ee\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u865a\u62df\u73af\u5883\u6765\u9009\u62e9\u4e0d\u540c\u7684\u89e3\u91ca\u5668\u3002\u4f7f\u7528 <code>venv<\/code> \u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u865a\u62df\u73af\u5883\uff0c\u5e76\u6307\u5b9aPython\u7248\u672c\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165 <code>python3 -m venv your_env_name<\/code>\uff0c\u7136\u540e\u6fc0\u6d3b\u8be5\u73af\u5883\u3002\u6fc0\u6d3b\u540e\uff0c\u6240\u6709\u7684\u4f9d\u8d56\u5305\u548c\u89e3\u91ca\u5668\u8bbe\u7f6e\u90fd\u5c06\u5728\u8be5\u73af\u5883\u4e2d\u751f\u6548\uff0c\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684Python\u7248\u672c\u3002<\/p>\n<p><strong>\u66f4\u6362Python\u89e3\u91ca\u5668\u4f1a\u5f71\u54cd\u73b0\u6709\u7684\u9879\u76ee\u5417\uff1f<\/strong><br \/>\u66f4\u6362Python\u89e3\u91ca\u5668\u53ef\u80fd\u4f1a\u5f71\u54cd\u5f53\u524d\u9879\u76ee\u7684\u4f9d\u8d56\u5173\u7cfb\u548c\u8fd0\u884c\u73af\u5883\u3002\u5982\u679c\u9879\u76ee\u4f9d\u8d56\u4e8e\u7279\u5b9a\u7248\u672c\u7684\u5e93\u6216\u529f\u80fd\uff0c\u5efa\u8bae\u5728\u66f4\u6362\u89e3\u91ca\u5668\u4e4b\u524d\u68c0\u67e5\u8fd9\u4e9b\u4f9d\u8d56\u662f\u5426\u4e0e\u65b0\u89e3\u91ca\u5668\u517c\u5bb9\u3002\u4f7f\u7528\u865a\u62df\u73af\u5883\u53ef\u4ee5\u5e2e\u52a9\u9694\u79bb\u8fd9\u4e9b\u5f71\u54cd\uff0c\u786e\u4fdd\u4e0d\u540c\u9879\u76ee\u4e4b\u95f4\u7684\u517c\u5bb9\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728IDE\u4e2d\u66f4\u6539Python\u89e3\u91ca\u5668\uff1f<\/strong><br \/>\u5728\u8bb8\u591a\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u4e2d\uff0c\u4f8b\u5982PyCharm\u6216Visual Studio Code\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u8bbe\u7f6e\u4e2d\u8f7b\u677e\u66f4\u6539Python\u89e3\u91ca\u5668\u3002\u5728PyCharm\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7File -&gt; Settings -&gt; Project -&gt; Project Interpreter\u6765\u9009\u62e9\u4e0d\u540c\u7684\u89e3\u91ca\u5668\u3002\u5728VS Code\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u9762\u677f\uff08Ctrl + Shift + P\uff09\u5e76\u8f93\u5165\u201cPython: Select Interpreter\u201d\u6765\u66f4\u6539\u89e3\u91ca\u5668\u3002\u786e\u4fdd\u6240\u9009\u7684\u89e3\u91ca\u5668\u4e0e\u9879\u76ee\u7684\u9700\u6c42\u4e00\u81f4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u66f4\u6362Python\u89e3\u91ca\u5668\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883\u3001\u901a\u8fc7IDE\u8fdb\u884c\u8bbe\u7f6e\u6765\u5b9e\u73b0\uff0c\u5176\u4e2d\u4f7f\u7528\u865a\u62df\u73af\u5883\u662f\u6700\u4e3a [&hellip;]","protected":false},"author":3,"featured_media":980273,"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\/980260"}],"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=980260"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/980260\/revisions"}],"predecessor-version":[{"id":980278,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/980260\/revisions\/980278"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/980273"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=980260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=980260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=980260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}