{"id":1002133,"date":"2024-12-27T10:06:04","date_gmt":"2024-12-27T02:06:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1002133.html"},"modified":"2024-12-27T10:06:07","modified_gmt":"2024-12-27T02:06:07","slug":"python%e5%a6%82%e4%bd%95%e6%b0%b8%e4%b9%85%e6%bf%80%e6%b4%bb%e7%8e%af%e5%a2%83","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1002133.html","title":{"rendered":"python\u5982\u4f55\u6c38\u4e45\u6fc0\u6d3b\u73af\u5883"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080016\/268ff053-6db0-4988-95fb-86de2eb16523.webp\" alt=\"python\u5982\u4f55\u6c38\u4e45\u6fc0\u6d3b\u73af\u5883\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u6c38\u4e45\u6fc0\u6d3b\u73af\u5883\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4fee\u6539\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u3001\u4f7f\u7528shell\u542f\u52a8\u811a\u672c\u3001\u914d\u7f6eIDE\u7684\u9ed8\u8ba4\u73af\u5883<\/strong>\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u901a\u8fc7\u4fee\u6539\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u6c38\u4e45\u6fc0\u6d3bPython\u73af\u5883\u3002<\/p>\n<\/p>\n<p><p>\u8981\u6c38\u4e45\u6fc0\u6d3bPython\u73af\u5883\uff0c\u9996\u5148\u9700\u8981\u7406\u89e3Python\u865a\u62df\u73af\u5883\u7684\u57fa\u7840\u77e5\u8bc6\u3002Python\u865a\u62df\u73af\u5883\u662f\u4e00\u79cd\u5de5\u5177\uff0c\u5b83\u80fd\u591f\u4e3a\u6bcf\u4e2a\u9879\u76ee\u521b\u5efa\u4e00\u4e2a\u72ec\u7acb\u7684Python\u8fd0\u884c\u73af\u5883\uff0c\u5176\u4e2d\u5305\u542b\u7279\u5b9a\u7248\u672c\u7684Python\u89e3\u91ca\u5668\u548c\u4e00\u7ec4\u7279\u5b9a\u7248\u672c\u7684\u5e93\u3002\u901a\u8fc7\u4f7f\u7528\u865a\u62df\u73af\u5883\uff0c\u53ef\u4ee5\u907f\u514d\u4e0d\u540c\u9879\u76ee\u4e4b\u95f4\u7684\u4f9d\u8d56\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4fee\u6539\u7cfb\u7edf\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4fee\u6539\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\uff0c\u53ef\u4ee5\u8ba9\u7cfb\u7edf\u5728\u542f\u52a8\u65f6\u81ea\u52a8\u6fc0\u6d3b\u6307\u5b9a\u7684Python\u865a\u62df\u73af\u5883\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u4e00\u4e2aPython\u865a\u62df\u73af\u5883<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u865a\u62df\u73af\u5883\u3002\u53ef\u4ee5\u4f7f\u7528<code>venv<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m venv myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u547d\u4ee4\u5c06\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>myenv<\/code>\u7684\u865a\u62df\u73af\u5883\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u83b7\u53d6\u6fc0\u6d3b\u811a\u672c\u8def\u5f84<\/strong><\/p>\n<\/p>\n<p><p>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u9700\u8981\u8fd0\u884c\u6fc0\u6d3b\u811a\u672c\u3002\u811a\u672c\u7684\u8def\u5f84\u53d6\u51b3\u4e8e\u60a8\u7684\u64cd\u4f5c\u7cfb\u7edf\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5728Windows\u4e0a\uff0c\u6fc0\u6d3b\u811a\u672c\u8def\u5f84\u4e3a\uff1a<code>myenv\\Scripts\\activate.bat<\/code><\/li>\n<li>\u5728Unix\u6216MacOS\u4e0a\uff0c\u6fc0\u6d3b\u811a\u672c\u8def\u5f84\u4e3a\uff1a<code>myenv\/bin\/activate<\/code><\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u4fee\u6539\u7cfb\u7edf\u73af\u5883\u53d8\u91cf<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u5728\u7cfb\u7edf\u542f\u52a8\u65f6\u81ea\u52a8\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff0c\u60a8\u53ef\u4ee5\u5c06\u6fc0\u6d3b\u811a\u672c\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684\u542f\u52a8\u811a\u672c\u4e2d\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>Windows<\/strong>: \u60a8\u53ef\u4ee5\u7f16\u8f91\u7cfb\u7edf\u7684<code>Autoexec.bat<\/code>\u6587\u4ef6\uff08\u5bf9\u4e8e\u8f83\u65e7\u7684Windows\u7248\u672c\uff09\u6216\u4f7f\u7528\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u5728\u7528\u6237\u767b\u5f55\u65f6\u8fd0\u884c\u6279\u5904\u7406\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>Unix\u6216MacOS<\/strong>: \u60a8\u53ef\u4ee5\u5728<code>~\/.bashrc<\/code>\u6216<code>~\/.bash_profile<\/code>\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">source \/path\/to\/myenv\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c\u6bcf\u6b21\u6253\u5f00\u7ec8\u7aef\u65f6\uff0c\u90fd\u4f1a\u81ea\u52a8\u6fc0\u6d3b\u6307\u5b9a\u7684\u865a\u62df\u73af\u5883\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u4f7f\u7528shell\u542f\u52a8\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u521b\u5efa\u4e00\u4e2ashell\u811a\u672c\uff0c\u6bcf\u6b21\u542f\u52a8shell\u65f6\u81ea\u52a8\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7f16\u5199\u542f\u52a8\u811a\u672c<\/strong><\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>startenv.sh<\/code>\u7684\u811a\u672c\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">#!\/bin\/bash<\/p>\n<p>source \/path\/to\/myenv\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u811a\u672c\u5c06\u6fc0\u6d3b\u6307\u5b9a\u7684\u865a\u62df\u73af\u5883\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bbe\u7f6e\u811a\u672c\u7684\u6267\u884c\u6743\u9650<\/strong><\/p>\n<\/p>\n<p><p>\u5728Unix\u6216MacOS\u7cfb\u7edf\u4e0a\uff0c\u60a8\u9700\u8981\u4e3a\u811a\u672c\u8bbe\u7f6e\u6267\u884c\u6743\u9650\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">chmod +x startenv.sh<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c\u542f\u52a8\u811a\u672c<\/strong><\/p>\n<\/p>\n<p><p>\u6bcf\u6b21\u9700\u8981\u6fc0\u6d3b\u73af\u5883\u65f6\uff0c\u53ea\u9700\u8fd0\u884c\u8be5\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">.\/startenv.sh<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u914d\u7f6eIDE\u7684\u9ed8\u8ba4\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u60a8\u4f7f\u7528IDE\u8fdb\u884c\u5f00\u53d1\uff0c\u53ef\u4ee5\u5728IDE\u4e2d\u8bbe\u7f6e\u9ed8\u8ba4\u7684Python\u89e3\u91ca\u5668\u4e3a\u865a\u62df\u73af\u5883\u4e2d\u7684\u89e3\u91ca\u5668\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>PyCharm<\/strong><\/p>\n<\/p>\n<p><p>\u5728PyCharm\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8bbe\u7f6e\u9ed8\u8ba4Python\u89e3\u91ca\u5668\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00PyCharm\uff0c\u8fdb\u5165\u9879\u76ee\u8bbe\u7f6e\u3002<\/li>\n<li>\u5728\u201cProject Interpreter\u201d\u4e2d\uff0c\u9009\u62e9\u201cAdd Interpreter\u201d\u3002<\/li>\n<li>\u9009\u62e9\u201cExisting Environment\u201d\uff0c\u7136\u540e\u9009\u62e9\u60a8\u7684\u865a\u62df\u73af\u5883\u89e3\u91ca\u5668\uff08\u901a\u5e38\u5728<code>myenv\/bin\/python<\/code>\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>Visual Studio Code<\/strong><\/p>\n<\/p>\n<p><p>\u5728VS Code\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8bbe\u7f6e\u9ed8\u8ba4Python\u89e3\u91ca\u5668\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00\u547d\u4ee4\u9762\u677f\uff08Ctrl+Shift+P\uff09\u3002<\/li>\n<li>\u8f93\u5165\u5e76\u9009\u62e9\u201cPython: Select Interpreter\u201d\u3002<\/li>\n<li>\u9009\u62e9\u60a8\u7684\u865a\u62df\u73af\u5883\u89e3\u91ca\u5668\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u5f00\u53d1\u73af\u5883\u4e2d\u5b9e\u73b0Python\u865a\u62df\u73af\u5883\u7684\u6c38\u4e45\u6fc0\u6d3b\u3002\u8fd9\u4e0d\u4ec5\u6709\u52a9\u4e8e\u7b80\u5316\u5f00\u53d1\u8fc7\u7a0b\uff0c\u8fd8\u80fd\u6709\u6548\u7ba1\u7406\u9879\u76ee\u7684\u4f9d\u8d56\u5173\u7cfb\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u6c38\u4e45\u6fc0\u6d3b\u7684\u865a\u62df\u73af\u5883\uff1f<\/strong><br \/>\u8981\u521b\u5efa\u4e00\u4e2a\u6c38\u4e45\u6fc0\u6d3b\u7684\u865a\u62df\u73af\u5883\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>venv<\/code>\u6a21\u5757\u521b\u5efa\u4e00\u4e2a\u865a\u62df\u73af\u5883\uff0c\u5e76\u901a\u8fc7\u4fee\u6539\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u6216\u5728\u542f\u52a8\u811a\u672c\u4e2d\u6dfb\u52a0\u6fc0\u6d3b\u547d\u4ee4\u6765\u5b9e\u73b0\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u521b\u5efa\u865a\u62df\u73af\u5883\u540e\uff0c\u5c06\u6fc0\u6d3b\u547d\u4ee4\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684\u542f\u52a8\u9879\u4e2d\uff0c\u786e\u4fdd\u6bcf\u6b21\u542f\u52a8\u65f6\u81ea\u52a8\u6fc0\u6d3b\u3002<\/p>\n<p><strong>\u5728Windows\u548cLinux\u7cfb\u7edf\u4e2d\uff0c\u5982\u4f55\u7b80\u5316\u865a\u62df\u73af\u5883\u7684\u6fc0\u6d3b\u8fc7\u7a0b\uff1f<\/strong><br \/>\u5728Windows\u548cLinux\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u6279\u5904\u7406\u6587\u4ef6\u6216Shell\u811a\u672c\u6765\u7b80\u5316\u865a\u62df\u73af\u5883\u7684\u6fc0\u6d3b\u8fc7\u7a0b\u3002\u4f8b\u5982\uff0c\u5728Windows\u4e2d\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a.bat\u6587\u4ef6\uff0c\u5185\u5bb9\u4e3a\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u7684\u547d\u4ee4\uff1b\u5728Linux\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5728.bashrc\u6587\u4ef6\u4e2d\u6dfb\u52a0\u6fc0\u6d3b\u547d\u4ee4\uff0c\u786e\u4fdd\u6bcf\u6b21\u6253\u5f00\u7ec8\u7aef\u65f6\u81ea\u52a8\u6267\u884c\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528Jupyter Notebook\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u4f7f\u7528\u6307\u5b9a\u7684Python\u865a\u62df\u73af\u5883\uff1f<\/strong><br \/>\u4e3a\u4e86\u5728Jupyter Notebook\u4e2d\u4f7f\u7528\u7279\u5b9a\u7684Python\u865a\u62df\u73af\u5883\uff0c\u60a8\u9700\u8981\u5728\u8be5\u73af\u5883\u4e2d\u5b89\u88c5<code>ipykernel<\/code>\uff0c\u5e76\u4f7f\u7528\u547d\u4ee4\u5c06\u5176\u6dfb\u52a0\u5230Jupyter\u7684\u5185\u6838\u5217\u8868\u4e2d\u3002\u901a\u8fc7\u8fd0\u884c<code>python -m ipykernel install --user --name=myenv<\/code>\uff0c\u60a8\u53ef\u4ee5\u5728Notebook\u4e2d\u9009\u62e9\u521a\u521b\u5efa\u7684\u5185\u6838\uff0c\u4ece\u800c\u4f7f\u7528\u8be5\u865a\u62df\u73af\u5883\u7684\u5305\u548c\u8bbe\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u6c38\u4e45\u6fc0\u6d3b\u73af\u5883\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4fee\u6539\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u3001\u4f7f\u7528shell\u542f\u52a8\u811a\u672c\u3001\u914d\u7f6eIDE\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1002142,"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\/1002133"}],"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=1002133"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1002133\/revisions"}],"predecessor-version":[{"id":1002146,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1002133\/revisions\/1002146"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1002142"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1002133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1002133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1002133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}