{"id":1000916,"date":"2024-12-27T09:54:32","date_gmt":"2024-12-27T01:54:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1000916.html"},"modified":"2024-12-27T09:54:44","modified_gmt":"2024-12-27T01:54:44","slug":"%e5%a6%82%e4%bd%95%e5%88%87%e6%8d%a2python%e7%9a%84%e7%89%88%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1000916.html","title":{"rendered":"\u5982\u4f55\u5207\u6362python\u7684\u7248\u672c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075319\/8aee292b-ec10-4825-848f-8e4088039774.webp\" alt=\"\u5982\u4f55\u5207\u6362python\u7684\u7248\u672c\" \/><\/p>\n<p><p> \u5728Python\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c<strong>\u5207\u6362Python\u7248\u672c\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528pyenv\u3001virtualenv\u3001\u4ee5\u53caAnaconda<\/strong>\u3002\u8fd9\u4e9b\u5de5\u5177\u5404\u6709\u5176\u72ec\u7279\u7684\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<strong>pyenv<\/strong>\u662f\u4e00\u79cd\u6d41\u884c\u7684Python\u7248\u672c\u7ba1\u7406\u5de5\u5177\uff0c\u5b83\u5141\u8bb8\u5728\u591a\u4e2aPython\u7248\u672c\u4e4b\u95f4\u8f7b\u677e\u5207\u6362\uff1b<strong>virtualenv<\/strong>\u4e3b\u8981\u7528\u4e8e\u521b\u5efa\u72ec\u7acb\u7684Python\u73af\u5883\uff0c\u4ee5\u907f\u514d\u4e0d\u540c\u9879\u76ee\u4e4b\u95f4\u7684\u4f9d\u8d56\u51b2\u7a81\uff1b\u800c<strong>Anaconda<\/strong>\u5219\u662f\u4e00\u4e2a\u9002\u7528\u4e8e\u6570\u636e\u79d1\u5b66\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7684Python\u53d1\u884c\u7248\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u73af\u5883\u7ba1\u7406\u529f\u80fd\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e09\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528PYENV\u5207\u6362Python\u7248\u672c<\/h3>\n<\/p>\n<p><p>Pyenv \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5b89\u88c5\u548c\u7ba1\u7406\u591a\u4e2aPython\u7248\u672c\u3002\u5b83\u5141\u8bb8\u4f60\u5728\u4e0d\u540c\u7684\u9879\u76ee\u4e2d\u4f7f\u7528\u4e0d\u540c\u7684Python\u7248\u672c\uff0c\u800c\u4e0d\u5f71\u54cd\u7cfb\u7edf\u7684\u5168\u5c40Python\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Pyenv<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5pyenv\u3002\u5bf9\u4e8e\u5927\u591a\u6570Unix\u7cfb\u7edf\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">curl https:\/\/pyenv.run | bash<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u9700\u8981\u914d\u7f6eshell\u73af\u5883\u6765\u652f\u6301pyenv\u3002\u901a\u5e38\uff0c\u8fd9\u6d89\u53ca\u5230\u5728\u4f60\u7684shell\u914d\u7f6e\u6587\u4ef6\uff08\u5982<code>~\/.bashrc<\/code>\u6216<code>~\/.zshrc<\/code>\uff09\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u51e0\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export PATH=&quot;$HOME\/.pyenv\/bin:$PATH&quot;<\/p>\n<p>eval &quot;$(pyenv init --path)&quot;<\/p>\n<p>eval &quot;$(pyenv virtualenv-init -)&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u91cd\u65b0\u542f\u52a8shell\u6216\u8005\u6267\u884c<code>source ~\/.bashrc<\/code>\u6765\u5e94\u7528\u66f4\u6539\u3002<\/p>\n<\/p>\n<p><h4>2. \u5b89\u88c5\u548c\u5207\u6362Python\u7248\u672c<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u4e00\u4e2a\u65b0\u7684Python\u7248\u672c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyenv install 3.9.6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8bbe\u7f6e\u5168\u5c40Python\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyenv global 3.9.6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u53ea\u60f3\u5728\u67d0\u4e2a\u9879\u76ee\u4e2d\u4f7f\u7528\u7279\u5b9a\u7684Python\u7248\u672c\uff0c\u53ef\u4ee5\u5728\u8be5\u9879\u76ee\u7684\u76ee\u5f55\u4e0b\u8fd0\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyenv local 3.9.6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u5728\u5f53\u524d\u76ee\u5f55\u521b\u5efa\u4e00\u4e2a<code>.python-version<\/code>\u6587\u4ef6\uff0c\u6307\u5b9a\u8be5\u76ee\u5f55\u4e0b\u4f7f\u7528\u7684Python\u7248\u672c\u3002<\/p>\n<\/p>\n<p><h4>3. \u9a8c\u8bc1Python\u7248\u672c<\/h4>\n<\/p>\n<p><p>\u5207\u6362\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u9a8c\u8bc1\u5f53\u524d\u4f7f\u7528\u7684Python\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528VIRTUALENV\u521b\u5efa\u72ec\u7acb\u73af\u5883<\/h3>\n<\/p>\n<p><p>Virtualenv \u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u9694\u79bbPython\u73af\u5883\u7684\u5de5\u5177\u3002\u5b83\u5141\u8bb8\u6bcf\u4e2a\u9879\u76ee\u4f7f\u7528\u4e0d\u540c\u7684\u4f9d\u8d56\u548cPython\u7248\u672c\uff0c\u800c\u4e0d\u5f71\u54cd\u5176\u4ed6\u9879\u76ee\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Virtualenv<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5virtualenv\u3002\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install virtualenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u548c\u6fc0\u6d3bVirtualenv<\/h4>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">virtualenv venv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u521b\u5efa\u5b8c\u6210\u540e\uff0c\u6fc0\u6d3b\u8be5\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p>\u5728Unix\u6216MacOS\u4e0a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">source venv\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u5728Windows\u4e0a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">.\\venv\\Scripts\\activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u6fc0\u6d3b\u540e\uff0c\u547d\u4ee4\u884c\u63d0\u793a\u7b26\u4f1a\u663e\u793a\u865a\u62df\u73af\u5883\u7684\u540d\u79f0\uff0c\u8868\u660e\u4f60\u5df2\u8fdb\u5165\u8be5\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h4>3. \u5b89\u88c5\u7279\u5b9aPython\u7248\u672c<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u865a\u62df\u73af\u5883\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9aPython\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">virtualenv -p \/usr\/bin\/python3.7 venv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u4f7f\u7528Python 3.7\u7684\u865a\u62df\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h4>4. \u9000\u51faVirtualenv<\/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\u4f7f\u7528ANACONDA\u7ba1\u7406Python\u7248\u672c<\/h3>\n<\/p>\n<p><p>Anaconda \u662f\u4e00\u4e2a\u6d41\u884c\u7684\u6570\u636e\u79d1\u5b66\u5e73\u53f0\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5305\u548c\u73af\u5883\u7ba1\u7406\u529f\u80fd\u3002\u5b83\u7279\u522b\u9002\u5408\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u9886\u57df\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Anaconda<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4eceAnaconda\u5b98\u7f51\u4e0b\u8f7d\u5b89\u88c5\u5305\uff0c\u5e76\u6309\u7167\u8bf4\u660e\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h4>2. \u521b\u5efaConda\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Conda\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u548c\u7ba1\u7406\u73af\u5883\u3002\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u73af\u5883\u5e76\u6307\u5b9aPython\u7248\u672c\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\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><h4>3. \u6fc0\u6d3b\u548c\u5207\u6362\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u5b8c\u6210\u540e\uff0c\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6fc0\u6d3b\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">conda activate myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5728\u4e0d\u540c\u73af\u5883\u4e4b\u95f4\u5207\u6362\uff0c\u6216\u8005\u9000\u51fa\u5f53\u524d\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">conda deactivate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u7ba1\u7406\u73af\u5883<\/h4>\n<\/p>\n<p><p>Conda\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u547d\u4ee4\u6765\u7ba1\u7406\u73af\u5883\uff0c\u5982\u5217\u51fa\u6240\u6709\u73af\u5883\u3001\u5220\u9664\u73af\u5883\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">conda info --envs<\/p>\n<p>conda remove --name myenv --all<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5207\u6362Python\u7248\u672c\u662f\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u5e38\u89c1\u7684\u9700\u6c42\uff0c<strong>pyenv\u3001virtualenv\u548cAnaconda<\/strong>\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u89e3\u51b3\u65b9\u6848\u3002\u6839\u636e\u9879\u76ee\u9700\u6c42\u548c\u4e2a\u4eba\u504f\u597d\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<strong>pyenv\u9002\u5408\u9700\u8981\u9891\u7e41\u5207\u6362Python\u7248\u672c\u7684\u5f00\u53d1\u8005<\/strong>\uff0c<strong>virtualenv\u9002\u5408\u9694\u79bb\u9879\u76ee\u73af\u5883<\/strong>\uff0c\u800c<strong>Anaconda\u5219\u662f\u6570\u636e\u79d1\u5b66\u548c\u673a\u5668\u5b66\u4e60\u7684\u7406\u60f3\u9009\u62e9<\/strong>\u3002\u901a\u8fc7\u6b63\u786e\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u5f00\u53d1\u8005\u80fd\u591f\u5728\u4e0d\u540c\u7248\u672c\u7684Python\u4e4b\u95f4\u81ea\u7531\u5207\u6362\uff0c\u4ece\u800c\u66f4\u597d\u5730\u9002\u5e94\u5feb\u901f\u53d8\u5316\u7684\u9879\u76ee\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728\u540c\u4e00\u53f0\u7535\u8111\u4e0a\u5b89\u88c5\u591a\u4e2aPython\u7248\u672c\uff1f<\/strong><br \/>\u5728\u540c\u4e00\u53f0\u7535\u8111\u4e0a\u5b89\u88c5\u591a\u4e2aPython\u7248\u672c\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95eePython\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u4e0d\u540c\u7248\u672c\u7684\u5b89\u88c5\u5305\uff0c\u9009\u62e9\u9002\u5408\u60a8\u7684\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u5b89\u88c5\u3002\u5b89\u88c5\u65f6\uff0c\u786e\u4fdd\u9009\u62e9\u201c\u6dfb\u52a0\u5230\u73af\u5883\u53d8\u91cf\u201d\u7684\u9009\u9879\uff0c\u8fd9\u6837\u5728\u547d\u4ee4\u884c\u4e2d\u5c31\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u4e0d\u540c\u7248\u672c\u7684Python\u3002<\/p>\n<p><strong>\u5207\u6362Python\u7248\u672c\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u9879\u76ee\u4f9d\u8d56\u4e0d\u53d7\u5f71\u54cd\uff1f<\/strong><br \/>\u4f7f\u7528\u865a\u62df\u73af\u5883\u662f\u7ba1\u7406\u9879\u76ee\u4f9d\u8d56\u548c\u5207\u6362Python\u7248\u672c\u7684\u6709\u6548\u65b9\u6cd5\u3002\u53ef\u4ee5\u4f7f\u7528<code>venv<\/code>\u6216<code>virtualenv<\/code>\u5de5\u5177\u521b\u5efa\u72ec\u7acb\u7684\u865a\u62df\u73af\u5883\uff0c\u5728\u6bcf\u4e2a\u865a\u62df\u73af\u5883\u4e2d\u5b89\u88c5\u7279\u5b9a\u7248\u672c\u7684Python\u53ca\u5176\u4f9d\u8d56\u3002\u8fd9\u6837\uff0c\u65e0\u8bba\u5207\u6362\u5230\u54ea\u4e2a\u7248\u672c\uff0c\u90fd\u4e0d\u4f1a\u5f71\u54cd\u5176\u4ed6\u9879\u76ee\u3002<\/p>\n<p><strong>\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528pyenv\u6765\u7ba1\u7406Python\u7248\u672c\uff1f<\/strong><br \/>pyenv\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5728Linux\u7cfb\u7edf\u4e2d\u8f7b\u677e\u7ba1\u7406\u591a\u4e2aPython\u7248\u672c\u3002\u9996\u5148\uff0c\u901a\u8fc7Git\u514b\u9686pyenv\u7684\u4ed3\u5e93\u5e76\u6dfb\u52a0\u5230\u73af\u5883\u53d8\u91cf\u4e2d\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pyenv install<\/code>\u547d\u4ee4\u5b89\u88c5\u6240\u9700\u7248\u672c\uff0c\u4f7f\u7528<code>pyenv global<\/code>\u6216<code>pyenv local<\/code>\u547d\u4ee4\u8bbe\u7f6e\u5168\u5c40\u6216\u5c40\u90e8\u7684Python\u7248\u672c\u3002\u8fd9\u6837\uff0c\u65e0\u8bba\u5728\u4f55\u5904\u8fd0\u884cPython\u547d\u4ee4\uff0c\u90fd\u4f1a\u4f7f\u7528\u6307\u5b9a\u7684\u7248\u672c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u5207\u6362Python\u7248\u672c\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528pyenv\u3001virtualenv\u3001\u4ee5\u53caAnac [&hellip;]","protected":false},"author":3,"featured_media":1000937,"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\/1000916"}],"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=1000916"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1000916\/revisions"}],"predecessor-version":[{"id":1000939,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1000916\/revisions\/1000939"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1000937"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1000916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1000916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1000916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}