{"id":1022378,"date":"2024-12-27T13:33:54","date_gmt":"2024-12-27T05:33:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1022378.html"},"modified":"2024-12-27T13:33:56","modified_gmt":"2024-12-27T05:33:56","slug":"python%e5%a6%82%e4%bd%95%e4%b8%8b%e8%bd%bd%e5%ba%93%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1022378.html","title":{"rendered":"python\u5982\u4f55\u4e0b\u8f7d\u5e93\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25164759\/5bbaa05a-1385-4b89-8a61-2e89ef472f0d.webp\" alt=\"python\u5982\u4f55\u4e0b\u8f7d\u5e93\u51fd\u6570\" \/><\/p>\n<p><p> \u8981\u5728Python\u4e2d\u4e0b\u8f7d\u5e93\u51fd\u6570\uff0c\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a<strong>\u4f7f\u7528Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u3001\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u5b89\u88c5\u547d\u4ee4\u3001\u786e\u4fddPython\u73af\u5883\u6b63\u786e\u914d\u7f6e<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7pip\u6765\u5b89\u88c5\u5e93\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u662fPython\u7684\u5b98\u65b9\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u80fd\u591f\u4ecePython Package Index (PyPI) \u4e0b\u8f7d\u5e76\u5b89\u88c5\u5305\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86pip\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>pip --version<\/code>\u6765\u68c0\u67e5\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u4f60\u9700\u8981\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u7684\u4e0d\u540c\uff0c\u4f7f\u7528Python\u5b89\u88c5\u5305\u6216\u624b\u52a8\u4e0b\u8f7d\u8fdb\u884c\u914d\u7f6e\u3002\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528<code>pip install \u5e93\u540d<\/code>\u547d\u4ee4\u5b89\u88c5\u6240\u9700\u7684\u5e93\u3002\u4f8b\u5982\uff0c\u5b89\u88c5NumPy\u53ef\u4ee5\u4f7f\u7528<code>pip install numpy<\/code>\u3002\u8981\u786e\u4fdd\u987a\u5229\u5b89\u88c5\uff0c\u4f60\u9700\u8981\u6709\u7a33\u5b9a\u7684\u7f51\u7edc\u8fde\u63a5\uff0c\u5e76\u4e14\u53ef\u80fd\u9700\u8981\u7ba1\u7406\u5458\u6743\u9650\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001PIP\u7684\u57fa\u672c\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>Pip\u662fPython\u7684\u5305\u7ba1\u7406\u5668\uff0c\u7528\u4e8e\u5b89\u88c5\u548c\u7ba1\u7406Python\u8f6f\u4ef6\u5305\u3002\u4e86\u89e3\u5982\u4f55\u6709\u6548\u4f7f\u7528pip\u662f\u4e0b\u8f7d\u5e93\u51fd\u6570\u7684\u57fa\u7840\u3002<\/p>\n<\/p>\n<p><h4>1.1\u3001\u5b89\u88c5\u548c\u66f4\u65b0pip<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u786e\u4fddpip\u5df2\u7ecf\u5b89\u88c5\u5728\u4f60\u7684\u7cfb\u7edf\u4e2d\u3002\u5927\u591a\u6570\u73b0\u4ee3\u7684Python\u5b89\u88c5\u5305\u5df2\u7ecf\u81ea\u5e26pip\u3002\u5982\u679c\u6ca1\u6709\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4e0b\u8f7dget-pip.py\u811a\u672c\u5e76\u8fd0\u884c<code>python get-pip.py<\/code>\u6765\u624b\u52a8\u5b89\u88c5\u3002\u4e3a\u4e86\u786e\u4fddpip\u662f\u6700\u65b0\u7248\u672c\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4<code>pip install --upgrade pip<\/code>\u8fdb\u884c\u66f4\u65b0\u3002\u4fdd\u6301pip\u4e3a\u6700\u65b0\u7248\u672c\u6709\u52a9\u4e8e\u907f\u514d\u8bb8\u591a\u5df2\u77e5\u7684bug\u548c\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>1.2\u3001\u57fa\u672c\u7684pip\u547d\u4ee4<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5Python\u5e93\u7684\u57fa\u672c\u547d\u4ee4\u662f<code>pip install \u5e93\u540d<\/code>\u3002\u4f8b\u5982\uff0c\u8981\u5b89\u88c5requests\u5e93\uff0c\u4f60\u53ef\u4ee5\u8fd0\u884c<code>pip install requests<\/code>\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>pip uninstall \u5e93\u540d<\/code>\u6765\u5378\u8f7d\u4e00\u4e2a\u5e93\uff0c\u6216\u8005\u4f7f\u7528<code>pip list<\/code>\u6765\u67e5\u770b\u5f53\u524d\u73af\u5883\u4e2d\u5b89\u88c5\u7684\u6240\u6709\u5e93\u3002\u6b64\u5916\uff0c<code>pip show \u5e93\u540d<\/code>\u53ef\u4ee5\u663e\u793a\u4e00\u4e2a\u5e93\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5305\u62ec\u7248\u672c\u53f7\u548c\u5b89\u88c5\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001VIRTUAL ENVIRONMENT\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u865a\u62df\u73af\u5883\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u9879\u76ee\u521b\u5efa\u72ec\u7acb\u7684Python\u73af\u5883\uff0c\u786e\u4fdd\u4e0d\u540c\u9879\u76ee\u4e4b\u95f4\u7684\u5e93\u4f9d\u8d56\u4e0d\u4f1a\u76f8\u4e92\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><h4>2.1\u3001\u521b\u5efa\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u865a\u62df\u73af\u5883\u53ef\u4ee5\u901a\u8fc7<code>venv<\/code>\u6a21\u5757\u6765\u521b\u5efa\u3002\u5728\u4f60\u7684\u9879\u76ee\u76ee\u5f55\u4e2d\uff0c\u4f60\u53ef\u4ee5\u8fd0\u884c<code>python -m venv myenv<\/code>\u547d\u4ee4\u6765\u521b\u5efa\u4e00\u4e2a\u540d\u4e3amyenv\u7684\u865a\u62df\u73af\u5883\u3002\u8fd9\u4e2a\u73af\u5883\u5c06\u5305\u542b\u72ec\u7acb\u7684Python\u53ef\u6267\u884c\u6587\u4ef6\u548cpip\uff0c\u4ece\u800c\u4e0d\u4f1a\u5f71\u54cd\u7cfb\u7edf\u7684\u5168\u5c40Python\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h4>2.2\u3001\u6fc0\u6d3b\u548c\u4f7f\u7528\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u865a\u62df\u73af\u5883\u540e\uff0c\u4f60\u9700\u8981\u6fc0\u6d3b\u5b83\u624d\u80fd\u5728\u8be5\u73af\u5883\u4e2d\u8fd0\u884cPython\u548cpip\u547d\u4ee4\u3002\u5728Windows\u4e0a\uff0c\u4f60\u53ef\u4ee5\u8fd0\u884c<code>myenv\\Scripts\\activate<\/code>\u6765\u6fc0\u6d3b\u73af\u5883\u3002\u5728macOS\u548cLinux\u4e0a\uff0c\u4f60\u53ef\u4ee5\u8fd0\u884c<code>source myenv\/bin\/activate<\/code>\u3002\u6fc0\u6d3b\u540e\uff0c\u547d\u4ee4\u884c\u7684\u63d0\u793a\u7b26\u901a\u5e38\u4f1a\u663e\u793a\u865a\u62df\u73af\u5883\u7684\u540d\u79f0\uff0c\u8868\u793a\u4f60\u5df2\u7ecf\u8fdb\u5165\u8be5\u73af\u5883\u3002\u5728\u6fc0\u6d3b\u7684\u73af\u5883\u4e2d\uff0c\u6240\u6709\u7684<code>pip install<\/code>\u547d\u4ee4\u90fd\u4f1a\u5b89\u88c5\u5728\u865a\u62df\u73af\u5883\u4e2d\uff0c\u800c\u4e0d\u662f\u7cfb\u7edf\u7684\u5168\u5c40Python\u73af\u5883\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u89e3\u51b3\u5e38\u89c1\u7684\u5b89\u88c5\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528pip\u5b89\u88c5Python\u5e93\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u95ee\u9898\uff0c\u5305\u62ec\u7f51\u7edc\u95ee\u9898\u3001\u6743\u9650\u95ee\u9898\u4ee5\u53ca\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>3.1\u3001\u7f51\u7edc\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u5728\u5b89\u88c5Python\u5e93\u65f6\uff0c\u7a33\u5b9a\u7684\u7f51\u7edc\u8fde\u63a5\u662f\u5fc5\u8981\u7684\u3002\u5982\u679c\u4f60\u7684\u7f51\u7edc\u73af\u5883\u4e0d\u7a33\u5b9a\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e0b\u8f7d\u5931\u8d25\u3002\u53ef\u4ee5\u901a\u8fc7\u5c1d\u8bd5\u66f4\u6362\u7f51\u7edc\uff0c\u6216\u8005\u4f7f\u7528<code>pip install --timeout=100 \u5e93\u540d<\/code>\u589e\u52a0\u8d85\u65f6\u8bbe\u7f6e\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u56fd\u5185\u955c\u50cf\u6e90\u53ef\u4ee5\u63d0\u9ad8\u4e0b\u8f7d\u901f\u5ea6\uff0c\u6bd4\u5982\u4f7f\u7528\u6e05\u534e\u5927\u5b66\u7684\u955c\u50cf\uff1a<code>pip install -i https:\/\/pypi.tuna.tsinghua.edu.cn\/simple \u5e93\u540d<\/code>\u3002<\/p>\n<\/p>\n<p><h4>3.2\u3001\u6743\u9650\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u64cd\u4f5c\u7cfb\u7edf\u4e0a\uff0c\u4f7f\u7528pip\u5b89\u88c5\u5e93\u53ef\u80fd\u9700\u8981\u7ba1\u7406\u5458\u6743\u9650\u3002\u5982\u679c\u9047\u5230\u6743\u9650\u62d2\u7edd\u7684\u9519\u8bef\uff0c\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u5728\u547d\u4ee4\u524d\u52a0\u4e0a<code>sudo<\/code>\uff08\u5728Linux\u548cmacOS\u4e0a\uff09\uff0c\u6216\u8005\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c\u547d\u4ee4\u63d0\u793a\u7b26\uff08\u5728Windows\u4e0a\uff09\u3002\u6b64\u5916\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>--user<\/code>\u9009\u9879\u5728\u7528\u6237\u76ee\u5f55\u4e0b\u5b89\u88c5\u5e93\uff1a<code>pip install --user \u5e93\u540d<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ba1\u7406\u548c\u66f4\u65b0\u5df2\u5b89\u88c5\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u6709\u6548\u7ba1\u7406\u548c\u66f4\u65b0\u5df2\u5b89\u88c5\u7684\u5e93\u53ef\u4ee5\u786e\u4fdd\u4f60\u7684\u9879\u76ee\u4f9d\u8d56\u4fdd\u6301\u5728\u6700\u65b0\u4e14\u7a33\u5b9a\u7684\u7248\u672c\u3002<\/p>\n<\/p>\n<p><h4>4.1\u3001\u67e5\u770b\u5df2\u5b89\u88c5\u7684\u5e93<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>pip list<\/code>\u547d\u4ee4\u53ef\u4ee5\u67e5\u770b\u5f53\u524d\u73af\u5883\u4e2d\u6240\u6709\u5df2\u5b89\u88c5\u7684Python\u5e93\u53ca\u5176\u7248\u672c\u3002\u8fd9\u6709\u52a9\u4e8e\u4f60\u4e86\u89e3\u9879\u76ee\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u4ee5\u53ca\u662f\u5426\u9700\u8981\u66f4\u65b0\u67d0\u4e9b\u5e93\u3002<\/p>\n<\/p>\n<p><h4>4.2\u3001\u66f4\u65b0\u5e93<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u4fdd\u6301\u5df2\u5b89\u88c5\u5e93\u7684\u6700\u65b0\u7248\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip install --upgrade \u5e93\u540d<\/code>\u547d\u4ee4\u6765\u66f4\u65b0\u5355\u4e2a\u5e93\u3002\u5982\u679c\u60f3\u8981\u66f4\u65b0\u6240\u6709\u5df2\u5b89\u88c5\u7684\u5e93\uff0c\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u811a\u672c\uff1a<code>pip list --outdated --format=freeze | grep -v &#39;^\\\\-e&#39; | cut -d = -f 1 | xargs -n1 pip install -U<\/code>\u3002\u8fd9\u5c06\u4f1a\u68c0\u67e5\u6240\u6709\u8fc7\u65f6\u7684\u5e93\u5e76\u8fdb\u884c\u66f4\u65b0\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528requirements.txt\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u56e2\u961f\u9879\u76ee\u4e2d\uff0c\u4f7f\u7528requirements.txt\u6587\u4ef6\u53ef\u4ee5\u786e\u4fdd\u6240\u6709\u6210\u5458\u7684\u5f00\u53d1\u73af\u5883\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><h4>5.1\u3001\u751f\u6210requirements.txt<\/h4>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u5f00\u53d1\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>pip freeze &gt; requirements.txt<\/code>\u547d\u4ee4\u751f\u6210\u4e00\u4e2arequirements.txt\u6587\u4ef6\u3002\u8fd9\u5c06\u8bb0\u5f55\u5f53\u524d\u73af\u5883\u4e2d\u6240\u6709\u5e93\u53ca\u5176\u7248\u672c\uff0c\u4f7f\u5f97\u9879\u76ee\u7684\u4f9d\u8d56\u5173\u7cfb\u660e\u786e\u3002<\/p>\n<\/p>\n<p><h4>5.2\u3001\u4ecerequirements.txt\u5b89\u88c5\u5e93<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u65b0\u73af\u5883\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<code>pip install -r requirements.txt<\/code>\u547d\u4ee4\u6765\u5b89\u88c5requirements.txt\u6587\u4ef6\u4e2d\u5217\u51fa\u7684\u6240\u6709\u5e93\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u786e\u4fdd\u4e0d\u540c\u5f00\u53d1\u73af\u5883\u7684\u4e00\u81f4\u6027\uff0c\u5c24\u5176\u662f\u5728\u56e2\u961f\u534f\u4f5c\u6216\u90e8\u7f72\u9879\u76ee\u65f6\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528conda\u5b89\u88c5Python\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86pip\uff0cAnaconda\u7684\u5305\u7ba1\u7406\u5de5\u5177conda\u4e5f\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u9009\u62e9\uff0c\u7279\u522b\u662f\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u9886\u57df\u3002<\/p>\n<\/p>\n<p><h4>6.1\u3001\u5b89\u88c5\u548c\u7ba1\u7406\u5e93<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528conda\u5b89\u88c5\u5e93\u7684\u547d\u4ee4\u662f<code>conda install \u5e93\u540d<\/code>\uff0c\u7c7b\u4f3c\u4e8epip\u3002Conda\u4e0d\u4ec5\u53ef\u4ee5\u5b89\u88c5Python\u5e93\uff0c\u8fd8\u53ef\u4ee5\u5b89\u88c5\u975ePython\u7684\u5305\u548c\u5de5\u5177\uff0c\u6bd4\u5982R\u3001Java\u7b49\u3002\u8fd9\u4f7f\u5f97\u5b83\u5728\u591a\u8bed\u8a00\u9879\u76ee\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>6.2\u3001\u521b\u5efa\u548c\u7ba1\u7406\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u4e0epip\u7ed3\u5408venv\u4e0d\u540c\uff0cconda\u6709\u81ea\u5df1\u5185\u5efa\u7684\u73af\u5883\u7ba1\u7406\u529f\u80fd\u3002\u4f7f\u7528<code>conda create --name \u73af\u5883\u540d<\/code>\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u73af\u5883\uff0c\u4f7f\u7528<code>conda activate \u73af\u5883\u540d<\/code>\u6fc0\u6d3b\u8be5\u73af\u5883\u3002Conda\u8fd8\u63d0\u4f9b\u4e86\u73af\u5883\u5bfc\u51fa\u548c\u5bfc\u5165\u529f\u80fd\uff0c\u4f7f\u7528<code>conda env export &gt; environment.yml<\/code>\u53ef\u4ee5\u5bfc\u51fa\u5f53\u524d\u73af\u5883\u7684\u914d\u7f6e\uff0c\u800c\u4f7f\u7528<code>conda env create -f environment.yml<\/code>\u53ef\u4ee5\u6839\u636e\u5bfc\u51fa\u7684\u6587\u4ef6\u521b\u5efa\u65b0\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u7406\u89e3PyPI\u548c\u7b2c\u4e09\u65b9\u5305\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>Python Package Index (PyPI) \u662fPython\u7684\u5b98\u65b9\u5305\u5b58\u50a8\u5e93\uff0c\u4e86\u89e3\u5176\u5de5\u4f5c\u539f\u7406\u6709\u52a9\u4e8e\u66f4\u597d\u5730\u7ba1\u7406Python\u5e93\u3002<\/p>\n<\/p>\n<p><h4>7.1\u3001PyPI\u7684\u57fa\u672c\u6982\u5ff5<\/h4>\n<\/p>\n<p><p>PyPI\u662f\u4e00\u4e2a\u9762\u5411Python\u793e\u533a\u7684\u516c\u5171\u8d44\u6e90\u5e93\uff0c\u6258\u7ba1\u4e86\u6570\u4ee5\u4e07\u8ba1\u7684Python\u5305\u3002\u6bcf\u4e2a\u5305\u90fd\u5305\u542b\u4e86\u4ee3\u7801\u3001\u6587\u6863\u548c\u5176\u4ed6\u6587\u4ef6\uff0c\u4f9b\u5f00\u53d1\u8005\u4e0b\u8f7d\u548c\u4f7f\u7528\u3002\u901a\u8fc7pip\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8f7b\u677e\u5730\u4ecePyPI\u5b89\u88c5\u548c\u66f4\u65b0\u8fd9\u4e9b\u5305\u3002<\/p>\n<\/p>\n<p><h4>7.2\u3001\u7b2c\u4e09\u65b9\u5305\u7ba1\u7406<\/h4>\n<\/p>\n<p><p>\u9664\u4e86PyPI\uff0cPython\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5305\u7ba1\u7406\u5de5\u5177\u548c\u5e73\u53f0\uff0c\u6bd4\u5982GitHub\u3001Bitbucket\u7b49\u3002\u8fd9\u4e9b\u5e73\u53f0\u901a\u5e38\u7528\u4e8e\u6258\u7ba1\u5f00\u6e90\u9879\u76ee\u7684\u6e90\u4ee3\u7801\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u901a\u8fc7git clone\u547d\u4ee4\u4e0b\u8f7d\u4ee3\u7801\u5e76\u624b\u52a8\u5b89\u88c5\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u7528\u4e8e\u9700\u8981\u4fee\u6539\u6216\u5b9a\u5236\u7684\u9879\u76ee\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528Docker\u7ba1\u7406Python\u73af\u5883<\/h3>\n<\/p>\n<p><p>Docker\u662f\u4e00\u4e2a\u5bb9\u5668\u5316\u5e73\u53f0\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u521b\u5efa\u72ec\u7acb\u7684\u3001\u53ef\u79fb\u690d\u7684\u5e94\u7528\u7a0b\u5e8f\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h4>8.1\u3001\u521b\u5efaPython Docker\u955c\u50cf<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7Dockerfile\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2aPython\u5e94\u7528\u7684\u8fd0\u884c\u73af\u5883\u3002\u4e00\u4e2a\u7b80\u5355\u7684Dockerfile\u53ef\u4ee5\u5305\u62ec\u57fa\u7840\u7684Python\u955c\u50cf\u3001\u9700\u8981\u5b89\u88c5\u7684\u4f9d\u8d56\u5305\u4ee5\u53ca\u8fd0\u884c\u7684\u547d\u4ee4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-dockerfile\">FROM python:3.8-slim<\/p>\n<p>WORKDIR \/app<\/p>\n<p>COPY requirements.txt .<\/p>\n<p>RUN pip install --no-cache-dir -r requirements.txt<\/p>\n<p>COPY . .<\/p>\n<p>CMD [&quot;python&quot;, &quot;app.py&quot;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>docker build -t my-python-app .<\/code>\u547d\u4ee4\uff0c\u4f60\u53ef\u4ee5\u6784\u5efa\u4e00\u4e2aDocker\u955c\u50cf\u3002<\/p>\n<\/p>\n<p><h4>8.2\u3001\u8fd0\u884cPython Docker\u5bb9\u5668<\/h4>\n<\/p>\n<p><p>\u4e00\u65e6\u521b\u5efa\u4e86Docker\u955c\u50cf\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>docker run -d my-python-app<\/code>\u547d\u4ee4\u542f\u52a8\u4e00\u4e2a\u5bb9\u5668\u3002Docker\u5bb9\u5668\u53ef\u4ee5\u5728\u4efb\u4f55\u652f\u6301Docker\u7684\u73af\u5883\u4e2d\u8fd0\u884c\uff0c\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u73af\u5883\u7684\u4e00\u81f4\u6027\u548c\u53ef\u79fb\u690d\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528Jupyter Notebook\u7ba1\u7406\u5e93<\/h3>\n<\/p>\n<p><p>Jupyter Notebook\u662f\u4e00\u79cd\u4ea4\u4e92\u5f0f\u7684Python\u5f00\u53d1\u73af\u5883\uff0c\u5e7f\u6cdb\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u3002<\/p>\n<\/p>\n<p><h4>9.1\u3001\u5728Notebook\u4e2d\u5b89\u88c5\u5e93<\/h4>\n<\/p>\n<p><p>\u5728Jupyter Notebook\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u9b54\u6cd5\u547d\u4ee4<code>!pip install \u5e93\u540d<\/code>\u6765\u5b89\u88c5Python\u5e93\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u7528\u4e8e\u9700\u8981\u5728Notebook\u4e2d\u5feb\u901f\u5b89\u88c5\u548c\u6d4b\u8bd5\u5e93\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>9.2\u3001\u7ba1\u7406Notebook\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u4fdd\u6301Notebook\u73af\u5883\u7684\u6574\u6d01\u548c\u4e00\u81f4\u6027\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>conda<\/code>\u6216<code>venv<\/code>\u6765\u521b\u5efa\u72ec\u7acb\u7684\u73af\u5883\uff0c\u5e76\u5728\u542f\u52a8Notebook\u65f6\u6307\u5b9a\u73af\u5883\u3002\u901a\u8fc7<code>jupyter notebook --notebook-dir=\u73af\u5883\u8def\u5f84<\/code>\u547d\u4ee4\uff0c\u4f60\u53ef\u4ee5\u6307\u5b9aNotebook\u4f7f\u7528\u7684Python\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3\u4e0e\u5efa\u8bae<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u4e0b\u8f7d\u548c\u7ba1\u7406\u5e93\u51fd\u6570\u662f\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002<strong>\u719f\u7ec3\u4f7f\u7528pip\u548cconda\u3001\u521b\u5efa\u548c\u7ba1\u7406\u865a\u62df\u73af\u5883\u3001\u89e3\u51b3\u5b89\u88c5\u95ee\u9898\u3001\u4f7f\u7528requirements.txt\u6587\u4ef6\u7ba1\u7406\u4f9d\u8d56<\/strong>\u662f\u63d0\u5347\u5f00\u53d1\u6548\u7387\u548c\u786e\u4fdd\u9879\u76ee\u7a33\u5b9a\u6027\u7684\u5173\u952e\u3002\u5bf9\u4e8e\u5927\u578b\u9879\u76ee\u6216\u56e2\u961f\u534f\u4f5c\uff0c\u4f7f\u7528Docker\u548cJupyter Notebook\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u5f00\u53d1\u6d41\u7a0b\u3002\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\uff0c\u6839\u636e\u9879\u76ee\u7684\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u65b9\u6cd5\uff0c\u4ee5\u8fbe\u5230\u6700\u4f73\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u67e5\u627e\u9700\u8981\u4e0b\u8f7d\u7684\u5e93\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Python Package Index (PyPI) \u6765\u67e5\u627e\u53ef\u7528\u7684\u5e93\u51fd\u6570\u3002\u60a8\u53ef\u4ee5\u8bbf\u95ee <a href=\"https:\/\/pypi.org\/%EF%BC%8C%E5%9C%A8%E6%90%9C%E7%B4%A2%E6%A1%86%E4%B8%AD%E8%BE%93%E5%85%A5%E6%82%A8%E9%9C%80%E8%A6%81%E7%9A%84%E5%8A%9F%E8%83%BD%E6%88%96%E5%BA%93%E7%9A%84%E5%90%8D%E7%A7%B0%EF%BC%8C%E6%9F%A5%E6%89%BE%E7%9B%B8%E5%85%B3%E5%BA%93%E7%9A%84%E8%AF%A6%E7%BB%86%E4%BF%A1%E6%81%AF%E5%92%8C%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97%E3%80%82\">https:\/\/pypi.org\/\uff0c\u5728\u641c\u7d22\u6846\u4e2d\u8f93\u5165\u60a8\u9700\u8981\u7684\u529f\u80fd\u6216\u5e93\u7684\u540d\u79f0\uff0c\u67e5\u627e\u76f8\u5173\u5e93\u7684\u8be6\u7ec6\u4fe1\u606f\u548c\u4f7f\u7528\u6307\u5357\u3002<\/a><\/p>\n<p><strong>\u4e0b\u8f7dPython\u5e93\u51fd\u6570\u9700\u8981\u5177\u5907\u4ec0\u4e48\u6761\u4ef6\uff1f<\/strong><br \/>\u4e0b\u8f7dPython\u5e93\u51fd\u6570\u901a\u5e38\u9700\u8981\u5b89\u88c5Python\u548c\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u5982pip\u3002\u786e\u4fdd\u60a8\u7684\u8ba1\u7b97\u673a\u4e0a\u5df2\u6b63\u786e\u5b89\u88c5Python\uff0c\u5e76\u4e14pip\u4e5f\u968f\u4e4b\u5b89\u88c5\u3002\u60a8\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>python --version<\/code>\u548c<code>pip --version<\/code>\u6765\u9a8c\u8bc1\u5b89\u88c5\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528pip\u547d\u4ee4\u4e0b\u8f7d\u7279\u5b9a\u7684Python\u5e93\uff1f<\/strong><br \/>\u4f7f\u7528pip\u547d\u4ee4\u4e0b\u8f7dPython\u5e93\u975e\u5e38\u7b80\u5355\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>pip install \u5e93\u540d<\/code>\uff0c\u4f8b\u5982\uff1a<code>pip install requests<\/code>\uff0c\u5373\u53ef\u4e0b\u8f7d\u5e76\u5b89\u88c5requests\u5e93\u3002\u5b89\u88c5\u8fc7\u7a0b\u4e2d\uff0cpip\u4f1a\u81ea\u52a8\u5904\u7406\u4f9d\u8d56\u9879\uff0c\u786e\u4fdd\u5e93\u7684\u6b63\u5e38\u4f7f\u7528\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd0\u884c\u547d\u4ee4\u65f6\u53ef\u80fd\u9700\u8981\u7ba1\u7406\u5458\u6743\u9650\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u60a8\u7684\u64cd\u4f5c\u7cfb\u7edf\u8bbe\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u4e0b\u8f7d\u5e93\u51fd\u6570\uff0c\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a\u4f7f\u7528Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u3001\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u5b89\u88c5\u547d\u4ee4\u3001\u786e\u4fddP [&hellip;]","protected":false},"author":3,"featured_media":1022389,"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\/1022378"}],"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=1022378"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1022378\/revisions"}],"predecessor-version":[{"id":1022392,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1022378\/revisions\/1022392"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1022389"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1022378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1022378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1022378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}