{"id":932804,"date":"2024-12-26T18:05:12","date_gmt":"2024-12-26T10:05:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/932804.html"},"modified":"2024-12-26T18:05:14","modified_gmt":"2024-12-26T10:05:14","slug":"%e5%a6%82%e4%bd%95%e7%94%a8%e7%bb%88%e7%ab%afpython","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/932804.html","title":{"rendered":"\u5982\u4f55\u7528\u7ec8\u7aefpython"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070432\/5e78163f-2415-4e26-98dc-d6cdda609f77.webp\" alt=\"\u5982\u4f55\u7528\u7ec8\u7aefpython\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d\uff1a<br \/><strong>\u4f7f\u7528\u7ec8\u7aef\u8fd0\u884cPython\u7684\u57fa\u672c\u65b9\u6cd5\u3001\u5728\u7ec8\u7aef\u4e2d\u6267\u884cPython\u811a\u672c\u4ee5\u53ca\u5b89\u88c5\u548c\u4f7f\u7528Python\u5e93<\/strong>\u3002\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884cPython\u662f\u5f00\u53d1\u548c\u8c03\u8bd5Python\u4ee3\u7801\u7684\u91cd\u8981\u6280\u80fd\u3002\u9996\u5148\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u5728\u7ec8\u7aef\u4e2d\u542f\u52a8Python\u89e3\u91ca\u5668\uff0c\u8f93\u5165Python\u4ee3\u7801\u5e76\u7acb\u5373\u67e5\u770b\u7ed3\u679c\u3002\u5176\u6b21\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u6267\u884cPython\u811a\u672c\u6587\u4ef6\uff0c\u8fd9\u5bf9\u4e8e\u8fd0\u884c\u5927\u578b\u7a0b\u5e8f\u5c24\u5176\u6709\u7528\u3002\u6b64\u5916\uff0c\u7ec8\u7aef\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5b89\u88c5\u548c\u7ba1\u7406Python\u5e93\uff0c\u8fd9\u4f7f\u5f97\u5f00\u53d1\u66f4\u52a0\u9ad8\u6548\u3002\u5728\u8fd9\u4e09\u79cd\u65b9\u6cd5\u4e2d\uff0c<strong>\u5728\u7ec8\u7aef\u4e2d\u6267\u884cPython\u811a\u672c<\/strong>\u662f\u6700\u5e38\u89c1\u548c\u6700\u5b9e\u7528\u7684\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u4f60\u8fd0\u884c\u9884\u5148\u7f16\u5199\u7684\u4ee3\u7801\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u6765\u5f71\u54cd\u7a0b\u5e8f\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u7ec8\u7aef\u8fd0\u884cPython\u89e3\u91ca\u5668<br \/>\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884cPython\u89e3\u91ca\u5668\u662f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\u4e4b\u4e00\u3002\u4f60\u53ea\u9700\u8981\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165<code>python<\/code>\u6216<code>python3<\/code>\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u4f60\u7684\u7cfb\u7edf\u914d\u7f6e\uff0c\u5373\u53ef\u8fdb\u5165Python\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u7528\u4e8e\u6d4b\u8bd5\u7b80\u5355\u7684\u4ee3\u7801\u7247\u6bb5\u6216\u8fdb\u884c\u57fa\u672c\u7684\u6570\u5b66\u8fd0\u7b97\u3002Python\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5373\u65f6\u53cd\u9988\u7684\u73af\u5883\uff0c\u8fd9\u5bf9\u4e8e\u5b66\u4e60\u548c\u6d4b\u8bd5\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<p><p>1.1\u3001\u8fdb\u5165Python\u89e3\u91ca\u5668<br \/>\u8981\u8fdb\u5165Python\u89e3\u91ca\u5668\uff0c\u4f60\u53ea\u9700\u6253\u5f00\u7ec8\u7aef\u5e76\u952e\u5165<code>python<\/code>\u6216<code>python3<\/code>\u3002\u5982\u679c\u5b89\u88c5\u4e86\u591a\u4e2a\u7248\u672c\u7684Python\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u6307\u5b9a\u7248\u672c\u53f7\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>python3.8<\/code>\u53ef\u4ee5\u8fdb\u5165Python 3.8\u7684\u89e3\u91ca\u5668\u3002\u8fdb\u5165\u89e3\u91ca\u5668\u540e\uff0c\u4f60\u4f1a\u770b\u5230\u4e00\u4e2a\u63d0\u793a\u7b26<code>&gt;&gt;&gt;<\/code>\uff0c\u8fd9\u8868\u793a\u4f60\u53ef\u4ee5\u5f00\u59cb\u8f93\u5165Python\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>1.2\u3001\u4f7f\u7528Python\u89e3\u91ca\u5668<br \/>\u5728\u89e3\u91ca\u5668\u4e2d\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u8f93\u5165Python\u4ee3\u7801\u5e76\u6309Enter\u952e\u6267\u884c\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u8f93\u5165<code>print(&quot;Hello, World!&quot;)<\/code>\uff0c\u7136\u540e\u6309Enter\u952e\uff0c\u8f93\u51fa\u5c06\u7acb\u5373\u663e\u793a\u5728\u7ec8\u7aef\u4e2d\u3002\u4f60\u8fd8\u53ef\u4ee5\u8fdb\u884c\u53d8\u91cf\u8d4b\u503c\u3001\u8c03\u7528\u51fd\u6570\u548c\u5bfc\u5165\u6a21\u5757\u3002\u8fd9\u79cd\u4ea4\u4e92\u65b9\u5f0f\u7279\u522b\u9002\u5408\u4e8e\u5b9e\u9a8c\u548c\u8c03\u8bd5\u77ed\u5c0f\u7684\u4ee3\u7801\u7247\u6bb5\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5728\u7ec8\u7aef\u4e2d\u6267\u884cPython\u811a\u672c<br \/>\u5728\u7ec8\u7aef\u4e2d\u6267\u884cPython\u811a\u672c\u662f\u8fd0\u884c\u9884\u5148\u7f16\u5199\u7684Python\u7a0b\u5e8f\u7684\u5e38\u7528\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u5c06\u4ee3\u7801\u4fdd\u5b58\u5230\u4e00\u4e2a<code>.py<\/code>\u6587\u4ef6\u4e2d\uff0c\u5e76\u901a\u8fc7\u547d\u4ee4\u884c\u6267\u884c\u8be5\u6587\u4ef6\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u4e8e\u8fd0\u884c\u590d\u6742\u7684\u7a0b\u5e8f\u6216\u9700\u8981\u591a\u6b21\u8fd0\u884c\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>2.1\u3001\u7f16\u5199Python\u811a\u672c<br \/>\u9996\u5148\uff0c\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\uff08\u5982VS Code\u3001Sublime Text\u6216Atom\uff09\u7f16\u5199Python\u4ee3\u7801\uff0c\u5e76\u4fdd\u5b58\u4e3a<code>.py<\/code>\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u5c06\u4ee5\u4e0b\u4ee3\u7801\u4fdd\u5b58\u4e3a<code>hello.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    print(&quot;Hello, World!&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2.2\u3001\u6267\u884cPython\u811a\u672c<br \/>\u8981\u6267\u884cPython\u811a\u672c\uff0c\u6253\u5f00\u7ec8\u7aef\u5e76\u5bfc\u822a\u5230\u811a\u672c\u6240\u5728\u7684\u76ee\u5f55\u3002\u7136\u540e\u8f93\u5165<code>python hello.py<\/code>\u6216<code>python3 hello.py<\/code>\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u4f60\u7684Python\u7248\u672c\u3002\u811a\u672c\u5c06\u88ab\u6267\u884c\uff0c\u8f93\u51fa\u7ed3\u679c\u5c06\u663e\u793a\u5728\u7ec8\u7aef\u4e2d\u3002\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u5411\u811a\u672c\u4f20\u9012\u6570\u636e\uff0c\u8fd9\u5728\u7f16\u5199\u901a\u7528\u811a\u672c\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5728\u7ec8\u7aef\u4e2d\u5b89\u88c5\u548c\u7ba1\u7406Python\u5e93<br \/>\u7ec8\u7aef\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u8fd0\u884cPython\u4ee3\u7801\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5b89\u88c5\u548c\u7ba1\u7406Python\u5e93\u3002Python\u7684\u5305\u7ba1\u7406\u5668pip\u4f7f\u5f97\u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002\u901a\u8fc7\u5728\u7ec8\u7aef\u4e2d\u4f7f\u7528pip\u547d\u4ee4\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5b89\u88c5\u3001\u66f4\u65b0\u548c\u5378\u8f7dPython\u5e93\u3002<\/p>\n<\/p>\n<p><p>3.1\u3001\u5b89\u88c5Python\u5e93<br \/>\u8981\u5b89\u88c5Python\u5e93\uff0c\u6253\u5f00\u7ec8\u7aef\u5e76\u8f93\u5165<code>pip install package_name<\/code>\uff0c\u5c06<code>package_name<\/code>\u66ff\u6362\u4e3a\u4f60\u8981\u5b89\u88c5\u7684\u5e93\u7684\u540d\u79f0\u3002\u4f8b\u5982\uff0c\u8f93\u5165<code>pip install requests<\/code>\u5c06\u5b89\u88c5<code>requests<\/code>\u5e93\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u8be5\u5e93\u3002<\/p>\n<\/p>\n<p><p>3.2\u3001\u7ba1\u7406Python\u5e93<br \/>\u4f60\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u6765\u7ba1\u7406\u5df2\u5b89\u88c5\u7684\u5e93\u3002\u8f93\u5165<code>pip list<\/code>\u53ef\u4ee5\u67e5\u770b\u6240\u6709\u5df2\u5b89\u88c5\u7684\u5e93\u53ca\u5176\u7248\u672c\u3002\u8981\u66f4\u65b0\u5e93\uff0c\u8f93\u5165<code>pip install --upgrade package_name<\/code>\u3002\u8981\u5378\u8f7d\u5e93\uff0c\u8f93\u5165<code>pip uninstall package_name<\/code>\u3002\u8fd9\u4e9b\u547d\u4ee4\u4f7f\u5f97\u5e93\u7684\u7ba1\u7406\u53d8\u5f97\u9ad8\u6548\u548c\u4fbf\u6377\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5728\u7ec8\u7aef\u4e2d\u4f7f\u7528\u865a\u62df\u73af\u5883<br \/>\u865a\u62df\u73af\u5883\u662f\u4e00\u79cd\u5728\u9879\u76ee\u4e2d\u521b\u5efa\u9694\u79bb\u7684Python\u73af\u5883\u7684\u5de5\u5177\uff0c\u5b83\u5141\u8bb8\u4f60\u5728\u4e0d\u5f71\u54cd\u7cfb\u7edf\u5176\u4ed6\u90e8\u5206\u7684\u60c5\u51b5\u4e0b\u4e3a\u9879\u76ee\u5b89\u88c5\u7279\u5b9a\u7684\u5e93\u548c\u7248\u672c\u3002\u4f7f\u7528\u865a\u62df\u73af\u5883\u53ef\u4ee5\u907f\u514d\u4e0d\u540c\u9879\u76ee\u4e4b\u95f4\u7684\u5e93\u7248\u672c\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><p>4.1\u3001\u521b\u5efa\u865a\u62df\u73af\u5883<br \/>\u8981\u521b\u5efa\u865a\u62df\u73af\u5883\uff0c\u9996\u5148\u786e\u4fdd\u5df2\u5b89\u88c5<code>virtualenv<\/code>\u6216<code>venv<\/code>\u6a21\u5757\u3002\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u6253\u5f00\u7ec8\u7aef\uff0c\u8f93\u5165<code>python -m venv env_name<\/code>\uff0c\u5c06<code>env_name<\/code>\u66ff\u6362\u4e3a\u865a\u62df\u73af\u5883\u7684\u540d\u79f0\u3002\u521b\u5efa\u5b8c\u6210\u540e\uff0c\u865a\u62df\u73af\u5883\u5c06\u5305\u542b\u4e00\u4e2a\u72ec\u7acb\u7684Python\u89e3\u91ca\u5668\u548cpip\u3002<\/p>\n<\/p>\n<p><p>4.2\u3001\u6fc0\u6d3b\u548c\u4f7f\u7528\u865a\u62df\u73af\u5883<br \/>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u8f93\u5165<code>.\\env_name\\Scripts\\activate<\/code>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u3002\u5728Mac\u6216Linux\u7cfb\u7edf\u4e2d\uff0c\u8f93\u5165<code>source env_name\/bin\/activate<\/code>\u3002\u6fc0\u6d3b\u540e\uff0c\u7ec8\u7aef\u63d0\u793a\u7b26\u5c06\u663e\u793a\u865a\u62df\u73af\u5883\u7684\u540d\u79f0\u3002\u6b64\u65f6\uff0c\u6240\u6709\u7684pip\u64cd\u4f5c\u90fd\u5c06\u4f5c\u7528\u4e8e\u8fd9\u4e2a\u865a\u62df\u73af\u5883\u3002\u8981\u9000\u51fa\u865a\u62df\u73af\u5883\uff0c\u8f93\u5165<code>deactivate<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5728\u7ec8\u7aef\u4e2d\u8c03\u8bd5Python\u4ee3\u7801<br \/>\u8c03\u8bd5\u662f\u7f16\u7a0b\u8fc7\u7a0b\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002Python\u63d0\u4f9b\u4e86\u591a\u4e2a\u5de5\u5177\u548c\u5e93\u6765\u5e2e\u52a9\u5f00\u53d1\u8005\u5728\u7ec8\u7aef\u4e2d\u8c03\u8bd5\u4ee3\u7801\u3002\u901a\u8fc7\u8c03\u8bd5\uff0c\u4f60\u53ef\u4ee5\u6df1\u5165\u4e86\u89e3\u4ee3\u7801\u7684\u6267\u884c\u8fc7\u7a0b\uff0c\u5e76\u627e\u51fa\u548c\u4fee\u6b63\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>5.1\u3001\u4f7f\u7528pdb\u8fdb\u884c\u8c03\u8bd5<br \/>Python\u81ea\u5e26\u7684pdb\u6a21\u5757\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u547d\u4ee4\u884c\u8c03\u8bd5\u5668\u3002\u8981\u4f7f\u7528pdb\u8c03\u8bd5Python\u811a\u672c\uff0c\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165pdb\u5e76\u63d2\u5165<code>pdb.set_trace()<\/code>\uff0c\u811a\u672c\u6267\u884c\u5230\u6b64\u5904\u5c06\u6682\u505c\uff0c\u5e76\u8fdb\u5165pdb\u8c03\u8bd5\u6a21\u5f0f\u3002\u5728\u7ec8\u7aef\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528pdb\u547d\u4ee4\u5982<code>n<\/code>\uff08next\uff09\u3001<code>c<\/code>\uff08continue\uff09\u3001<code>q<\/code>\uff08quit\uff09\u7b49\u6765\u63a7\u5236\u4ee3\u7801\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<p><p>5.2\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u8c03\u8bd5\u5de5\u5177<br \/>\u9664\u4e86pdb\uff0c\u8fd8\u6709\u8bb8\u591a\u7b2c\u4e09\u65b9\u8c03\u8bd5\u5de5\u5177\u53ef\u4ee5\u5728\u7ec8\u7aef\u4e2d\u4f7f\u7528\u3002\u4f8b\u5982\uff0c<code>ipdb<\/code>\u662fpdb\u7684\u589e\u5f3a\u7248\uff0c\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\u3002\u8981\u4f7f\u7528ipdb\uff0c\u9996\u5148\u901a\u8fc7pip\u5b89\u88c5<code>ipdb<\/code>\u5e93\uff0c\u7136\u540e\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528<code>ipdb.set_trace()<\/code>\u3002ipdb\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u547d\u4ee4\u548c\u66f4\u53cb\u597d\u7684\u754c\u9762\uff0c\u9002\u5408\u4e8e\u590d\u6742\u9879\u76ee\u7684\u8c03\u8bd5\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5728\u7ec8\u7aef\u4e2d\u4f7f\u7528\u7248\u672c\u63a7\u5236\u4e0ePython<br \/>\u7248\u672c\u63a7\u5236\u7cfb\u7edf\uff08VCS\uff09\u662f\u73b0\u4ee3\u8f6f\u4ef6\u5f00\u53d1\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002Git\u662f\u6700\u6d41\u884c\u7684\u7248\u672c\u63a7\u5236\u7cfb\u7edf\u4e4b\u4e00\uff0c\u7ed3\u5408Python\u5f00\u53d1\u53ef\u4ee5\u63d0\u9ad8\u9879\u76ee\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u534f\u4f5c\u6027\u3002\u5728\u7ec8\u7aef\u4e2d\u4f7f\u7528Git\u8fdb\u884c\u7248\u672c\u63a7\u5236\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7ba1\u7406\u4ee3\u7801\u7684\u53d8\u66f4\uff0c\u534f\u4f5c\u5f00\u53d1\uff0c\u4ee5\u53ca\u56de\u6eda\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>6.1\u3001\u4f7f\u7528Git\u521d\u59cb\u5316Python\u9879\u76ee<br \/>\u8981\u5728\u7ec8\u7aef\u4e2d\u4f7f\u7528Git\u7ba1\u7406Python\u9879\u76ee\uff0c\u9996\u5148\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u8f93\u5165<code>git init<\/code>\u521d\u59cb\u5316Git\u4ed3\u5e93\u3002\u63a5\u4e0b\u6765\uff0c\u521b\u5efa<code>.gitignore<\/code>\u6587\u4ef6\uff0c\u5217\u51fa\u4e0d\u9700\u8981\u8ddf\u8e2a\u7684\u6587\u4ef6\u548c\u76ee\u5f55\uff0c\u4f8b\u5982<code>__pycache__<\/code>\u3001<code>.vscode<\/code>\u7b49\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>git add .<\/code>\u5c06\u6240\u6709\u6587\u4ef6\u6dfb\u52a0\u5230\u6682\u5b58\u533a\uff0c\u5e76\u4f7f\u7528<code>git commit -m &quot;Initial commit&quot;<\/code>\u63d0\u4ea4\u5230\u4ed3\u5e93\u3002<\/p>\n<\/p>\n<p><p>6.2\u3001\u5e38\u7528Git\u547d\u4ee4<br \/>\u5728Python\u9879\u76ee\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u5e38\u7528\u7684Git\u547d\u4ee4\u5305\u62ec<code>git status<\/code>\uff08\u67e5\u770b\u4ed3\u5e93\u72b6\u6001\uff09\u3001<code>git diff<\/code>\uff08\u67e5\u770b\u53d8\u66f4\uff09\u3001<code>git log<\/code>\uff08\u67e5\u770b\u63d0\u4ea4\u5386\u53f2\uff09\u3001<code>git branch<\/code>\uff08\u7ba1\u7406\u5206\u652f\uff09\u3001<code>git merge<\/code>\uff08\u5408\u5e76\u5206\u652f\uff09\u7b49\u3002\u901a\u8fc7\u8fd9\u4e9b\u547d\u4ee4\uff0c\u4f60\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u9879\u76ee\u7684\u53d8\u66f4\u548c\u534f\u4f5c\u5f00\u53d1\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u5728\u7ec8\u7aef\u4e2d\u6267\u884c\u81ea\u52a8\u5316\u4efb\u52a1<br \/>\u7ec8\u7aef\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u8fd0\u884cPython\u4ee3\u7801\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u6267\u884c\u81ea\u52a8\u5316\u4efb\u52a1\u3002\u901a\u8fc7\u7f16\u5199\u811a\u672c\u548c\u4f7f\u7528\u4efb\u52a1\u8c03\u5ea6\u5de5\u5177\uff0c\u4f60\u53ef\u4ee5\u81ea\u52a8\u5316\u5e38\u89c1\u7684\u5f00\u53d1\u4efb\u52a1\uff0c\u5982\u4ee3\u7801\u6d4b\u8bd5\u3001\u6253\u5305\u548c\u90e8\u7f72\u3002<\/p>\n<\/p>\n<p><p>7.1\u3001\u4f7f\u7528Makefile\u8fdb\u884c\u81ea\u52a8\u5316<br \/>Makefile\u662f\u4e00\u79cd\u7b80\u5355\u800c\u5f3a\u5927\u7684\u81ea\u52a8\u5316\u5de5\u5177\u3002\u4f60\u53ef\u4ee5\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u521b\u5efa<code>Makefile<\/code>\u6587\u4ef6\uff0c\u5b9a\u4e49\u5e38\u89c1\u7684\u4efb\u52a1\u548c\u547d\u4ee4\u3002\u4f8b\u5982\uff0c\u5b9a\u4e49<code>test<\/code>\u4efb\u52a1\u8fd0\u884c\u5355\u5143\u6d4b\u8bd5\uff0c\u5b9a\u4e49<code>build<\/code>\u4efb\u52a1\u6253\u5305\u9879\u76ee\uff0c\u5b9a\u4e49<code>deploy<\/code>\u4efb\u52a1\u90e8\u7f72\u4ee3\u7801\u3002\u7136\u540e\uff0c\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165<code>make test<\/code>\u3001<code>make build<\/code>\u7b49\u547d\u4ee4\u5373\u53ef\u6267\u884c\u76f8\u5e94\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>7.2\u3001\u4f7f\u7528Python\u811a\u672c\u8fdb\u884c\u81ea\u52a8\u5316<br \/>\u9664\u4e86Makefile\uff0c\u4f60\u8fd8\u53ef\u4ee5\u7f16\u5199Python\u811a\u672c\u6765\u81ea\u52a8\u5316\u4efb\u52a1\u3002\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684<code>subprocess<\/code>\u6a21\u5757\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u8c03\u7528\u7ec8\u7aef\u547d\u4ee4\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2aPython\u811a\u672c\uff0c\u4f7f\u7528<code>subprocess.run([&quot;pytest&quot;])<\/code>\u8c03\u7528pytest\u8fd0\u884c\u6d4b\u8bd5\uff0c\u4f7f\u7528<code>subprocess.run([&quot;twine&quot;, &quot;upload&quot;, &quot;dist\/*&quot;])<\/code>\u4e0a\u4f20\u5305\u5230PyPI\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5c06\u590d\u6742\u7684\u81ea\u52a8\u5316\u4efb\u52a1\u5c01\u88c5\u5230Python\u811a\u672c\u4e2d\uff0c\u7b80\u5316\u5f00\u53d1\u6d41\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u5728\u7ec8\u7aef\u4e2d\u4f7f\u7528\u5bb9\u5668\u5316\u6280\u672f<br \/>\u5bb9\u5668\u5316\u6280\u672f\u4f7f\u5f97\u5e94\u7528\u7a0b\u5e8f\u7684\u90e8\u7f72\u548c\u8fd0\u884c\u66f4\u52a0\u9ad8\u6548\u548c\u4e00\u81f4\u3002Docker\u662f\u6700\u6d41\u884c\u7684\u5bb9\u5668\u5316\u5de5\u5177\u4e4b\u4e00\uff0c\u5b83\u5141\u8bb8\u4f60\u5728\u4e00\u4e2a\u9694\u79bb\u7684\u73af\u5883\u4e2d\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u3002\u5728\u7ec8\u7aef\u4e2d\u4f7f\u7528Docker\u53ef\u4ee5\u5e2e\u52a9\u4f60\u6784\u5efa\u3001\u6d4b\u8bd5\u548c\u90e8\u7f72Python\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>8.1\u3001\u521b\u5efaDockerfile<br \/>\u8981\u5c06Python\u5e94\u7528\u5bb9\u5668\u5316\uff0c\u9996\u5148\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u521b\u5efa<code>Dockerfile<\/code>\u3002\u5728Dockerfile\u4e2d\uff0c\u5b9a\u4e49\u57fa\u7840\u955c\u50cf\uff08\u5982<code>python:3.8<\/code>\uff09\uff0c\u590d\u5236\u9879\u76ee\u6587\u4ef6\u5230\u955c\u50cf\u4e2d\uff0c\u5b89\u88c5\u4f9d\u8d56\u5e93\uff0c\u5e76\u5b9a\u4e49\u542f\u52a8\u547d\u4ee4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-Dockerfile\">FROM python:3.8<\/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>8.2\u3001\u6784\u5efa\u548c\u8fd0\u884cDocker\u955c\u50cf<br \/>\u5728\u7ec8\u7aef\u4e2d\uff0c\u4f7f\u7528<code>docker build -t my-python-app .<\/code>\u547d\u4ee4\u6784\u5efaDocker\u955c\u50cf\uff0c<code>my-python-app<\/code>\u662f\u955c\u50cf\u7684\u540d\u79f0\u3002\u6784\u5efa\u5b8c\u6210\u540e\uff0c\u4f7f\u7528<code>docker run -p 5000:5000 my-python-app<\/code>\u8fd0\u884c\u5bb9\u5668\uff0c<code>-p<\/code>\u9009\u9879\u5c06\u5bb9\u5668\u7684\u7aef\u53e3\u6620\u5c04\u5230\u4e3b\u673a\u7684\u7aef\u53e3\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5728\u9694\u79bb\u7684\u73af\u5883\u4e2d\u8fd0\u884cPython\u5e94\u7528\uff0c\u907f\u514d\u73af\u5883\u5dee\u5f02\u5bfc\u81f4\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u5728\u7ec8\u7aef\u4e2d\u4f7f\u7528\u8fdc\u7a0b\u670d\u52a1\u5668<br \/>\u5728\u5f00\u53d1\u548c\u90e8\u7f72Python\u5e94\u7528\u65f6\uff0c\u5e38\u5e38\u9700\u8981\u5728\u8fdc\u7a0b\u670d\u52a1\u5668\u4e0a\u8fd0\u884c\u4ee3\u7801\u3002\u901a\u8fc7\u7ec8\u7aef\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528SSH\u8fde\u63a5\u5230\u8fdc\u7a0b\u670d\u52a1\u5668\uff0c\u6267\u884c\u547d\u4ee4\u548c\u7ba1\u7406\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>9.1\u3001\u4f7f\u7528SSH\u8fde\u63a5\u8fdc\u7a0b\u670d\u52a1\u5668<br \/>\u8981\u4f7f\u7528SSH\u8fde\u63a5\u5230\u8fdc\u7a0b\u670d\u52a1\u5668\uff0c\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165<code>ssh username@hostname<\/code>\uff0c<code>username<\/code>\u662f\u4f60\u7684\u7528\u6237\u540d\uff0c<code>hostname<\/code>\u662f\u670d\u52a1\u5668\u7684\u5730\u5740\u3002\u8fde\u63a5\u6210\u529f\u540e\uff0c\u4f60\u5c06\u5728\u8fdc\u7a0b\u670d\u52a1\u5668\u7684\u7ec8\u7aef\u4e2d\u6267\u884c\u547d\u4ee4\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>scp<\/code>\u547d\u4ee4\u4e0a\u4f20\u548c\u4e0b\u8f7d\u6587\u4ef6\uff0c\u4f8b\u5982<code>scp localfile username@hostname:\/remotepath<\/code>\u3002<\/p>\n<\/p>\n<p><p>9.2\u3001\u5728\u8fdc\u7a0b\u670d\u52a1\u5668\u4e0a\u8fd0\u884cPython\u4ee3\u7801<br \/>\u5728\u8fde\u63a5\u5230\u8fdc\u7a0b\u670d\u52a1\u5668\u540e\uff0c\u4f60\u53ef\u4ee5\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884cPython\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u8f93\u5165<code>python script.py<\/code>\u6267\u884cPython\u811a\u672c\uff0c\u8f93\u5165<code>pip install package_name<\/code>\u5b89\u88c5Python\u5e93\u3002\u5728\u8fdc\u7a0b\u670d\u52a1\u5668\u4e0a\u8fd0\u884c\u4ee3\u7801\u5141\u8bb8\u4f60\u5229\u7528\u670d\u52a1\u5668\u7684\u8ba1\u7b97\u8d44\u6e90\u548c\u7f51\u7edc\u73af\u5883\uff0c\u9002\u5408\u4e8e\u90e8\u7f72\u548c\u6d4b\u8bd5\u5927\u89c4\u6a21\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u4e2a\u65b9\u9762\uff0c\u4f60\u53ef\u4ee5\u5168\u9762\u638c\u63e1\u5728\u7ec8\u7aef\u4e2d\u4f7f\u7528Python\u7684\u5404\u79cd\u6280\u80fd\u3002\u8fd9\u4e9b\u6280\u80fd\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u5f00\u53d1\u6548\u7387\uff0c\u8fd8\u4e3a\u4f60\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u8fd0\u884c\u548c\u7ba1\u7406Python\u5e94\u7528\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u652f\u6301\u3002\u968f\u7740\u5bf9\u7ec8\u7aef\u64cd\u4f5c\u7684\u719f\u7ec3\u638c\u63e1\uff0c\u4f60\u5c06\u80fd\u591f\u66f4\u597d\u5730\u5e94\u5bf9\u5404\u79cd\u5f00\u53d1\u548c\u90e8\u7f72\u6311\u6218\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728\u7ec8\u7aef\u4e2d\u5b89\u88c5Python\uff1f<\/strong><br \/>\u5728\u7ec8\u7aef\u4e2d\u5b89\u88c5Python\u901a\u5e38\u9700\u8981\u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u4f8b\u5982\u5728Ubuntu\u7cfb\u7edf\u4e2d\u53ef\u4ee5\u4f7f\u7528<code>apt<\/code>\u547d\u4ee4\u3002\u8f93\u5165<code>sudo apt update<\/code>\u66f4\u65b0\u8f6f\u4ef6\u5305\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528<code>sudo apt install python3<\/code>\u6765\u5b89\u88c5Python 3\u3002\u5bf9\u4e8emacOS\u7528\u6237\uff0c\u53ef\u4ee5\u901a\u8fc7Homebrew\u8fdb\u884c\u5b89\u88c5\uff0c\u8f93\u5165<code>brew install python<\/code>\u5373\u53ef\u3002Windows\u7528\u6237\u53ef\u4ee5\u76f4\u63a5\u4ece\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u5e76\u5b89\u88c5Python\uff0c\u5b89\u88c5\u5b8c\u6210\u540e\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8f93\u5165<code>python<\/code>\u6765\u9a8c\u8bc1\u5b89\u88c5\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884cPython\u811a\u672c\uff1f<\/strong><br \/>\u8981\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884cPython\u811a\u672c\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u60a8\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u3002\u521b\u5efa\u4e00\u4e2aPython\u6587\u4ef6\uff0c\u4f8b\u5982<code>script.py<\/code>\uff0c\u7136\u540e\u5728\u7ec8\u7aef\u4e2d\u5bfc\u822a\u5230\u8be5\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\u3002\u8f93\u5165<code>python script.py<\/code>\u6216<code>python3 script.py<\/code>\u6765\u6267\u884c\u811a\u672c\u3002\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684Python\u7248\u672c\uff0c\u6839\u636e\u60a8\u7684Python\u73af\u5883\u9009\u62e9\u76f8\u5e94\u7684\u547d\u4ee4\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u7ec8\u7aef\u4e2d\u4f7f\u7528Python\u4ea4\u4e92\u5f0f\u73af\u5883\uff1f<\/strong><br \/>\u5728\u7ec8\u7aef\u4e2d\u4f7f\u7528Python\u7684\u4ea4\u4e92\u5f0f\u73af\u5883\u76f8\u5bf9\u7b80\u5355\u3002\u53ea\u9700\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165<code>python<\/code>\u6216<code>python3<\/code>\uff0c\u7136\u540e\u6309\u4e0b\u56de\u8f66\u952e\uff0c\u60a8\u5c06\u8fdb\u5165Python\u7684\u4ea4\u4e92\u6a21\u5f0f\u3002\u5728\u8fd9\u91cc\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u8f93\u5165Python\u4ee3\u7801\u5e76\u7acb\u5373\u67e5\u770b\u7ed3\u679c\uff0c\u8fd9\u5bf9\u4e8e\u8c03\u8bd5\u548c\u6d4b\u8bd5\u5c0f\u6bb5\u4ee3\u7801\u975e\u5e38\u6709\u5e2e\u52a9\u3002\u8981\u9000\u51fa\u4ea4\u4e92\u5f0f\u73af\u5883\uff0c\u60a8\u53ef\u4ee5\u8f93\u5165<code>exit()<\/code>\u6216\u4f7f\u7528<code>Ctrl + D<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d\uff1a\u4f7f\u7528\u7ec8\u7aef\u8fd0\u884cPython\u7684\u57fa\u672c\u65b9\u6cd5\u3001\u5728\u7ec8\u7aef\u4e2d\u6267\u884cPython\u811a\u672c\u4ee5\u53ca\u5b89\u88c5\u548c\u4f7f\u7528Python\u5e93\u3002\u5728\u7ec8\u7aef [&hellip;]","protected":false},"author":3,"featured_media":932808,"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\/932804"}],"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=932804"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/932804\/revisions"}],"predecessor-version":[{"id":932810,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/932804\/revisions\/932810"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/932808"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=932804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=932804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=932804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}