{"id":1178258,"date":"2025-01-15T18:08:38","date_gmt":"2025-01-15T10:08:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1178258.html"},"modified":"2025-01-15T18:08:41","modified_gmt":"2025-01-15T10:08:41","slug":"%e5%a6%82%e4%bd%95%e4%b8%8b%e8%bd%bdpython%e8%99%9a%e6%8b%9f%e6%9c%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1178258.html","title":{"rendered":"\u5982\u4f55\u4e0b\u8f7dpython\u865a\u62df\u673a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25113000\/772a1429-e0ff-4ffb-aef7-da58668ad096.webp\" alt=\"\u5982\u4f55\u4e0b\u8f7dpython\u865a\u62df\u673a\" \/><\/p>\n<p><p> <strong>\u4e0b\u8f7dPython\u865a\u62df\u673a\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528Anaconda\u3001pyenv\u3001Virtualenv\u7b49\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u4f60\u521b\u5efa\u548c\u7ba1\u7406Python\u865a\u62df\u73af\u5883\u3001\u786e\u4fdd\u9879\u76ee\u7684\u72ec\u7acb\u6027\u3002<\/strong>\u5176\u4e2d\uff0cAnaconda\u662f\u4e00\u79cd\u6d41\u884c\u7684\u9009\u62e9\uff0c\u5b83\u4e0d\u4ec5\u63d0\u4f9b\u4e86\u4e00\u4e2a\u865a\u62df\u73af\u5883\u7ba1\u7406\u5668\uff0c\u8fd8\u5305\u62ec\u4e86\u8bb8\u591a\u79d1\u5b66\u8ba1\u7b97\u5e93\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5982\u4f55\u4f7f\u7528Anaconda\u4e0b\u8f7d\u548c\u7ba1\u7406Python\u865a\u62df\u673a\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4e0b\u8f7d\u548c\u5b89\u88c5Anaconda<\/h3>\n<\/p>\n<p><h4>1.1 \u4e0b\u8f7dAnaconda<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u4eceAnaconda\u7684\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u9002\u5408\u4f60\u64cd\u4f5c\u7cfb\u7edf\u7684\u5b89\u88c5\u5305\u3002\u8bbf\u95ee<a href=\"https:\/\/www.anaconda.com\/products\/distribution\">Anaconda\u5b98\u7f51<\/a>\u5e76\u9009\u62e9\u9002\u5408\u4f60\u7cfb\u7edf\u7684\u7248\u672c\uff08Windows\u3001macOS\u6216Linux\uff09\u3002\u70b9\u51fb\u4e0b\u8f7d\u6309\u94ae\u5e76\u4fdd\u5b58\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u5b89\u88c5Anaconda<\/h4>\n<\/p>\n<p><p>\u4e0b\u8f7d\u5b8c\u6210\u540e\uff0c\u53cc\u51fb\u4e0b\u8f7d\u7684\u5b89\u88c5\u5305\uff0c\u6309\u7167\u5b89\u88c5\u5411\u5bfc\u7684\u6b65\u9aa4\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>Windows<\/strong>\uff1a\u8fd0\u884c\u4e0b\u8f7d\u7684<code>.exe<\/code>\u6587\u4ef6\uff0c\u6309\u7167\u63d0\u793a\u5b8c\u6210\u5b89\u88c5\u3002\u5efa\u8bae\u5c06Anaconda\u5b89\u88c5\u5728\u9ed8\u8ba4\u8def\u5f84\uff0c\u5e76\u5c06Anaconda\u6dfb\u52a0\u5230\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/li>\n<li><strong>macOS<\/strong>\uff1a\u6253\u5f00\u7ec8\u7aef\uff0c\u8fd0\u884c\u4e0b\u8f7d\u7684<code>.pkg<\/code>\u6587\u4ef6\uff0c\u6309\u7167\u63d0\u793a\u5b8c\u6210\u5b89\u88c5\u3002<\/li>\n<li><strong>Linux<\/strong>\uff1a\u6253\u5f00\u7ec8\u7aef\uff0c\u8fd0\u884c\u4e0b\u8f7d\u7684<code>.sh<\/code>\u6587\u4ef6\uff0c\u4f7f\u7528\u547d\u4ee4<code>bash Anaconda3-x.x.x-Linux-x86_64.sh<\/code>\uff0c\u7136\u540e\u6309\u7167\u63d0\u793a\u5b8c\u6210\u5b89\u88c5\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u548c\u7ba1\u7406\u865a\u62df\u73af\u5883<\/h3>\n<\/p>\n<p><h4>2.1 \u521b\u5efa\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Anaconda\u521b\u5efa\u865a\u62df\u73af\u5883\u3002\u6253\u5f00\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">conda create -n myenv python=3.8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6761\u547d\u4ee4\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>myenv<\/code>\u7684\u865a\u62df\u73af\u5883\uff0c\u5e76\u6307\u5b9aPython\u7248\u672c\u4e3a3.8\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u66f4\u6539\u73af\u5883\u540d\u79f0\u548cPython\u7248\u672c\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u5b8c\u6210\u540e\uff0c\u4f60\u9700\u8981\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u624d\u80fd\u4f7f\u7528\u5b83\u3002\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>Windows<\/strong>\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-sh\">conda activate myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>macOS\u548cLinux<\/strong>\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-sh\">source activate myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6fc0\u6d3b\u540e\uff0c\u4f60\u7684\u547d\u4ee4\u63d0\u793a\u7b26\u4f1a\u663e\u793a\u5f53\u524d\u865a\u62df\u73af\u5883\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><h4>2.3 \u5b89\u88c5\u5305<\/h4>\n<\/p>\n<p><p>\u5728\u6fc0\u6d3b\u7684\u865a\u62df\u73af\u5883\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>conda<\/code>\u6216<code>pip<\/code>\u6765\u5b89\u88c5\u6240\u9700\u7684\u5305\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">conda install numpy<\/p>\n<p>pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.4 \u9000\u51fa\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u5b8c\u6210\u5de5\u4f5c\u540e\uff0c\u4f60\u53ef\u4ee5\u9000\u51fa\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">conda deactivate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528pyenv\u548cVirtualenv<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Anaconda\uff0c<code>pyenv<\/code>\u548c<code>Virtualenv<\/code>\u4e5f\u662f\u5e38\u7528\u7684Python\u865a\u62df\u73af\u5883\u7ba1\u7406\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u5b89\u88c5pyenv<\/h4>\n<\/p>\n<p><p><code>pyenv<\/code>\u662f\u4e00\u4e2a\u7b80\u5355\u7684Python\u7248\u672c\u7ba1\u7406\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u591a\u4e2aPython\u7248\u672c\u4e4b\u95f4\u5207\u6362\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>macOS\u548cLinux<\/strong>\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-sh\">curl https:\/\/pyenv.run | bash<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>Windows<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>pyenv-win<\/code>\uff0c\u8fd9\u662f\u4e00\u4e2a\u9002\u7528\u4e8eWindows\u7684<code>pyenv<\/code>\u7248\u672c\u3002\u5b89\u88c5\u65b9\u6cd5\u53ef\u4ee5\u53c2\u8003\u5176<a href=\"https:\/\/github.com\/pyenv-win\/pyenv-win\">GitHub\u9875\u9762<\/a>\u3002<\/li>\n<\/ul>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6309\u7167\u63d0\u793a\u5c06<code>pyenv<\/code>\u6dfb\u52a0\u5230\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4e2d\uff0c\u5e76\u91cd\u65b0\u542f\u52a8\u7ec8\u7aef\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u5b89\u88c5Python\u7248\u672c<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>pyenv<\/code>\u5b89\u88c5\u6307\u5b9a\u7684Python\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pyenv install 3.8.10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.3 \u8bbe\u7f6e\u5168\u5c40\u6216\u5c40\u90e8Python\u7248\u672c<\/h4>\n<\/p>\n<p><pre><code class=\"language-sh\">pyenv global 3.8.10  # \u8bbe\u7f6e\u5168\u5c40Python\u7248\u672c<\/p>\n<p>pyenv local 3.8.10   # \u8bbe\u7f6e\u5f53\u524d\u76ee\u5f55\u7684Python\u7248\u672c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.4 \u5b89\u88c5Virtualenv<\/h4>\n<\/p>\n<p><p>\u5728\u5b89\u88c5\u4e86<code>pyenv<\/code>\u540e\uff0c\u4f60\u8fd8\u53ef\u4ee5\u5b89\u88c5<code>Virtualenv<\/code>\u6765\u521b\u5efa\u548c\u7ba1\u7406\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install virtualenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.5 \u521b\u5efa\u548c\u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><pre><code class=\"language-sh\">virtualenv myenv  # \u521b\u5efa\u865a\u62df\u73af\u5883<\/p>\n<p>source myenv\/bin\/activate  # \u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff08macOS\u548cLinux\uff09<\/p>\n<p>myenv\\Scripts\\activate  # \u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff08Windows\uff09<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9000\u51fa\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">deactivate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528Anaconda\u3001pyenv\u548cVirtualenv\uff0c\u4f60\u53ef\u4ee5\u65b9\u4fbf\u5730\u4e0b\u8f7d\u548c\u7ba1\u7406Python\u865a\u62df\u673a\u3002<strong>Anaconda\u9002\u5408\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5305\u7ba1\u7406\u529f\u80fd\u3001pyenv\u9002\u5408\u9700\u8981\u7ba1\u7406\u591a\u4e2aPython\u7248\u672c\u7684\u7528\u6237\u3001Virtualenv\u5219\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u865a\u62df\u73af\u5883\u7ba1\u7406\u5de5\u5177\u3002<\/strong>\u9009\u62e9\u9002\u5408\u4f60\u7684\u5de5\u5177\u5c06\u5927\u5927\u63d0\u9ad8\u4f60\u7684\u5f00\u53d1\u6548\u7387\u548c\u9879\u76ee\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u4f60\u9009\u62e9\u54ea\u79cd\u5de5\u5177\uff0c\u90fd\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u548c\u7ba1\u7406\u865a\u62df\u73af\u5883\u6765\u786e\u4fdd\u9879\u76ee\u7684\u72ec\u7acb\u6027\uff0c\u907f\u514d\u5305\u7248\u672c\u51b2\u7a81\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u865a\u62df\u73af\u5883\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u9002\u5408\u6211\u7684\u64cd\u4f5c\u7cfb\u7edf\u7684Python\u865a\u62df\u73af\u5883\uff1f<\/strong><br \/>\u5728\u4e0b\u8f7dPython\u865a\u62df\u673a\u4e4b\u524d\uff0c\u4e86\u89e3\u81ea\u5df1\u7684\u64cd\u4f5c\u7cfb\u7edf\u975e\u5e38\u91cd\u8981\u3002Windows\u3001macOS\u548cLinux\u90fd\u6709\u4e0d\u540c\u7684\u5b89\u88c5\u65b9\u6cd5\u3002\u5bf9\u4e8eWindows\u7528\u6237\uff0c\u53ef\u4ee5\u4f7f\u7528Anaconda\u6216\u76f4\u63a5\u4ecePython\u5b98\u7f51\u4e0b\u8f7d\u5b89\u88c5\u5305\uff1bmacOS\u7528\u6237\u53ef\u4ee5\u901a\u8fc7Homebrew\u8f7b\u677e\u5b89\u88c5\uff1b\u800cLinux\u7528\u6237\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u5b89\u88c5Python\u548c\u76f8\u5173\u7684\u865a\u62df\u73af\u5883\u5de5\u5177\u3002<\/p>\n<p><strong>Python\u865a\u62df\u73af\u5883\u6709\u4ec0\u4e48\u597d\u5904\uff1f<\/strong><br \/>\u4f7f\u7528Python\u865a\u62df\u73af\u5883\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u4e0d\u540c\u9879\u76ee\u4e4b\u95f4\u9694\u79bb\u4f9d\u8d56\u5305\uff0c\u907f\u514d\u7248\u672c\u51b2\u7a81\u3002\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u5728\u540c\u4e00\u53f0\u673a\u5668\u4e0a\u8fd0\u884c\u591a\u4e2a\u9879\u76ee\uff0c\u6bcf\u4e2a\u9879\u76ee\u90fd\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7248\u672c\u7684\u5e93\u800c\u4e0d\u4e92\u76f8\u5e72\u6270\u3002\u6b64\u5916\uff0c\u865a\u62df\u73af\u5883\u8fd8\u80fd\u7b80\u5316\u9879\u76ee\u7684\u7ba1\u7406\u548c\u90e8\u7f72\uff0c\u4f7f\u5f97\u56e2\u961f\u5408\u4f5c\u66f4\u52a0\u987a\u5229\u3002<\/p>\n<p><strong>\u5728Python\u865a\u62df\u73af\u5883\u4e2d\u5982\u4f55\u5b89\u88c5\u989d\u5916\u7684\u5e93\u548c\u4f9d\u8d56\uff1f<\/strong><br \/>\u521b\u5efa\u5b8cPython\u865a\u62df\u73af\u5883\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<code>pip<\/code>\u547d\u4ee4\u5b89\u88c5\u6240\u9700\u7684\u5e93\u3002\u8fdb\u5165\u865a\u62df\u73af\u5883\u540e\uff0c\u4f7f\u7528<code>pip install package_name<\/code>\u547d\u4ee4\u6765\u5b89\u88c5\u7279\u5b9a\u7684\u5e93\uff0c\u6bd4\u5982<code>pip install numpy<\/code>\u3002\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u9879\u76ee\u7684\u4f9d\u8d56\u6587\u4ef6\uff08\u5982<code>requirements.txt<\/code>\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pip install -r requirements.txt<\/code>\u4e00\u6b21\u6027\u5b89\u88c5\u6240\u6709\u4f9d\u8d56\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u9879\u76ee\u73af\u5883\u7684\u4e00\u81f4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e0b\u8f7dPython\u865a\u62df\u673a\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528Anaconda\u3001pyenv\u3001Virtualenv\u7b49\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5 [&hellip;]","protected":false},"author":3,"featured_media":1178265,"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\/1178258"}],"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=1178258"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1178258\/revisions"}],"predecessor-version":[{"id":1178268,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1178258\/revisions\/1178268"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1178265"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1178258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1178258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1178258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}