{"id":998035,"date":"2024-12-27T09:29:22","date_gmt":"2024-12-27T01:29:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/998035.html"},"modified":"2024-12-27T09:29:26","modified_gmt":"2024-12-27T01:29:26","slug":"python%e5%a6%82%e4%bd%95%e5%8d%b8%e8%bd%bd%e6%89%80%e4%bb%a5%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/998035.html","title":{"rendered":"python\u5982\u4f55\u5378\u8f7d\u6240\u4ee5\u6a21\u5757"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073618\/46eaf810-c388-4697-91eb-dfb187e194c3.webp\" alt=\"python\u5982\u4f55\u5378\u8f7d\u6240\u4ee5\u6a21\u5757\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5378\u8f7d\u6240\u6709\u6a21\u5757\u7684\u5e38\u89c1\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5305\u7ba1\u7406\u5668\uff08\u5982pip\uff09\u9010\u4e2a\u5378\u8f7d\u3001\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u865a\u62df\u73af\u5883\u3001\u4f7f\u7528\u81ea\u52a8\u5316\u811a\u672c\u3002<\/strong>\u63a8\u8350\u4f7f\u7528\u865a\u62df\u73af\u5883\u6765\u7ba1\u7406Python\u9879\u76ee\u4e2d\u7684\u4f9d\u8d56\u5305\uff0c\u8fd9\u662f\u56e0\u4e3a\u5b83\u53ef\u4ee5\u907f\u514d\u5168\u5c40\u73af\u5883\u7684\u6df7\u4e71\uff0c\u5e76\u4e14\u66f4\u5bb9\u6613\u7ba1\u7406\u9879\u76ee\u6240\u9700\u7684\u7279\u5b9a\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u4f7f\u7528\u865a\u62df\u73af\u5883\u662f\u7ba1\u7406Python\u6a21\u5757\u7684\u6700\u4f73\u5b9e\u8df5\u4e4b\u4e00\u3002\u5728\u865a\u62df\u73af\u5883\u4e2d\uff0c\u6240\u6709\u7684\u4f9d\u8d56\u5305\u90fd\u662f\u9694\u79bb\u7684\uff0c\u5f7c\u6b64\u4e4b\u95f4\u4e0d\u4f1a\u4e92\u76f8\u5e72\u6270\u3002\u8fd9\u610f\u5473\u7740\u5728\u4e00\u4e2a\u9879\u76ee\u4e2d\u6240\u505a\u7684\u66f4\u6539\u4e0d\u4f1a\u5f71\u54cd\u5230\u5176\u4ed6\u9879\u76ee\u3002\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u865a\u62df\u73af\u5883\uff0c\u53ef\u4ee5\u4f7f\u7528<code>venv<\/code>\u6a21\u5757\uff0c\u901a\u8fc7\u547d\u4ee4<code>python -m venv myenv<\/code>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u73af\u5883\u3002\u6fc0\u6d3b\u8fd9\u4e2a\u73af\u5883\u540e\uff0c\u60a8\u53ef\u4ee5\u968f\u5fc3\u6240\u6b32\u5730\u5b89\u88c5\u6216\u5378\u8f7d\u6a21\u5757\uff0c\u800c\u65e0\u9700\u62c5\u5fc3\u5f71\u54cd\u5230\u5168\u5c40\u7684Python\u73af\u5883\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\u5373\u4f7f\u9700\u8981\u91cd\u7f6e\u6240\u6709\u6a21\u5757\uff0c\u53ea\u9700\u5220\u9664\u5e76\u91cd\u65b0\u521b\u5efa\u865a\u62df\u73af\u5883\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5173\u4e8e\u5982\u4f55\u5378\u8f7d\u6240\u6709Python\u6a21\u5757\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff1a<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528PIP\u9010\u4e2a\u5378\u8f7d<\/strong><\/h2>\n<p><p>PIP\u662fPython\u7684\u5305\u7ba1\u7406\u5668\uff0c\u5e38\u7528\u4e8e\u5b89\u88c5\u548c\u7ba1\u7406Python\u5e93\u3002\u867d\u7136\u6ca1\u6709\u76f4\u63a5\u7684\u547d\u4ee4\u53ef\u4ee5\u4e00\u6b21\u6027\u5378\u8f7d\u6240\u6709\u6a21\u5757\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u65b9\u6cd5\u5b9e\u73b0\u6279\u91cf\u5378\u8f7d\u3002<\/p>\n<\/p>\n<p><h3>1.1 \u5217\u51fa\u6240\u6709\u5df2\u5b89\u88c5\u7684\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5217\u51fa\u6240\u6709\u5df2\u5b89\u88c5\u7684\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip freeze &gt; requirements.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4f1a\u5c06\u6240\u6709\u5df2\u5b89\u88c5\u6a21\u5757\u53ca\u5176\u7248\u672c\u8bb0\u5f55\u5230\u4e00\u4e2a<code>requirements.txt<\/code>\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>1.2 \u6279\u91cf\u5378\u8f7d<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6279\u91cf\u5378\u8f7d\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip uninstall -r requirements.txt -y<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8bfb\u53d6<code>requirements.txt<\/code>\u6587\u4ef6\u5e76\u5378\u8f7d\u5176\u4e2d\u5217\u51fa\u7684\u6240\u6709\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p><strong>\u6ce8\u610f\uff1a<\/strong> \u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4f1a\u5378\u8f7dPIP\u81ea\u8eab\u6216Python\u6807\u51c6\u5e93\u4e2d\u7684\u6a21\u5757\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u521b\u5efa\u65b0\u7684\u865a\u62df\u73af\u5883<\/strong><\/h2>\n<p><p>\u4f7f\u7528\u865a\u62df\u73af\u5883\u662f\u7ba1\u7406Python\u9879\u76ee\u4f9d\u8d56\u7684\u63a8\u8350\u65b9\u6cd5\u3002\u901a\u8fc7\u521b\u5efa\u65b0\u7684\u865a\u62df\u73af\u5883\uff0c\u53ef\u4ee5\u62e5\u6709\u4e00\u4e2a\u5e72\u51c0\u7684\u73af\u5883\uff0c\u907f\u514d\u590d\u6742\u7684\u4f9d\u8d56\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><h3>2.1 \u521b\u5efa\u865a\u62df\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\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\">python -m venv myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\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<p><h3>2.2 \u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u7684\u65b9\u6cd5\u56e0\u64cd\u4f5c\u7cfb\u7edf\u800c\u5f02\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p>\u5728Windows\u4e0a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">myenv\\Scripts\\activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u5728macOS\u548cLinux\u4e0a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">source myenv\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u6fc0\u6d3b\u540e\uff0c\u60a8\u53ef\u4ee5\u770b\u5230\u547d\u4ee4\u884c\u63d0\u793a\u7b26\u4e2d\u51fa\u73b0\u4e86\u73af\u5883\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><h3>2.3 \u5728\u865a\u62df\u73af\u5883\u4e2d\u5b89\u88c5\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5728\u6fc0\u6d3b\u7684\u865a\u62df\u73af\u5883\u4e2d\u5b89\u88c5\u6216\u5378\u8f7d\u6a21\u5757\uff0c\u4e0d\u4f1a\u5f71\u54cd\u5168\u5c40\u7684Python\u73af\u5883\u3002\u8fd9\u4f7f\u5f97\u4f9d\u8d56\u7ba1\u7406\u66f4\u52a0\u7b80\u5355\u548c\u5b89\u5168\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528\u81ea\u52a8\u5316\u811a\u672c<\/strong><\/h2>\n<p><p>\u5982\u679c\u9700\u8981\u591a\u6b21\u6267\u884c\u6279\u91cf\u5378\u8f7d\u64cd\u4f5c\uff0c\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u81ea\u52a8\u5316\u811a\u672c\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>3.1 \u811a\u672c\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Python\u811a\u672c\u793a\u4f8b\uff0c\u7528\u4e8e\u5378\u8f7d\u6240\u6709\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pkg_resources<\/p>\n<p>from subprocess import call<\/p>\n<p>packages = [dist.project_name for dist in pkg_resources.working_set]<\/p>\n<p>call(&quot;pip uninstall &quot; + &#39; &#39;.join(packages) + &quot; -y&quot;, shell=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.2 \u8fd0\u884c\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u5c06\u4e0a\u8ff0\u811a\u672c\u4fdd\u5b58\u4e3a<code>uninstall_all.py<\/code>\uff0c\u7136\u540e\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python uninstall_all.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u5378\u8f7d\u6240\u6709\u7b2c\u4e09\u65b9\u6a21\u5757\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u624b\u52a8\u5220\u9664\u76ee\u5f55<\/strong><\/h2>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u624b\u52a8\u5220\u9664\u6a21\u5757\u6240\u5728\u7684\u76ee\u5f55\u6765\u5b9e\u73b0\u5378\u8f7d\u3002<\/p>\n<\/p>\n<p><h3>4.1 \u67e5\u627e\u6a21\u5757\u76ee\u5f55<\/h3>\n<\/p>\n<p><p>\u901a\u5e38\uff0cPython\u6a21\u5757\u5b89\u88c5\u5728<code>site-packages<\/code>\u76ee\u5f55\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u67e5\u627e\u8be5\u76ee\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m site<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.2 \u5220\u9664\u76ee\u5f55<\/h3>\n<\/p>\n<p><p>\u627e\u5230<code>site-packages<\/code>\u76ee\u5f55\u540e\uff0c\u53ef\u4ee5\u624b\u52a8\u5220\u9664\u5176\u4e2d\u7684\u6587\u4ef6\u5939\u3002\u4e0d\u8fc7\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u63a8\u8350\uff0c\u56e0\u4e3a\u5bb9\u6613\u5bfc\u81f4\u73af\u5883\u4e0d\u7a33\u5b9a\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u5907\u4efd\u4e0e\u6062\u590d\u73af\u5883<\/strong><\/h2>\n<p><p>\u5728\u5378\u8f7d\u6a21\u5757\u524d\uff0c\u5efa\u8bae\u5907\u4efd\u5f53\u524d\u73af\u5883\uff0c\u4ee5\u4fbf\u5728\u9700\u8981\u65f6\u6062\u590d\u3002<\/p>\n<\/p>\n<p><h3>5.1 \u5bfc\u51fa\u5f53\u524d\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5bfc\u51fa\u5f53\u524d\u73af\u5883\u7684\u6a21\u5757\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip freeze &gt; requirements_backup.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.2 \u6062\u590d\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u8981\u6062\u590d\u73af\u5883\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u91cd\u65b0\u5b89\u88c5\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install -r requirements_backup.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u6839\u636e\u5907\u4efd\u6587\u4ef6\u91cd\u65b0\u5b89\u88c5\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u7075\u6d3b\u5730\u7ba1\u7406Python\u6a21\u5757\uff0c\u65e0\u8bba\u662f\u5378\u8f7d\u6240\u6709\u6a21\u5757\u8fd8\u662f\u521b\u5efa\u65b0\u7684\u865a\u62df\u73af\u5883\uff0c\u90fd\u80fd\u5e2e\u52a9\u60a8\u4fdd\u6301\u5f00\u53d1\u73af\u5883\u7684\u6574\u6d01\u548c\u9ad8\u6548\u3002\u9009\u62e9\u9002\u5408\u60a8\u9700\u6c42\u7684\u65b9\u6cd5\uff0c\u4ee5\u786e\u4fdd\u9879\u76ee\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u67e5\u770b\u5f53\u524d\u5b89\u88c5\u7684Python\u6a21\u5757\u5217\u8868\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>pip list<\/code>\u6765\u67e5\u770b\u5f53\u524d\u5b89\u88c5\u7684\u6240\u6709Python\u6a21\u5757\u3002\u8fd9\u5c06\u5217\u51fa\u6240\u6709\u5df2\u5b89\u88c5\u6a21\u5757\u53ca\u5176\u7248\u672c\u4fe1\u606f\uff0c\u5e2e\u52a9\u4f60\u786e\u8ba4\u9700\u8981\u5378\u8f7d\u7684\u6a21\u5757\u3002<\/p>\n<p><strong>\u5378\u8f7d\u5355\u4e2aPython\u6a21\u5757\u7684\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u8981\u5378\u8f7d\u5355\u4e2aPython\u6a21\u5757\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4<code>pip uninstall \u6a21\u5757\u540d<\/code>\u3002\u66ff\u6362\u201c\u6a21\u5757\u540d\u201d\u4e3a\u4f60\u60f3\u5378\u8f7d\u7684\u5177\u4f53\u6a21\u5757\u540d\u79f0\u3002\u6267\u884c\u540e\u7cfb\u7edf\u4f1a\u63d0\u793a\u4f60\u786e\u8ba4\u662f\u5426\u5378\u8f7d\uff0c\u8f93\u5165\u2018y\u2019\u8fdb\u884c\u786e\u8ba4\u5373\u53ef\u3002<\/p>\n<p><strong>\u662f\u5426\u6709\u65b9\u6cd5\u53ef\u4ee5\u4e00\u6b21\u6027\u5378\u8f7d\u6240\u6709Python\u6a21\u5757\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528<code>pip freeze<\/code>\u548c<code>xargs<\/code>\u547d\u4ee4\u6765\u4e00\u6b21\u6027\u5378\u8f7d\u6240\u6709\u6a21\u5757\u3002\u4f7f\u7528<code>pip freeze | xargs pip uninstall -y<\/code>\u547d\u4ee4\uff0c\u53ef\u4ee5\u5c06\u6240\u6709\u5df2\u5b89\u88c5\u7684\u6a21\u5757\u4f20\u9012\u7ed9\u5378\u8f7d\u547d\u4ee4\uff0c\u5b8c\u6210\u6279\u91cf\u5378\u8f7d\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u5378\u8f7d\u6240\u6709\u6a21\u5757\uff0c\u8bf7\u786e\u4fdd\u4f60\u4e0d\u518d\u9700\u8981\u5b83\u4eec\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5378\u8f7d\u6240\u6709\u6a21\u5757\u7684\u5e38\u89c1\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5305\u7ba1\u7406\u5668\uff08\u5982pip\uff09\u9010\u4e2a\u5378\u8f7d\u3001\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u865a\u62df\u73af\u5883\u3001\u4f7f\u7528\u81ea\u52a8\u5316 [&hellip;]","protected":false},"author":3,"featured_media":998048,"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\/998035"}],"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=998035"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998035\/revisions"}],"predecessor-version":[{"id":998051,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998035\/revisions\/998051"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/998048"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=998035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=998035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=998035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}