{"id":953770,"date":"2024-12-27T01:47:45","date_gmt":"2024-12-26T17:47:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/953770.html"},"modified":"2024-12-27T01:47:48","modified_gmt":"2024-12-26T17:47:48","slug":"python-%e5%a6%82%e4%bd%95%e5%bc%95%e5%85%a5%e4%be%9d%e8%b5%96%e5%8c%85","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/953770.html","title":{"rendered":"python \u5982\u4f55\u5f15\u5165\u4f9d\u8d56\u5305"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094140\/c7dfed41-a620-4374-9f06-f9170eafc940.webp\" alt=\"python \u5982\u4f55\u5f15\u5165\u4f9d\u8d56\u5305\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u5f15\u5165\u4f9d\u8d56\u5305\u7684\u65b9\u5f0f\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<strong>\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\u3001\u901a\u8fc7requirements.txt\u7ba1\u7406\u4f9d\u8d56\u3001\u5229\u7528\u865a\u62df\u73af\u5883\u7ba1\u7406\u5305<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528pip\u5de5\u5177\u76f4\u63a5\u5b89\u88c5\u4f9d\u8d56\u5305\uff0cpip\u662fPython\u7684\u5305\u7ba1\u7406\u5668\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u4ecePython Package Index\uff08PyPI\uff09\u4e0b\u8f7d\u5e76\u5b89\u88c5\u8f6f\u4ef6\u5305\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528pip\u4ee5\u53ca\u5176\u4ed6\u65b9\u6cd5\u6765\u7ba1\u7406Python\u9879\u76ee\u4e2d\u7684\u4f9d\u8d56\u5305\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Pip\u5b89\u88c5\u4f9d\u8d56\u5305<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u7279\u5b9a\u5305<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528pip\u5b89\u88c5\u7279\u5b9a\u7684Python\u5305\u662f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\u3002\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install package_name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u8981\u5b89\u88c5requests\u5e93\uff0c\u53ef\u4ee5\u8fd0\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6307\u5b9a\u7248\u672c\u5b89\u88c5<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5b89\u88c5\u7279\u5b9a\u7248\u672c\u7684\u5305\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u5305\u540d\u540e\u52a0\u4e0a\u7248\u672c\u53f7\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install package_name==version_number<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5b89\u88c5requests\u76842.25.1\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests==2.25.1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5347\u7ea7\u5305<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u5347\u7ea7\u5df2\u7ecf\u5b89\u88c5\u7684\u5305\u5230\u6700\u65b0\u7248\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install --upgrade package_name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u67e5\u770b\u5df2\u5b89\u88c5\u5305<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u67e5\u770b\u5f53\u524d\u73af\u5883\u4e0b\u5df2\u7ecf\u5b89\u88c5\u7684\u5305\u53ca\u5176\u7248\u672c\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip list<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5378\u8f7d\u5305<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5378\u8f7d\u67d0\u4e2a\u5305\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip uninstall package_name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u901a\u8fc7requirements.txt\u7ba1\u7406\u4f9d\u8d56<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u751f\u6210requirements.txt<\/strong><\/p>\n<\/p>\n<p><p>\u5f53\u4f60\u9700\u8981\u5c06\u9879\u76ee\u4e2d\u7684\u6240\u6709\u4f9d\u8d56\u8bb0\u5f55\u4e0b\u6765\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u7684freeze\u547d\u4ee4\u751f\u6210requirements.txt\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip freeze &gt; requirements.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5requirements.txt\u4e2d\u7684\u4f9d\u8d56<\/strong><\/p>\n<\/p>\n<p><p>\u5f53\u4f60\u9700\u8981\u5728\u65b0\u7684\u73af\u5883\u4e2d\u5b89\u88c5\u9879\u76ee\u6240\u9700\u7684\u6240\u6709\u4f9d\u8d56\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8bfb\u53d6requirements.txt\u6587\u4ef6\u5e76\u5b89\u88c5\u6240\u6709\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install -r requirements.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u8f91requirements.txt<\/strong><\/p>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u624b\u52a8\u7f16\u8f91requirements.txt\u6587\u4ef6\uff0c\u6dfb\u52a0\u6216\u4fee\u6539\u5305\u540d\u53ca\u7248\u672c\u53f7\uff0c\u4ee5\u6b64\u6765\u7ba1\u7406\u9879\u76ee\u4f9d\u8d56\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code>requests==2.25.1<\/p>\n<p>numpy&gt;=1.19.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u5229\u7528\u865a\u62df\u73af\u5883\u7ba1\u7406\u5305<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u865a\u62df\u73af\u5883<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528virtualenv\u6216venv\u6a21\u5757\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u865a\u62df\u73af\u5883\uff0c\u8fd9\u6837\u53ef\u4ee5\u5728\u9694\u79bb\u7684\u73af\u5883\u4e2d\u5b89\u88c5\u548c\u7ba1\u7406\u4f9d\u8d56\u5305\uff0c\u4e0d\u5f71\u54cd\u5168\u5c40Python\u73af\u5883\u3002\u521b\u5efa\u865a\u62df\u73af\u5883\u7684\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m venv myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/strong><\/p>\n<\/p>\n<p><p>\u521b\u5efa\u865a\u62df\u73af\u5883\u540e\uff0c\u9700\u8981\u6fc0\u6d3b\u5b83\u3002\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u6709\u4e0d\u540c\u7684\u6fc0\u6d3b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<ul>\n<li>Windows:\n<pre><code class=\"language-bash\">myenv\\Scripts\\activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>macOS\u548cLinux:\n<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\u5b89\u88c5\u5305<\/strong><\/p>\n<\/p>\n<p><p>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u540e\uff0c\u5b89\u88c5\u5305\u7684\u547d\u4ee4\u4e0e\u5168\u5c40\u73af\u5883\u76f8\u540c\uff0c\u4f46\u5b83\u4eec\u53ea\u4f1a\u5728\u5f53\u524d\u865a\u62df\u73af\u5883\u4e2d\u751f\u6548\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9000\u51fa\u865a\u62df\u73af\u5883<\/strong><\/p>\n<\/p>\n<p><p>\u5b8c\u6210\u5de5\u4f5c\u540e\uff0c\u4f60\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<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u4f7f\u7528Conda\u8fdb\u884c\u5305\u7ba1\u7406<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Conda<\/strong><\/p>\n<\/p>\n<p><p>Conda\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u5305\u7ba1\u7406\u7cfb\u7edf\u548c\u73af\u5883\u7ba1\u7406\u7cfb\u7edf\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b89\u88c5\u548c\u7ba1\u7406Python\u5305\u53ca\u5176\u4f9d\u8d56\u3002\u5728\u4f7f\u7528Conda\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5Anaconda\u6216Miniconda\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efaConda\u73af\u5883<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Conda\u521b\u5efa\u865a\u62df\u73af\u5883\u7684\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">conda create --name myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6fc0\u6d3bConda\u73af\u5883<\/strong><\/p>\n<\/p>\n<p><p>\u6fc0\u6d3bConda\u73af\u5883\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">conda activate myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5\u5305<\/strong><\/p>\n<\/p>\n<p><p>\u5728Conda\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">conda install package_name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5bfc\u51fa\u73af\u5883<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06Conda\u73af\u5883\u5bfc\u51fa\u4e3a\u4e00\u4e2a.yml\u6587\u4ef6\uff0c\u65b9\u4fbf\u5728\u5176\u4ed6\u5730\u65b9\u91cd\u5efa\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">conda env export &gt; environment.yml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u901a\u8fc7.yml\u6587\u4ef6\u521b\u5efa\u73af\u5883<\/strong><\/p>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u5728\u65b0\u73af\u5883\u4e2d\u5b89\u88c5\u6240\u6709\u4f9d\u8d56\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7.yml\u6587\u4ef6\u521b\u5efa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">conda env create -f environment.yml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5f15\u5165\u548c\u7ba1\u7406Python\u7684\u4f9d\u8d56\u5305\u662f\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u65e0\u8bba\u662f\u4f7f\u7528pip\u3001requirements.txt\u3001\u865a\u62df\u73af\u5883\uff0c\u8fd8\u662fConda\uff0c\u90fd\u662f\u4e3a\u4e86\u5728\u4e0d\u540c\u7684\u9879\u76ee\u4e2d\u4fdd\u6301\u4f9d\u8d56\u7684\u72ec\u7acb\u6027\u548c\u4e00\u81f4\u6027\u3002\u901a\u8fc7\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u9879\u76ee\u7684\u4f9d\u8d56\uff0c\u786e\u4fdd\u9879\u76ee\u5728\u4e0d\u540c\u73af\u5883\u4e0b\u7684\u53ef\u79fb\u690d\u6027\u548c\u7a33\u5b9a\u6027\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u9879\u76ee\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u88c5\u4f9d\u8d56\u5305\uff1f<\/strong><br \/>\u5728Python\u4e2d\u5b89\u88c5\u4f9d\u8d56\u5305\u901a\u5e38\u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177\u5982pip\u3002\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>pip install \u5305\u540d<\/code>\u6765\u5b89\u88c5\u6240\u9700\u7684\u4f9d\u8d56\u5305\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u9700\u8981\u5b89\u88c5NumPy\u5e93\uff0c\u53ef\u4ee5\u8f93\u5165<code>pip install numpy<\/code>\u3002\u786e\u4fdd\u5728\u5b89\u88c5\u4e4b\u524d\u5df2\u7ecf\u6b63\u786e\u914d\u7f6e\u4e86Python\u73af\u5883\u548cpip\u5de5\u5177\u3002<\/p>\n<p><strong>\u5982\u4f55\u7ba1\u7406Python\u9879\u76ee\u4e2d\u7684\u4f9d\u8d56\u5305\uff1f<\/strong><br \/>\u7ba1\u7406Python\u9879\u76ee\u4e2d\u7684\u4f9d\u8d56\u5305\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a<code>requirements.txt<\/code>\u6587\u4ef6\u6765\u5b9e\u73b0\u3002\u8be5\u6587\u4ef6\u5217\u51fa\u9879\u76ee\u6240\u9700\u7684\u6240\u6709\u4f9d\u8d56\u5305\u53ca\u5176\u7248\u672c\u3002\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4<code>pip freeze &gt; requirements.txt<\/code>\u5c06\u5f53\u524d\u73af\u5883\u4e2d\u7684\u6240\u6709\u5305\u53ca\u5176\u7248\u672c\u4fdd\u5b58\u5230\u8be5\u6587\u4ef6\u4e2d\uff0c\u4e4b\u540e\u901a\u8fc7<code>pip install -r requirements.txt<\/code>\u5feb\u901f\u5b89\u88c5\u8fd9\u4e9b\u4f9d\u8d56\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728Python\u4e2d\u4f7f\u7528\u865a\u62df\u73af\u5883\u6765\u7ba1\u7406\u4f9d\u8d56\u5305\uff1f<\/strong><br \/>\u4f7f\u7528\u865a\u62df\u73af\u5883\u662f\u7ba1\u7406Python\u4f9d\u8d56\u5305\u7684\u6700\u4f73\u5b9e\u8df5\u4e4b\u4e00\u3002\u53ef\u4ee5\u901a\u8fc7<code>venv<\/code>\u6a21\u5757\u521b\u5efa\u4e00\u4e2a\u72ec\u7acb\u7684\u73af\u5883\uff0c\u8fd9\u6837\u6bcf\u4e2a\u9879\u76ee\u90fd\u53ef\u4ee5\u6709\u81ea\u5df1\u7684\u4f9d\u8d56\u5305\uff0c\u4e0d\u4f1a\u4e92\u76f8\u5e72\u6270\u3002\u521b\u5efa\u865a\u62df\u73af\u5883\u7684\u547d\u4ee4\u4e3a<code>python -m venv myenv<\/code>\uff0c\u6fc0\u6d3b\u540e\u53ef\u4ee5\u5728\u5176\u4e2d\u81ea\u7531\u5b89\u88c5\u548c\u7ba1\u7406\u4f9d\u8d56\u5305\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5f15\u5165\u4f9d\u8d56\u5305\u7684\u65b9\u5f0f\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\u3001\u901a\u8fc7requirements.txt\u7ba1\u7406 [&hellip;]","protected":false},"author":3,"featured_media":953774,"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\/953770"}],"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=953770"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/953770\/revisions"}],"predecessor-version":[{"id":953777,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/953770\/revisions\/953777"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/953774"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=953770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=953770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=953770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}