{"id":922905,"date":"2024-12-26T14:40:25","date_gmt":"2024-12-26T06:40:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/922905.html"},"modified":"2024-12-26T14:40:27","modified_gmt":"2024-12-26T06:40:27","slug":"%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8bpython%e5%ae%89%e8%a3%85%e5%8c%85","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/922905.html","title":{"rendered":"\u5982\u4f55\u67e5\u770bpython\u5b89\u88c5\u5305"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211155\/ef79e922-6cc0-4538-b768-13d62d3efefa.webp\" alt=\"\u5982\u4f55\u67e5\u770bpython\u5b89\u88c5\u5305\" \/><\/p>\n<p><p> <strong>\u8981\u67e5\u770b\u5df2\u5b89\u88c5\u7684Python\u5305\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u3001\u4f7f\u7528Python\u4ee3\u7801\u67e5\u770b\u3001\u6216\u5229\u7528\u865a\u62df\u73af\u5883\u7ba1\u7406\u5de5\u5177\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u547d\u4ee4\u884c\u5de5\u5177<code>pip<\/code>\u6765\u67e5\u770b\uff0c\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u67e5\u770bPython\u5b89\u88c5\u5305\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u67e5\u770b<\/p>\n<\/p>\n<p><p>Python\u7684\u5305\u7ba1\u7406\u5de5\u5177<code>pip<\/code>\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u67e5\u770b\u5df2\u5b89\u88c5\u7684\u5305\u3002\u901a\u8fc7\u547d\u4ee4\u884c\u8f93\u5165<code>pip list<\/code>\u547d\u4ee4\uff0c\u53ef\u4ee5\u5217\u51fa\u5f53\u524d\u73af\u5883\u4e2d\u6240\u6709\u5df2\u5b89\u88c5\u7684\u5305\u53ca\u5176\u7248\u672c\u53f7\u3002\u8fd9\u662f\u6700\u76f4\u63a5\u3001\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u7840\u547d\u4ee4<code>pip list<\/code><\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>pip list<\/code>\u547d\u4ee4\u53ef\u4ee5\u5217\u51fa\u6240\u6709\u5df2\u5b89\u88c5\u7684Python\u5305\u53ca\u5176\u5bf9\u5e94\u7684\u7248\u672c\u3002\u8fd9\u4e2a\u547d\u4ee4\u901a\u5e38\u7528\u4e8e\u5feb\u901f\u4e86\u89e3\u5f53\u524d\u73af\u5883\u4e0b\u7684\u5305\u4f9d\u8d56\u60c5\u51b5\u3002\u6267\u884c\u8be5\u547d\u4ee4\u540e\uff0c\u7cfb\u7edf\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff0c\u663e\u793a\u6bcf\u4e2a\u5305\u7684\u540d\u79f0\u548c\u7248\u672c\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip list<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code>Package    Version<\/p>\n<p>---------- -------<\/p>\n<p>numpy      1.19.5<\/p>\n<p>pandas     1.2.3<\/p>\n<p>requests   2.25.1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u901a\u8fc7<code>pip freeze<\/code>\u751f\u6210\u4f9d\u8d56\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p><code>pip freeze<\/code>\u547d\u4ee4\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5df2\u5b89\u88c5\u5305\u7684\u6587\u4ef6\uff0c\u901a\u5e38\u547d\u540d\u4e3a<code>requirements.txt<\/code>\u3002\u8fd9\u5728\u9700\u8981\u5c06\u5f53\u524d\u73af\u5883\u7684\u5305\u4f9d\u8d56\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u73af\u5883\u65f6\u975e\u5e38\u6709\u7528\u3002<\/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><code>requirements.txt<\/code>\u6587\u4ef6\u4e2d\u5c06\u5305\u542b\u7c7b\u4f3c\u4e8e<code>pip list<\/code>\u7684\u8f93\u51fa\uff0c\u4f46\u683c\u5f0f\u5316\u4e3a\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u5b89\u88c5\u7684\u683c\u5f0f\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\u53ef\u4ee5\u8f7b\u677e\u5c06\u73af\u5883\u79fb\u690d\u5230\u5176\u4ed6\u673a\u5668\u4e0a\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u641c\u7d22\u7279\u5b9a\u5305<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u53ea\u60f3\u67e5\u627e\u67d0\u4e2a\u7279\u5b9a\u5305\u662f\u5426\u5df2\u5b89\u88c5\uff0c\u53ef\u4ee5\u7ed3\u5408<code>grep<\/code>\u547d\u4ee4\uff08\u9002\u7528\u4e8eUnix-like\u7cfb\u7edf\uff09\u6765\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip list | grep &lt;package_name&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u68c0\u67e5<code>numpy<\/code>\u662f\u5426\u5df2\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip list | grep numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528Python\u4ee3\u7801\u67e5\u770b<\/p>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u5728Python\u811a\u672c\u4e2d\u68c0\u67e5\u5df2\u5b89\u88c5\u7684\u5305\u3002\u53ef\u4ee5\u4f7f\u7528<code>pkg_resources<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528<code>pkg_resources<\/code>\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p><code>pkg_resources<\/code>\u662f<code>setuptools<\/code>\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u5bf9Python\u5305\u5206\u5e03\u7684\u8bbf\u95ee\u3002\u53ef\u4ee5\u901a\u8fc7\u5b83\u6765\u5217\u51fa\u6240\u6709\u5df2\u5b89\u88c5\u7684\u5305\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pkg_resources<\/p>\n<p>installed_packages = pkg_resources.working_set<\/p>\n<p>installed_packages_list = sorted([&quot;%s==%s&quot; % (i.key, i.version) for i in installed_packages])<\/p>\n<p>for package in installed_packages_list:<\/p>\n<p>    print(package)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa\u4e0e<code>pip list<\/code>\u7c7b\u4f3c\u7684\u7ed3\u679c\u3002\u5b83\u6709\u52a9\u4e8e\u5728Python\u7a0b\u5e8f\u4e2d\u52a8\u6001\u83b7\u53d6\u5b89\u88c5\u5305\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528<code>importlib.metadata<\/code>\u6a21\u5757\uff08Python 3.8+\uff09<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8ePython 3.8\u53ca\u4ee5\u4e0a\u7248\u672c\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>importlib.metadata<\/code>\u6a21\u5757\u6765\u83b7\u53d6\u5df2\u5b89\u88c5\u7684\u5305\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import importlib.metadata<\/p>\n<p>installed_packages = importlib.metadata.distributions()<\/p>\n<p>for package in installed_packages:<\/p>\n<p>    print(f&quot;{package.metadata[&#39;Name&#39;]}=={package.version}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7<code>importlib.metadata<\/code>\u6a21\u5757\u8bbf\u95ee\u5305\u5143\u6570\u636e\uff0c\u63d0\u4f9b\u4e86\u5bf9\u5305\u4fe1\u606f\u7684\u66f4\u597d\u652f\u6301\u548c\u6269\u5c55\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u5229\u7528\u865a\u62df\u73af\u5883\u7ba1\u7406\u5de5\u5177<\/p>\n<\/p>\n<p><p>\u865a\u62df\u73af\u5883\u662fPython\u9879\u76ee\u5f00\u53d1\u4e2d\u5e38\u7528\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u9694\u79bb\u9879\u76ee\u7684\u4f9d\u8d56\u5305\u3002\u901a\u8fc7\u5728\u865a\u62df\u73af\u5883\u4e2d\u4f7f\u7528<code>pip<\/code>\u547d\u4ee4\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u548c\u67e5\u770b\u9879\u76ee\u7684\u4f9d\u8d56\u5305\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u548c\u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>venv<\/code>\u6a21\u5757\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>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\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>\u5728Unix\u6216MacOS\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<\/li>\n<li>\n<p><strong>\u5728\u865a\u62df\u73af\u5883\u4e2d\u67e5\u770b\u5df2\u5b89\u88c5\u7684\u5305<\/strong><\/p>\n<\/p>\n<p><p>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u540e\uff0c\u4f7f\u7528<code>pip list<\/code>\u547d\u4ee4\u67e5\u770b\u8be5\u73af\u5883\u4e2d\u7684\u5305\u3002\u7531\u4e8e\u865a\u62df\u73af\u5883\u521d\u59cb\u72b6\u6001\u4e0b\u6ca1\u6709\u5b89\u88c5\u989d\u5916\u7684\u5305\uff0c\u56e0\u6b64\u53ef\u4ee5\u770b\u5230\u7684\u5305\u901a\u5e38\u53ea\u6709<code>pip<\/code>\u3001<code>setuptools<\/code>\u7b49\u57fa\u7840\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5728\u865a\u62df\u73af\u5883\u4e2d\u5b89\u88c5\u6240\u9700\u7684\u5305\uff0c\u53ef\u4ee5\u9694\u79bb\u4e0d\u540c\u9879\u76ee\u7684\u4f9d\u8d56\uff0c\u907f\u514d\u5305\u7248\u672c\u51b2\u7a81\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5bfc\u51fa\u548c\u5bfc\u5165\u865a\u62df\u73af\u5883\u5305\u4f9d\u8d56<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u7ed3\u675f\u6216\u9879\u76ee\u90e8\u7f72\u65f6\uff0c\u53ef\u4ee5\u5c06\u865a\u62df\u73af\u5883\u4e2d\u7684\u5305\u4f9d\u8d56\u5bfc\u51fa\u5230\u4e00\u4e2a\u6587\u4ef6\u4e2d\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>\u5728\u76ee\u6807\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u4f9d\u8d56\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install -r requirements.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u786e\u4fdd\u4e86\u4e0d\u540c\u73af\u5883\u4e0b\u5305\u4f9d\u8d56\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u67e5\u770bPython\u5b89\u88c5\u5305\u7684\u4fe1\u606f\uff0c\u5e76\u6839\u636e\u9700\u8981\u7ba1\u7406\u548c\u7ef4\u62a4\u9879\u76ee\u7684\u4f9d\u8d56\u3002\u65e0\u8bba\u662f\u901a\u8fc7\u547d\u4ee4\u884c\u5de5\u5177\u3001Python\u4ee3\u7801\u8fd8\u662f\u865a\u62df\u73af\u5883\u7ba1\u7406\u5de5\u5177\uff0c\u8fd9\u4e9b\u6280\u672f\u90fd\u80fd\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u7ec4\u7ec7\u548c\u7ba1\u7406Python\u9879\u76ee\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u786e\u8ba4\u6211\u7684Python\u5b89\u88c5\u5305\u662f\u5426\u6210\u529f\u5b89\u88c5\uff1f<\/strong><br \/>\u8981\u786e\u8ba4Python\u5b89\u88c5\u5305\u662f\u5426\u6210\u529f\u5b89\u88c5\uff0c\u53ef\u4ee5\u6253\u5f00\u547d\u4ee4\u884c\u5de5\u5177\uff08Windows\u4e0a\u662fCMD\uff0cmacOS\u6216Linux\u4e0a\u662fTerminal\uff09\uff0c\u8f93\u5165\u547d\u4ee4<code>python --version<\/code>\u6216<code>python3 --version<\/code>\u3002\u5982\u679c\u7cfb\u7edf\u8fd4\u56de\u4e86Python\u7684\u7248\u672c\u53f7\uff0c\u5219\u8bf4\u660e\u5b89\u88c5\u6210\u529f\u3002\u53e6\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip list<\/code>\u547d\u4ee4\u67e5\u770b\u5f53\u524d\u5b89\u88c5\u7684\u6240\u6709Python\u5305\uff0c\u4ee5\u9a8c\u8bc1\u6240\u9700\u7684\u5305\u662f\u5426\u5728\u5217\u8868\u4e2d\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u67e5\u770b\u5df2\u5b89\u88c5\u7684\u7b2c\u4e09\u65b9\u5e93\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\u5de5\u5177\u67e5\u770b\u5df2\u5b89\u88c5\u7684\u7b2c\u4e09\u65b9\u5e93\u3002\u6253\u5f00\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u8f93\u5165<code>pip list<\/code>\u6216<code>pip freeze<\/code>\uff0c\u5c06\u5217\u51fa\u6240\u6709\u5df2\u5b89\u88c5\u7684\u5e93\u53ca\u5176\u7248\u672c\u3002<code>pip freeze<\/code>\u547d\u4ee4\u7684\u8f93\u51fa\u683c\u5f0f\u9002\u5408\u4e8e\u751f\u6210<code>requirements.txt<\/code>\u6587\u4ef6\uff0c\u800c<code>pip list<\/code>\u5219\u63d0\u4f9b\u4e86\u66f4\u6613\u8bfb\u7684\u683c\u5f0f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5378\u8f7d\u4e0d\u518d\u9700\u8981\u7684Python\u5b89\u88c5\u5305\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5e0c\u671b\u5378\u8f7d\u4e0d\u518d\u9700\u8981\u7684Python\u5b89\u88c5\u5305\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip uninstall \u5305\u540d<\/code>\u547d\u4ee4\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u6b64\u547d\u4ee4\uff0c\u66ff\u6362\u201c\u5305\u540d\u201d\u4e3a\u60a8\u8981\u5378\u8f7d\u7684\u5177\u4f53\u5305\u7684\u540d\u79f0\u3002\u7cfb\u7edf\u4f1a\u63d0\u793a\u60a8\u786e\u8ba4\u5378\u8f7d\uff0c\u6309\u4e0b\u201cy\u201d\u952e\u786e\u8ba4\u5373\u53ef\u3002\u4e3a\u4e86\u786e\u4fdd\u73af\u5883\u7684\u6574\u6d01\uff0c\u5b9a\u671f\u6e05\u7406\u4e0d\u518d\u4f7f\u7528\u7684\u5305\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u67e5\u770b\u5df2\u5b89\u88c5\u7684Python\u5305\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u3001\u4f7f\u7528Python\u4ee3\u7801\u67e5\u770b\u3001\u6216\u5229\u7528\u865a\u62df\u73af\u5883\u7ba1\u7406\u5de5\u5177\u3002\u5176\u4e2d\uff0c\u6700\u5e38 [&hellip;]","protected":false},"author":3,"featured_media":922911,"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\/922905"}],"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=922905"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/922905\/revisions"}],"predecessor-version":[{"id":922912,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/922905\/revisions\/922912"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/922911"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=922905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=922905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=922905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}