{"id":1094747,"date":"2025-01-08T14:44:08","date_gmt":"2025-01-08T06:44:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1094747.html"},"modified":"2025-01-08T14:44:10","modified_gmt":"2025-01-08T06:44:10","slug":"%e5%a6%82%e4%bd%95%e5%ae%89%e8%a3%85%e9%85%8d%e7%bd%aepython%e7%8e%af%e5%a2%83%e5%8f%98%e9%87%8f-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1094747.html","title":{"rendered":"\u5982\u4f55\u5b89\u88c5\u914d\u7f6epython\u73af\u5883\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210715\/6e5b94d9-1385-4bcb-9afd-893e42f7bf2b.webp\" alt=\"\u5982\u4f55\u5b89\u88c5\u914d\u7f6epython\u73af\u5883\u53d8\u91cf\" \/><\/p>\n<p><p> \u5982\u4f55\u5b89\u88c5\u914d\u7f6epython\u73af\u5883\u53d8\u91cf<\/p>\n<\/p>\n<p><p><strong>\u5b89\u88c5Python\u3001\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u3001\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f<\/strong>\u662f\u5b89\u88c5\u914d\u7f6ePython\u73af\u5883\u53d8\u91cf\u7684\u4e09\u4e2a\u5173\u952e\u6b65\u9aa4\u3002\u9996\u5148\u4e0b\u8f7d\u5e76\u5b89\u88c5Python\uff0c\u5efa\u8bae\u4ecePython\u7684\u5b98\u65b9\u7ad9\u70b9\u4e0b\u8f7d\u6700\u65b0\u7684\u7a33\u5b9a\u7248\u672c\u3002\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u7684\u5173\u952e\u6b65\u9aa4\u662f\u52fe\u9009\u201cAdd Python to PATH\u201d\uff0c\u8fd9\u5c06\u81ea\u52a8\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u3002\u63a5\u4e0b\u6765\uff0c\u624b\u52a8\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u4ee5\u786e\u4fdd\u6240\u6709\u547d\u4ee4\u884c\u5de5\u5177\u90fd\u80fd\u6b63\u786e\u8bc6\u522bPython\u3002\u6700\u540e\uff0c\u901a\u8fc7\u547d\u4ee4\u884c\u5de5\u5177\u9a8c\u8bc1Python\u5b89\u88c5\u548c\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u662f\u5426\u6210\u529f\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5Python<\/h3>\n<\/p>\n<p><h4>1. \u4e0b\u8f7dPython\u5b89\u88c5\u5305<\/h4>\n<\/p>\n<p><p>\u4ecePython\u7684\u5b98\u65b9\u7f51\u7ad9\uff08<a href=\"https:\/\/www.python.org\/%EF%BC%89%E4%B8%8B%E8%BD%BD%E6%9C%80%E6%96%B0%E7%89%88%E6%9C%AC%E7%9A%84Python%E3%80%82%E6%A0%B9%E6%8D%AE%E4%BD%A0%E7%9A%84%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E9%80%89%E6%8B%A9%E5%90%88%E9%80%82%E7%9A%84%E5%AE%89%E8%A3%85%E5%8C%85%E3%80%82\">https:\/\/www.python.org\/\uff09\u4e0b\u8f7d\u6700\u65b0\u7248\u672c\u7684Python\u3002\u6839\u636e\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u9009\u62e9\u5408\u9002\u7684\u5b89\u88c5\u5305\u3002<\/a><\/p>\n<\/p>\n<p><h4>2. \u5b89\u88c5Python<\/h4>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0b\u8f7d\u7684\u5b89\u88c5\u5305\uff0c\u9009\u62e9\u201cCustomize installation\u201d\u8fdb\u884c\u81ea\u5b9a\u4e49\u5b89\u88c5\u3002\u786e\u4fdd\u5728\u5b89\u88c5\u9009\u9879\u4e2d\u52fe\u9009\u201cAdd Python to PATH\u201d\uff0c\u8fd9\u5c06\u81ea\u52a8\u914d\u7f6e\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u3002\u7ee7\u7eed\u5b89\u88c5\uff0c\u76f4\u5230\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><h4>3. \u9a8c\u8bc1\u5b89\u88c5<\/h4>\n<\/p>\n<p><p>\u6253\u5f00\u547d\u4ee4\u884c\u5de5\u5177\uff08Windows\u7528\u6237\u53ef\u4ee5\u4f7f\u7528cmd\u6216PowerShell\uff0cMac\u548cLinux\u7528\u6237\u53ef\u4ee5\u4f7f\u7528\u7ec8\u7aef\uff09\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u9a8c\u8bc1Python\u662f\u5426\u5b89\u88c5\u6210\u529f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u5b89\u88c5\u6210\u529f\uff0c\u4f60\u5c06\u770b\u5230Python\u7684\u7248\u672c\u53f7\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u914d\u7f6e\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><h4>1. \u624b\u52a8\u914d\u7f6e\u73af\u5883\u53d8\u91cf\uff08Windows\uff09<\/h4>\n<\/p>\n<p><p>\u5c3d\u7ba1\u52fe\u9009\u4e86\u201cAdd Python to PATH\u201d\uff0c\u4f46\u6709\u65f6\u4ecd\u9700\u624b\u52a8\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u3002\u53f3\u952e\u70b9\u51fb\u201c\u6b64\u7535\u8111\u201d\u6216\u201c\u6211\u7684\u7535\u8111\u201d\uff0c\u9009\u62e9\u201c\u5c5e\u6027\u201d\u3002\u7136\u540e\u70b9\u51fb\u201c\u9ad8\u7ea7\u7cfb\u7edf\u8bbe\u7f6e\u201d\uff0c\u5728\u201c\u7cfb\u7edf\u5c5e\u6027\u201d\u5bf9\u8bdd\u6846\u4e2d\u9009\u62e9\u201c\u73af\u5883\u53d8\u91cf\u201d\u3002<\/p>\n<\/p>\n<p><p>\u5728\u201c\u7cfb\u7edf\u53d8\u91cf\u201d\u90e8\u5206\uff0c\u627e\u5230\u5e76\u9009\u62e9\u201cPath\u201d\u53d8\u91cf\uff0c\u70b9\u51fb\u201c\u7f16\u8f91\u201d\u3002\u5728\u7f16\u8f91\u7a97\u53e3\u4e2d\uff0c\u70b9\u51fb\u201c\u65b0\u5efa\u201d\u5e76\u6dfb\u52a0Python\u7684\u5b89\u88c5\u8def\u5f84\uff08\u901a\u5e38\u4e3aC:\\Python39\u6216C:\\Users\\\u4f60\u7684\u7528\u6237\u540d\\AppData\\Local\\Programs\\Python\\Python39\uff09\u3002<\/p>\n<\/p>\n<p><h4>2. \u624b\u52a8\u914d\u7f6e\u73af\u5883\u53d8\u91cf\uff08Mac\u548cLinux\uff09<\/h4>\n<\/p>\n<p><p>\u5728\u7ec8\u7aef\u4e2d\u6253\u5f00\u4f60\u7684shell\u914d\u7f6e\u6587\u4ef6\uff08\u901a\u5e38\u4e3a<del>\/.bashrc\u3001<\/del>\/.zshrc\u6216~\/.bash_profile\uff09\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">export PATH=&quot;\/Library\/Frameworks\/Python.framework\/Versions\/3.9\/bin:${PATH}&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4fdd\u5b58\u6587\u4ef6\u5e76\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u4f7f\u66f4\u6539\u751f\u6548\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">source ~\/.bashrc<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u9a8c\u8bc1\u914d\u7f6e\u662f\u5426\u6210\u529f<\/h3>\n<\/p>\n<p><h4>1. \u68c0\u67e5Python\u7248\u672c<\/h4>\n<\/p>\n<p><p>\u518d\u6b21\u6253\u5f00\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u770b\u5230Python\u7684\u7248\u672c\u53f7\uff0c\u8bf4\u660ePython\u5df2\u6210\u529f\u5b89\u88c5\u5e76\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h4>2. \u68c0\u67e5pip\u7248\u672c<\/h4>\n<\/p>\n<p><p>pip\u662fPython\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u5b89\u88c5Python\u65f6\u9ed8\u8ba4\u4f1a\u5b89\u88c5pip\u3002\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u9a8c\u8bc1pip\u662f\u5426\u5b89\u88c5\u6210\u529f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u770b\u5230pip\u7684\u7248\u672c\u53f7\uff0c\u8bf4\u660epip\u5df2\u6210\u529f\u5b89\u88c5\u5e76\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b89\u88c5\u548c\u914d\u7f6e\u5e38\u7528Python\u5e93<\/h3>\n<\/p>\n<p><h4>1. \u5b89\u88c5pipenv<\/h4>\n<\/p>\n<p><p>pipenv\u662f\u4e00\u4e2a\u7528\u4e8e\u7ba1\u7406Python\u9879\u76ee\u7684\u5de5\u5177\uff0c\u7ed3\u5408\u4e86pip\u548cvirtualenv\u7684\u529f\u80fd\u3002\u5b89\u88c5pipenv\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install pipenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u865a\u62df\u73af\u5883\u7528\u4e8e\u9694\u79bb\u9879\u76ee\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u786e\u4fdd\u6bcf\u4e2a\u9879\u76ee\u7684\u4f9d\u8d56\u5305\u4e92\u4e0d\u5e72\u6270\u3002\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pipenv install<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2aPipfile\uff0c\u7528\u4e8e\u7ba1\u7406\u9879\u76ee\u7684\u4f9d\u8d56\u5173\u7cfb\u3002<\/p>\n<\/p>\n<p><h4>3. \u5b89\u88c5\u9879\u76ee\u4f9d\u8d56<\/h4>\n<\/p>\n<p><p>\u5728Pipfile\u4e2d\u6307\u5b9a\u9879\u76ee\u6240\u9700\u7684\u4f9d\u8d56\u5305\uff0c\u7136\u540e\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u4f9d\u8d56\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pipenv install<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u914d\u7f6eIDE\u548c\u6587\u672c\u7f16\u8f91\u5668<\/h3>\n<\/p>\n<p><h4>1. \u914d\u7f6eVS Code<\/h4>\n<\/p>\n<p><p>Visual Studio Code\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u4ee3\u7801\u7f16\u8f91\u5668\uff0c\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u3002\u5b89\u88c5Python\u63d2\u4ef6\u540e\uff0cVS Code\u4f1a\u81ea\u52a8\u8bc6\u522bPython\u73af\u5883\u3002\u6253\u5f00VS Code\uff0c\u6309\u4e0bCtrl+Shift+P\uff08\u6216Cmd+Shift+P\uff09\uff0c\u8f93\u5165\u201cPython: Select Interpreter\u201d\uff0c\u7136\u540e\u9009\u62e9\u4f60\u7684Python\u89e3\u91ca\u5668\u3002<\/p>\n<\/p>\n<p><h4>2. \u914d\u7f6ePyCharm<\/h4>\n<\/p>\n<p><p>PyCharm\u662f\u4e00\u4e2a\u4e13\u4e1a\u7684Python\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u3002\u5b89\u88c5\u5e76\u6253\u5f00PyCharm\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u9879\u76ee\u3002\u5728\u9879\u76ee\u8bbe\u7f6e\u4e2d\u9009\u62e9\u4f60\u7684Python\u89e3\u91ca\u5668\uff0cPyCharm\u4f1a\u81ea\u52a8\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u5e76\u5b89\u88c5\u6240\u9700\u7684\u4f9d\u8d56\u5305\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5e38\u89c1\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>1. Python\u547d\u4ee4\u672a\u627e\u5230<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u5728\u547d\u4ee4\u884c\u5de5\u5177\u4e2d\u8fd0\u884c<code>python --version<\/code>\u65f6\u663e\u793a\u201ccommand not found\u201d\u6216\u7c7b\u4f3c\u9519\u8bef\uff0c\u53ef\u80fd\u662f\u73af\u5883\u53d8\u91cf\u914d\u7f6e\u4e0d\u6b63\u786e\u3002\u68c0\u67e5\u5e76\u786e\u4fddPython\u7684\u5b89\u88c5\u8def\u5f84\u5df2\u6dfb\u52a0\u5230\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2. pip\u547d\u4ee4\u672a\u627e\u5230<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u5728\u547d\u4ee4\u884c\u5de5\u5177\u4e2d\u8fd0\u884c<code>pip --version<\/code>\u65f6\u663e\u793a\u201ccommand not found\u201d\u6216\u7c7b\u4f3c\u9519\u8bef\uff0c\u53ef\u80fd\u662fpip\u672a\u6b63\u786e\u5b89\u88c5\u6216\u73af\u5883\u53d8\u91cf\u914d\u7f6e\u4e0d\u6b63\u786e\u3002\u5c1d\u8bd5\u91cd\u65b0\u5b89\u88c5pip\uff0c\u6216\u624b\u52a8\u6dfb\u52a0pip\u7684\u8def\u5f84\u5230\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>3. \u5b89\u88c5\u5305\u5931\u8d25<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5b89\u88c5Python\u5305\u65f6\u4f1a\u9047\u5230\u7f51\u7edc\u95ee\u9898\u6216\u6743\u9650\u95ee\u9898\u3002\u5c1d\u8bd5\u4f7f\u7528<code>pip install \u5305\u540d --user<\/code>\u547d\u4ee4\u5728\u5f53\u524d\u7528\u6237\u73af\u5883\u4e2d\u5b89\u88c5\u5305\uff0c\u6216\u4f7f\u7528<code>pip install \u5305\u540d -i https:\/\/pypi.tuna.tsinghua.edu.cn\/simple<\/code>\u547d\u4ee4\u66f4\u6362\u955c\u50cf\u6e90\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f18\u5316Python\u5f00\u53d1\u73af\u5883<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u865a\u62df\u73af\u5883\u53ef\u4ee5\u9694\u79bb\u9879\u76ee\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u907f\u514d\u4e0d\u540c\u9879\u76ee\u4e4b\u95f4\u7684\u4f9d\u8d56\u51b2\u7a81\u3002\u5efa\u8bae\u6bcf\u4e2a\u9879\u76ee\u90fd\u4f7f\u7528\u5355\u72ec\u7684\u865a\u62df\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u7248\u672c\u7ba1\u7406\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u7248\u672c\u7ba1\u7406\u5de5\u5177\uff08\u5982pyenv\uff09\u53ef\u4ee5\u8f7b\u677e\u7ba1\u7406\u591a\u4e2aPython\u7248\u672c\uff0c\u5e76\u5728\u4e0d\u540c\u9879\u76ee\u95f4\u5207\u6362Python\u7248\u672c\u3002\u5b89\u88c5pyenv\u5e76\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\u548c\u5207\u6362Python\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pyenv install 3.9.0<\/p>\n<p>pyenv global 3.9.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u914d\u7f6e\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\uff08\u5982Black\uff09\u53ef\u4ee5\u5e2e\u52a9\u4f60\u4fdd\u6301\u4ee3\u7801\u98ce\u683c\u4e00\u81f4\u3002\u5b89\u88c5Black\u5e76\u914d\u7f6e\u4ee3\u7801\u7f16\u8f91\u5668\u4f7f\u7528Black\u8fdb\u884c\u4ee3\u7801\u683c\u5f0f\u5316\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install black<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728VS Code\u4e2d\uff0c\u5b89\u88c5Python\u63d2\u4ef6\u5e76\u5728\u8bbe\u7f6e\u4e2d\u542f\u7528Black\u4f5c\u4e3a\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h4>4. \u4f7f\u7528Lint\u5de5\u5177<\/h4>\n<\/p>\n<p><p>Lint\u5de5\u5177\uff08\u5982Flake8\uff09\u53ef\u4ee5\u5e2e\u52a9\u4f60\u53d1\u73b0\u4ee3\u7801\u4e2d\u7684\u6f5c\u5728\u95ee\u9898\u3002\u5b89\u88c5Flake8\u5e76\u914d\u7f6e\u4ee3\u7801\u7f16\u8f91\u5668\u4f7f\u7528Flake8\u8fdb\u884c\u4ee3\u7801\u68c0\u67e5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install flake8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728VS Code\u4e2d\uff0c\u5b89\u88c5Python\u63d2\u4ef6\u5e76\u5728\u8bbe\u7f6e\u4e2d\u542f\u7528Flake8\u4f5c\u4e3a\u4ee3\u7801\u68c0\u67e5\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u8fdb\u9636\u914d\u7f6e<\/h3>\n<\/p>\n<p><h4>1. \u914d\u7f6eJupyter Notebook<\/h4>\n<\/p>\n<p><p>Jupyter Notebook\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u7684Python\u5f00\u53d1\u73af\u5883\uff0c\u7279\u522b\u9002\u5408\u6570\u636e\u79d1\u5b66\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u9879\u76ee\u3002\u5b89\u88c5Jupyter Notebook\u5e76\u542f\u52a8\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install jupyter<\/p>\n<p>jupyter notebook<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u542f\u52a8Jupyter Notebook\uff0c\u5e76\u5728\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u5f00\u53d1\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h4>2. \u914d\u7f6eDocker<\/h4>\n<\/p>\n<p><p>Docker\u662f\u4e00\u4e2a\u5bb9\u5668\u5316\u5e73\u53f0\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u9694\u79bb\u7684\u73af\u5883\u4e2d\u8fd0\u884cPython\u5e94\u7528\u3002\u5b89\u88c5Docker\u5e76\u521b\u5efa\u4e00\u4e2aDockerfile\u6765\u5b9a\u4e49\u4f60\u7684Python\u5e94\u7528\u73af\u5883\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Dockerfile\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-Dockerfile\">FROM python:3.9-slim<\/p>\n<p>WORKDIR \/app<\/p>\n<p>COPY . \/app<\/p>\n<p>RUN pip install -r requirements.txt<\/p>\n<p>CMD [&quot;python&quot;, &quot;app.py&quot;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6784\u5efa\u5e76\u8fd0\u884cDocker\u5bb9\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">docker build -t my-python-app .<\/p>\n<p>docker run -p 5000:5000 my-python-app<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u914d\u7f6e<a href=\"https:\/\/docs.pingcode.com\/blog\/devops\/52691.html\" target=\"_blank\">CI\/CD<\/a><\/h4>\n<\/p>\n<p><p>\u6301\u7eed\u96c6\u6210\/\u6301\u7eed\u4ea4\u4ed8\uff08CI\/CD\uff09\u662f\u73b0\u4ee3\u8f6f\u4ef6\u5f00\u53d1\u6d41\u7a0b\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002\u4f7f\u7528\u5de5\u5177\uff08\u5982GitHub Actions\u3001GitLab CI\u6216Jenkins\uff09\u914d\u7f6eCI\/CD\u7ba1\u9053\uff0c\u53ef\u4ee5\u81ea\u52a8\u5316\u6d4b\u8bd5\u3001\u6784\u5efa\u548c\u90e8\u7f72Python\u5e94\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684GitHub Actions\u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-yaml\">name: CI<\/p>\n<p>on: [push]<\/p>\n<p>jobs:<\/p>\n<p>  build:<\/p>\n<p>    runs-on: ubuntu-latest<\/p>\n<p>    steps:<\/p>\n<p>    - uses: actions\/checkout@v2<\/p>\n<p>    - name: Set up Python<\/p>\n<p>      uses: actions\/setup-python@v2<\/p>\n<p>      with:<\/p>\n<p>        python-version: 3.9<\/p>\n<p>    - name: Install dependencies<\/p>\n<p>      run: |<\/p>\n<p>        python -m pip install --upgrade pip<\/p>\n<p>        pip install flake8<\/p>\n<p>        pip install -r requirements.txt<\/p>\n<p>    - name: Lint with flake8<\/p>\n<p>      run: |<\/p>\n<p>        flake8 .<\/p>\n<p>    - name: Run tests<\/p>\n<p>      run: |<\/p>\n<p>        python -m unittest discover<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5b89\u88c5\u548c\u914d\u7f6ePython\u73af\u5883\u53d8\u91cf\u662fPython\u5f00\u53d1\u7684\u57fa\u7840\u6b65\u9aa4\u3002\u901a\u8fc7\u4e0b\u8f7d\u5e76\u5b89\u88c5Python\u3001\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u3001\u9a8c\u8bc1\u5b89\u88c5\u3001\u5b89\u88c5\u548c\u914d\u7f6e\u5e38\u7528Python\u5e93\u3001\u914d\u7f6eIDE\u548c\u6587\u672c\u7f16\u8f91\u5668\u3001\u89e3\u51b3\u5e38\u89c1\u95ee\u9898\u3001\u4f18\u5316\u5f00\u53d1\u73af\u5883\u3001\u8fdb\u9636\u914d\u7f6e\u7b49\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u9ad8\u6548\u7684Python\u5f00\u53d1\u73af\u5883\u3002\u638c\u63e1\u8fd9\u4e9b\u6280\u80fd\u4e0d\u4ec5\u80fd\u5e2e\u52a9\u4f60\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\uff0c\u8fd8\u80fd\u786e\u4fdd\u4ee3\u7801\u7684\u8d28\u91cf\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u68c0\u67e5Python\u662f\u5426\u5df2\u6b63\u786e\u5b89\u88c5\uff1f<\/strong><br \/>\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8f93\u5165<code>python --version<\/code>\u6216<code>python3 --version<\/code>\uff0c\u53ef\u4ee5\u67e5\u770b\u5f53\u524d\u5b89\u88c5\u7684Python\u7248\u672c\u3002\u5982\u679c\u663e\u793a\u7248\u672c\u53f7\uff0c\u8bf4\u660ePython\u5df2\u7ecf\u6b63\u786e\u5b89\u88c5\u3002\u5982\u679c\u51fa\u73b0\u201c\u672a\u627e\u5230\u547d\u4ee4\u201d\u7684\u63d0\u793a\uff0c\u53ef\u80fd\u9700\u8981\u91cd\u65b0\u5b89\u88c5Python\u6216\u68c0\u67e5\u73af\u5883\u53d8\u91cf\u8bbe\u7f6e\u3002<\/p>\n<p><strong>\u4e3a\u4ec0\u4e48\u9700\u8981\u8bbe\u7f6ePython\u7684\u73af\u5883\u53d8\u91cf\uff1f<\/strong><br \/>\u8bbe\u7f6ePython\u7684\u73af\u5883\u53d8\u91cf\u4f7f\u5f97\u5728\u4efb\u4f55\u76ee\u5f55\u4e0b\u90fd\u80fd\u76f4\u63a5\u4f7f\u7528Python\u547d\u4ee4\u3002\u6ca1\u6709\u73af\u5883\u53d8\u91cf\u7684\u60c5\u51b5\u4e0b\uff0c\u7528\u6237\u5fc5\u987b\u5728Python\u7684\u5b89\u88c5\u76ee\u5f55\u4e2d\u6267\u884c\u547d\u4ee4\uff0c\u8fd9\u6837\u4f1a\u964d\u4f4e\u4f7f\u7528\u6548\u7387\uff0c\u5c24\u5176\u662f\u5728\u8fdb\u884c\u5f00\u53d1\u65f6\uff0c\u9891\u7e41\u7684\u8def\u5f84\u5207\u6362\u4f1a\u663e\u5f97\u975e\u5e38\u7e41\u7410\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Windows\u4e0a\u8bbe\u7f6ePython\u7684\u73af\u5883\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Windows\u4e0a\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8bbe\u7f6ePython\u7684\u73af\u5883\u53d8\u91cf\uff1a  <\/p>\n<ol>\n<li>\u53f3\u952e\u70b9\u51fb\u201c\u6b64\u7535\u8111\u201d\u6216\u201c\u8ba1\u7b97\u673a\u201d\uff0c\u9009\u62e9\u201c\u5c5e\u6027\u201d\u3002  <\/li>\n<li>\u70b9\u51fb\u201c\u9ad8\u7ea7\u7cfb\u7edf\u8bbe\u7f6e\u201d\uff0c\u7136\u540e\u9009\u62e9\u201c\u73af\u5883\u53d8\u91cf\u201d\u3002  <\/li>\n<li>\u5728\u201c\u7cfb\u7edf\u53d8\u91cf\u201d\u90e8\u5206\u627e\u5230\u201cPath\u201d\u53d8\u91cf\uff0c\u9009\u4e2d\u540e\u70b9\u51fb\u201c\u7f16\u8f91\u201d\u3002  <\/li>\n<li>\u6dfb\u52a0Python\u7684\u5b89\u88c5\u8def\u5f84\uff08\u4f8b\u5982<code>C:\\Python39<\/code>\u548c<code>C:\\Python39\\Scripts<\/code>\uff09\uff0c\u7136\u540e\u70b9\u51fb\u201c\u786e\u5b9a\u201d\u3002<br \/>\u5b8c\u6210\u540e\uff0c\u5173\u95ed\u547d\u4ee4\u63d0\u793a\u7b26\u5e76\u91cd\u65b0\u6253\u5f00\uff0c\u4ee5\u786e\u4fdd\u65b0\u7684\u8bbe\u7f6e\u751f\u6548\u3002<\/li>\n<\/ol>\n<p><strong>\u5728Mac\u6216Linux\u4e0a\u5982\u4f55\u914d\u7f6ePython\u7684\u73af\u5883\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Mac\u6216Linux\u4e0a\uff0c\u53ef\u4ee5\u901a\u8fc7\u7f16\u8f91<code>~\/.bash_profile<\/code>\u6216<code>~\/.bashrc<\/code>\u6587\u4ef6\u6765\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u3002\u6253\u5f00\u7ec8\u7aef\uff0c\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\u6253\u5f00\u6587\u4ef6\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u884c\uff1a  <\/p>\n<pre><code>export PATH=&quot;\/usr\/local\/bin\/python3:$PATH&quot;\n<\/code><\/pre>\n<p>\u4fdd\u5b58\u6587\u4ef6\u540e\uff0c\u8fd0\u884c<code>source ~\/.bash_profile<\/code>\u6216<code>source ~\/.bashrc<\/code>\u547d\u4ee4\u4f7f\u66f4\u6539\u751f\u6548\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u7ec8\u7aef\u4e2d\u76f4\u63a5\u4f7f\u7528Python\u547d\u4ee4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u5b89\u88c5\u914d\u7f6epython\u73af\u5883\u53d8\u91cf \u5b89\u88c5Python\u3001\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u3001\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\u662f\u5b89\u88c5\u914d\u7f6ePython\u73af\u5883\u53d8 [&hellip;]","protected":false},"author":3,"featured_media":1094755,"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\/1094747"}],"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=1094747"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094747\/revisions"}],"predecessor-version":[{"id":1094758,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094747\/revisions\/1094758"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1094755"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1094747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1094747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1094747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}