{"id":976737,"date":"2024-12-27T06:24:00","date_gmt":"2024-12-26T22:24:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/976737.html"},"modified":"2024-12-27T06:24:02","modified_gmt":"2024-12-26T22:24:02","slug":"%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8conda%e7%9a%84python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/976737.html","title":{"rendered":"\u5982\u4f55\u8c03\u7528conda\u7684python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24202315\/a45bd6bb-8f76-4eee-9d39-ef07a5696931.webp\" alt=\"\u5982\u4f55\u8c03\u7528conda\u7684python\" \/><\/p>\n<p><p> <strong>\u8c03\u7528Conda\u7684Python\u6709\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528Conda\u3001\u901a\u8fc7Conda\u73af\u5883\u6fc0\u6d3b\u3001\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u7b49\u3002\u901a\u8fc7\u5728\u547d\u4ee4\u884c\u4e2d\u6fc0\u6d3bConda\u73af\u5883\u3001\u4f7f\u7528Conda\u73af\u5883\u7ba1\u7406\u5de5\u5177\u3001\u76f4\u63a5\u8fd0\u884cConda\u8def\u5f84\u4e0b\u7684Python\u89e3\u91ca\u5668\uff0c\u7528\u6237\u53ef\u4ee5\u7075\u6d3b\u5730\u5728\u4e0d\u540c\u7684\u9879\u76ee\u4e2d\u5207\u6362Python\u7248\u672c\u548c\u4f9d\u8d56\u3002<\/strong>\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u901a\u8fc7\u547d\u4ee4\u884c\u6fc0\u6d3bConda\u73af\u5883<\/p>\n<\/p>\n<p><p>\u8981\u8c03\u7528Conda\u7684Python\uff0c\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u662f\u901a\u8fc7\u547d\u4ee4\u884c\u6fc0\u6d3b\u76f8\u5e94\u7684Conda\u73af\u5883\u3002Conda\u73af\u5883\u662f\u72ec\u7acb\u7684Python\u73af\u5883\uff0c\u5141\u8bb8\u7528\u6237\u4e3a\u6bcf\u4e2a\u9879\u76ee\u5b89\u88c5\u4e0d\u540c\u7684Python\u7248\u672c\u548c\u4f9d\u8d56\u5e93\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efaConda\u73af\u5883<\/strong><br \/>\u5728\u4f7f\u7528Conda\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5Miniconda\u6216Anaconda\u3002\u7136\u540e\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u65b0\u7684Conda\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">conda create --name myenv python=3.8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>myenv<\/code>\u7684\u73af\u5883\uff0c\u5e76\u5b89\u88c5Python 3.8\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6fc0\u6d3bConda\u73af\u5883<\/strong><br \/>\u5728\u521b\u5efa\u73af\u5883\u4e4b\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6fc0\u6d3b\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">conda activate myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6fc0\u6d3b\u73af\u5883\u540e\uff0c\u7ec8\u7aef\u63d0\u793a\u7b26\u5c06\u4f1a\u6539\u53d8\uff0c\u663e\u793a\u5f53\u524d\u6fc0\u6d3b\u7684\u73af\u5883\u540d\u79f0\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Python\u89e3\u91ca\u5668<\/strong><br \/>\u4e00\u65e6\u73af\u5883\u88ab\u6fc0\u6d3b\uff0c\u6240\u6709\u5728\u8be5\u73af\u5883\u4e2d\u5b89\u88c5\u7684Python\u7248\u672c\u548c\u5305\u5c06\u88ab\u4f7f\u7528\u3002\u53ef\u4ee5\u76f4\u63a5\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884cPython\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u542f\u52a8Conda\u73af\u5883\u4e2d\u7684Python\u89e3\u91ca\u5668\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528Conda\u73af\u5883\u7ba1\u7406\u5de5\u5177<\/p>\n<\/p>\n<p><p>Conda\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u73af\u5883\u7ba1\u7406\u5de5\u5177\uff0c\u4f7f\u5f97\u5728\u4e0d\u540c\u9879\u76ee\u4e2d\u5207\u6362Python\u7248\u672c\u548c\u4f9d\u8d56\u53d8\u5f97\u7b80\u5355\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5217\u51fa\u6240\u6709\u73af\u5883<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5217\u51fa\u6240\u6709\u53ef\u7528\u7684Conda\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">conda env list<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u663e\u793a\u6240\u6709\u5df2\u521b\u5efa\u7684Conda\u73af\u5883\u53ca\u5176\u8def\u5f84\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5\u548c\u7ba1\u7406\u5305<\/strong><br \/>\u5728\u6fc0\u6d3b\u67d0\u4e2a\u73af\u5883\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Conda\u7ba1\u7406\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">conda install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u5b89\u88c5<code>numpy<\/code>\u5e93\u5230\u5f53\u524d\u6fc0\u6d3b\u7684\u73af\u5883\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u66f4\u65b0\u548c\u5220\u9664\u73af\u5883<\/strong><br \/>\u53ef\u4ee5\u66f4\u65b0\u73af\u5883\u4e2d\u7684\u6240\u6709\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">conda update --all<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\u5220\u9664\u67d0\u4e2a\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">conda remove --name myenv --all<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u76f4\u63a5\u8fd0\u884cConda\u8def\u5f84\u4e0b\u7684Python\u89e3\u91ca\u5668<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u53ef\u80fd\u9700\u8981\u5728\u4e0d\u6fc0\u6d3b\u73af\u5883\u7684\u60c5\u51b5\u4e0b\u8fd0\u884c\u67d0\u4e2a\u7279\u5b9a\u7684Python\u7248\u672c\u3002\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u8c03\u7528Conda\u73af\u5883\u4e2d\u7684Python\u89e3\u91ca\u5668\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u67e5\u627ePython\u89e3\u91ca\u5668\u8def\u5f84<\/strong><br \/>\u6bcf\u4e2aConda\u73af\u5883\u90fd\u6709\u81ea\u5df1\u7684Python\u89e3\u91ca\u5668\uff0c\u53ef\u4ee5\u901a\u8fc7\u67e5\u627e\u73af\u5883\u76ee\u5f55\u6765\u786e\u5b9a\u5176\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">\/path\/to\/miniconda3\/envs\/myenv\/bin\/python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Windows\u4e0a\uff0c\u8def\u5f84\u53ef\u80fd\u7c7b\u4f3c\u4e8e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">C:\\path\\to\\miniconda3\\envs\\myenv\\python.exe<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u76f4\u63a5\u8fd0\u884cPython\u811a\u672c<\/strong><br \/>\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8be5\u8def\u5f84\u6765\u8fd0\u884cPython\u811a\u672c\uff0c\u800c\u4e0d\u9700\u8981\u6fc0\u6d3b\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">\/path\/to\/miniconda3\/envs\/myenv\/bin\/python myscript.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u4f7f\u7528\u6307\u5b9aConda\u73af\u5883\u4e2d\u7684Python\u89e3\u91ca\u5668\u6765\u8fd0\u884c\u811a\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u914d\u7f6e\u73af\u5883\u53d8\u91cf<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u914d\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7fConda\u7684Python\u89e3\u91ca\u5668\u6210\u4e3a\u7cfb\u7edf\u9ed8\u8ba4\u7684Python\u89e3\u91ca\u5668\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4fee\u6539\u7cfb\u7edf\u8def\u5f84<\/strong><br \/>\u5c06Conda\u73af\u5883\u4e2d\u7684Python\u8def\u5f84\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684PATH\u53d8\u91cf\u4e2d\u3002\u8fd9\u6837\u5728\u8fd0\u884c<code>python<\/code>\u547d\u4ee4\u65f6\uff0c\u7cfb\u7edf\u4f1a\u9ed8\u8ba4\u4f7f\u7528Conda\u7684Python\u89e3\u91ca\u5668\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5728bashrc\u6216zshrc\u4e2d\u914d\u7f6e<\/strong><br \/>\u5728Linux\u6216macOS\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u5728<code>.bashrc<\/code>\u6216<code>.zshrc<\/code>\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export PATH=&quot;\/path\/to\/miniconda3\/envs\/myenv\/bin:$PATH&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u4f7f\u5f97\u5728\u6253\u5f00\u65b0\u7684\u7ec8\u7aef\u4f1a\u8bdd\u65f6\uff0c\u81ea\u52a8\u4f7f\u7528\u6307\u5b9a\u7684Conda\u73af\u5883\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>Windows\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf<\/strong><br \/>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u201c\u7cfb\u7edf\u5c5e\u6027\u201d-&gt;\u201c\u9ad8\u7ea7\u201d-&gt;\u201c\u73af\u5883\u53d8\u91cf\u201d\u6765\u4fee\u6539\u7cfb\u7edf\u7684PATH\u53d8\u91cf\uff0c\u5c06Conda\u73af\u5883\u8def\u5f84\u6dfb\u52a0\u5230PATH\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u4f7f\u7528Jupyter Notebook\u6216\u5176\u4ed6IDE<\/p>\n<\/p>\n<p><p>\u5f88\u591a\u7528\u6237\u559c\u6b22\u5728Jupyter Notebook\u6216IDE\u4e2d\u4f7f\u7528Conda\u7684Python\u89e3\u91ca\u5668\uff0c\u8fd9\u9700\u8981\u4e00\u4e9b\u989d\u5916\u7684\u914d\u7f6e\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5728Jupyter Notebook\u4e2d\u4f7f\u7528Conda\u73af\u5883<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5c06Conda\u73af\u5883\u6dfb\u52a0\u4e3aJupyter Notebook\u7684\u5185\u6838\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">conda install ipykernel<\/p>\n<p>python -m ipykernel install --user --name=myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u4f7f\u5f97Jupyter Notebook\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528<code>myenv<\/code>\u73af\u5883\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6eIDE<\/strong><br \/>\u4e0d\u540c\u7684IDE\u6709\u4e0d\u540c\u7684\u914d\u7f6e\u65b9\u5f0f\u6765\u6307\u5b9a\u4f7f\u7528\u7684Python\u89e3\u91ca\u5668\u3002\u4f8b\u5982\uff0c\u5728VSCode\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>.vscode\/settings.json<\/code>\u6587\u4ef6\u6307\u5b9aPython\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-json\">{<\/p>\n<p>  &quot;python.pythonPath&quot;: &quot;\/path\/to\/miniconda3\/envs\/myenv\/bin\/python&quot;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u7528\u6237\u53ef\u4ee5\u7075\u6d3b\u5730\u8c03\u7528Conda\u7684Python\u89e3\u91ca\u5668\uff0c\u5e76\u5728\u4e0d\u540c\u7684\u9879\u76ee\u4e2d\u7ba1\u7406\u4e0d\u540c\u7248\u672c\u7684Python\u548c\u4f9d\u8d56\u3002Conda\u63d0\u4f9b\u7684\u73af\u5883\u9694\u79bb\u548c\u5305\u7ba1\u7406\u529f\u80fd\uff0c\u4f7f\u5f97Python\u5f00\u53d1\u53d8\u5f97\u66f4\u52a0\u9ad8\u6548\u548c\u4fbf\u6377\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728\u7ec8\u7aef\u4e2d\u786e\u8ba4conda\u7684python\u7248\u672c\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165<code>conda list python<\/code>\u6765\u67e5\u770b\u5f53\u524d\u73af\u5883\u4e2d\u5b89\u88c5\u7684Python\u7248\u672c\u3002\u8fd9\u5c06\u663e\u793a\u5305\u62ecPython\u53ca\u5176\u7248\u672c\u53f7\u7684\u6240\u6709\u5305\u5217\u8868\uff0c\u60a8\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>python --version<\/code>\u547d\u4ee4\u76f4\u63a5\u67e5\u770b\u5f53\u524d\u6fc0\u6d3b\u73af\u5883\u4e2d\u7684Python\u7248\u672c\u3002<\/p>\n<p><strong>\u5728\u4e0d\u540c\u7684conda\u73af\u5883\u4e2d\u5982\u4f55\u5207\u6362\u4f7f\u7528\u7684Python\u7248\u672c\uff1f<\/strong><br \/>\u8981\u5728\u4e0d\u540c\u7684conda\u73af\u5883\u4e2d\u4f7f\u7528\u7279\u5b9a\u7248\u672c\u7684Python\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4<code>conda create -n myenv python=3.x<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u73af\u5883\u5e76\u6307\u5b9aPython\u7248\u672c\u3002\u6fc0\u6d3b\u8be5\u73af\u5883\u540e\uff0c\u6240\u6709\u8fd0\u884c\u7684Python\u4ee3\u7801\u90fd\u4f1a\u4f7f\u7528\u8be5\u73af\u5883\u4e2d\u5b89\u88c5\u7684Python\u7248\u672c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Jupyter Notebook\u4e2d\u4f7f\u7528conda\u73af\u5883\u4e2d\u7684Python\uff1f<\/strong><br \/>\u5728Jupyter Notebook\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5ipykernel\u6765\u4f7f\u7528conda\u73af\u5883\u4e2d\u7684Python\u3002\u9996\u5148\uff0c\u6fc0\u6d3b\u60a8\u7684conda\u73af\u5883\uff0c\u7136\u540e\u8fd0\u884c<code>python -m ipykernel install --user --name=myenv<\/code>\u547d\u4ee4\u3002\u63a5\u4e0b\u6765\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728Jupyter Notebook\u4e2d\u9009\u62e9\u8fd9\u4e2a\u73af\u5883\u4f5c\u4e3a\u5185\u6838\uff0c\u786e\u4fdd\u60a8\u4f7f\u7528\u7684\u662f\u6b63\u786e\u7684Python\u7248\u672c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8c03\u7528Conda\u7684Python\u6709\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528Conda\u3001\u901a\u8fc7Conda\u73af\u5883\u6fc0\u6d3b\u3001\u914d\u7f6e\u73af\u5883\u53d8 [&hellip;]","protected":false},"author":3,"featured_media":976751,"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\/976737"}],"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=976737"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/976737\/revisions"}],"predecessor-version":[{"id":976753,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/976737\/revisions\/976753"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/976751"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=976737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=976737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=976737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}