{"id":955365,"date":"2024-12-27T02:15:33","date_gmt":"2024-12-26T18:15:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/955365.html"},"modified":"2024-12-27T02:15:35","modified_gmt":"2024-12-26T18:15:35","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e5%8f%96%e5%90%af%e5%8a%a8%e5%85%a5%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/955365.html","title":{"rendered":"python\u5982\u4f55\u8c03\u53d6\u542f\u52a8\u5165\u53e3"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095323\/9d293b17-5554-4275-aa56-8515381abb71.webp\" alt=\"python\u5982\u4f55\u8c03\u53d6\u542f\u52a8\u5165\u53e3\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8c03\u53d6\u542f\u52a8\u5165\u53e3\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u4e3b\u51fd\u6570\u3001\u4f7f\u7528<code>if __name__ == &quot;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&quot;<\/code>\u8bed\u53e5\u6765\u5b9e\u73b0\u3001\u4f7f\u7528\u5165\u53e3\u70b9\u914d\u7f6e\u6587\u4ef6\u7b49\u65b9\u5f0f\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528<code>if __name__ == &quot;__main__&quot;<\/code>\u8bed\u53e5\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\u3002\u901a\u8fc7\u5728Python\u811a\u672c\u4e2d\u4f7f\u7528\u8fd9\u4e00\u6761\u4ef6\u8bed\u53e5\uff0c\u53ef\u4ee5\u786e\u4fdd\u7279\u5b9a\u7684\u4ee3\u7801\u5757\u53ea\u5728\u811a\u672c\u4f5c\u4e3a\u4e3b\u7a0b\u5e8f\u8fd0\u884c\u65f6\u6267\u884c\uff0c\u800c\u4e0d\u5728\u5176\u4f5c\u4e3a\u6a21\u5757\u88ab\u5bfc\u5165\u65f6\u6267\u884c\uff0c\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u5730\u7ec4\u7ec7\u548c\u7ba1\u7406\u4ee3\u7801\u3002\u4ee5\u4e0b\u662f\u5bf9\u8fd9\u4e09\u79cd\u65b9\u6cd5\u7684\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b9a\u4e49\u4e3b\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5728Python\u7f16\u7a0b\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u4e3b\u51fd\u6570\u662f\u7ec4\u7ec7\u4ee3\u7801\u7684\u826f\u597d\u5b9e\u8df5\u3002\u867d\u7136Python\u4e0d\u50cfC\u6216Java\u90a3\u6837\u8981\u6c42\u4e00\u4e2a\u660e\u786e\u7684\u5165\u53e3\u70b9\uff0c\u4f46\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a<code>main<\/code>\u7684\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u6e05\u695a\u5730\u8868\u793a\u7a0b\u5e8f\u7684\u4e3b\u8981\u903b\u8f91\u6240\u5728\u3002\u4ee5\u4e0b\u662f\u5b9e\u73b0\u7684\u57fa\u672c\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b9a\u4e49\u4e00\u4e2a\u4e3b\u51fd\u6570<\/strong>\uff1a\u5728\u811a\u672c\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a<code>main<\/code>\u7684\u51fd\u6570\uff0c\u5c06\u7a0b\u5e8f\u7684\u4e3b\u8981\u903b\u8f91\u548c\u529f\u80fd\u653e\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\u3002\u8fd9\u6837\u505a\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u56e0\u4e3a\u5176\u4ed6\u5f00\u53d1\u8005\u53ef\u4ee5\u5feb\u901f\u8bc6\u522b\u7a0b\u5e8f\u7684\u5165\u53e3\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8c03\u7528\u4e3b\u51fd\u6570<\/strong>\uff1a\u5728\u811a\u672c\u7684\u672b\u5c3e\u4f7f\u7528<code>if __name__ == &quot;__main__&quot;:<\/code>\u8bed\u53e5\u6765\u8c03\u7528\u4e3b\u51fd\u6570\u3002\u8fd9\u4e2a\u6761\u4ef6\u8bed\u53e5\u7684\u4f5c\u7528\u662f\u786e\u4fdd\u53ea\u6709\u5728\u811a\u672c\u4f5c\u4e3a\u4e3b\u7a0b\u5e8f\u8fd0\u884c\u65f6\u624d\u8c03\u7528<code>main<\/code>\u51fd\u6570\uff0c\u800c\u5728\u811a\u672c\u4f5c\u4e3a\u6a21\u5757\u88ab\u5bfc\u5165\u65f6\u4e0d\u4f1a\u8c03\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u53c2\u6570\u4f20\u9012<\/strong>\uff1a\u5982\u679c\u9700\u8981\u4ece\u547d\u4ee4\u884c\u83b7\u53d6\u53c2\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys.argv<\/code>\u5217\u8868\u3002\u53ef\u4ee5\u5728\u4e3b\u51fd\u6570\u4e2d\u5904\u7406\u8fd9\u4e9b\u53c2\u6570\uff0c\u4ee5\u5b9e\u73b0\u66f4\u7075\u6d3b\u7684\u7a0b\u5e8f\u884c\u4e3a\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def main():<\/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>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>main<\/code>\u51fd\u6570\u5305\u542b\u7a0b\u5e8f\u7684\u4e3b\u8981\u903b\u8f91\u3002\u5f53\u811a\u672c\u4f5c\u4e3a\u4e3b\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c<code>main<\/code>\u51fd\u6570\u5c06\u88ab\u8c03\u7528\uff0c\u8f93\u51fa&quot;Hello, World!&quot;\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>if __name__ == &quot;__main__&quot;<\/code>\u8bed\u53e5<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>if __name__ == &quot;__main__&quot;<\/code>\u8bed\u53e5\u662fPython\u7a0b\u5e8f\u4e2d\u8c03\u53d6\u542f\u52a8\u5165\u53e3\u7684\u6807\u51c6\u65b9\u5f0f\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u5b9a\u4e49\u7a0b\u5e8f\u7684\u5165\u53e3\uff0c\u8fd8\u80fd\u907f\u514d\u5728\u811a\u672c\u88ab\u7528\u4f5c\u6a21\u5757\u5bfc\u5165\u65f6\u6267\u884c\u4e0d\u5fc5\u8981\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7406\u89e3<code>__name__<\/code>\u53d8\u91cf<\/strong>\uff1a\u5728Python\u4e2d\uff0c\u6bcf\u4e2a\u6a21\u5757\u90fd\u6709\u4e00\u4e2a\u540d\u79f0\u3002\u5f53\u6a21\u5757\u88ab\u76f4\u63a5\u8fd0\u884c\u65f6\uff0c<code>__name__<\/code>\u7684\u503c\u662f<code>&quot;__main__&quot;<\/code>\uff0c\u800c\u5f53\u6a21\u5757\u88ab\u5bfc\u5165\u65f6\uff0c<code>__name__<\/code>\u7684\u503c\u662f\u6a21\u5757\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5<\/strong>\uff1a\u901a\u8fc7<code>if __name__ == &quot;__main__&quot;:<\/code>\u6761\u4ef6\u8bed\u53e5\uff0c\u53ef\u4ee5\u786e\u4fdd\u53ea\u6709\u5728\u811a\u672c\u4f5c\u4e3a\u4e3b\u7a0b\u5e8f\u8fd0\u884c\u65f6\u624d\u6267\u884c\u7279\u5b9a\u7684\u4ee3\u7801\u5757\u3002\u8fd9\u79cd\u65b9\u5f0f\u5e7f\u6cdb\u7528\u4e8e\u5b9a\u4e49\u7a0b\u5e8f\u7684\u6267\u884c\u5165\u53e3\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5206\u79bb\u529f\u80fd\u548c\u6267\u884c<\/strong>\uff1a\u8fd9\u79cd\u65b9\u6cd5\u6709\u52a9\u4e8e\u5c06\u6a21\u5757\u7684\u529f\u80fd\u90e8\u5206\u4e0e\u6267\u884c\u90e8\u5206\u5206\u5f00\uff0c\u4f7f\u5f97\u6a21\u5757\u65e2\u53ef\u4ee5\u88ab\u7528\u4f5c\u72ec\u7acb\u7684\u811a\u672c\u8fd0\u884c\uff0c\u4e5f\u53ef\u4ee5\u88ab\u5176\u4ed6\u811a\u672c\u5bfc\u5165\u5e76\u590d\u7528\u5176\u529f\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def main():<\/p>\n<p>    print(&quot;This script is being run directly.&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5f53\u8fd9\u4e2a\u811a\u672c\u88ab\u76f4\u63a5\u8fd0\u884c\u65f6\uff0c<code>main<\/code>\u51fd\u6570\u4f1a\u88ab\u8c03\u7528\uff0c\u6253\u5370\u6d88\u606f\uff1b\u800c\u5f53\u5b83\u88ab\u5176\u4ed6\u811a\u672c\u5bfc\u5165\u65f6\uff0c<code>main<\/code>\u51fd\u6570\u4e0d\u4f1a\u88ab\u81ea\u52a8\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5165\u53e3\u70b9\u914d\u7f6e\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u590d\u6742\u9879\u76ee\u4e2d\uff0c\u5c24\u5176\u662f\u4f7f\u7528Python\u6253\u5305\u5de5\u5177\uff08\u5982<code>setuptools<\/code>\uff09\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u901a\u8fc7\u5165\u53e3\u70b9\u914d\u7f6e\u6587\u4ef6\u6765\u5b9a\u4e49\u7a0b\u5e8f\u7684\u542f\u52a8\u5165\u53e3\u3002\u8fd9\u79cd\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u6784\u5efa\u53ef\u6267\u884c\u7684\u547d\u4ee4\u884c\u5de5\u5177\u6216\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u5165\u53e3\u70b9<\/strong>\uff1a\u4f7f\u7528<code>setuptools<\/code>\u4e2d\u7684<code>entry_points<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u5728<code>setup.py<\/code>\u4e2d\u5b9a\u4e49\u5165\u53e3\u70b9\u3002\u5165\u53e3\u70b9\u662f\u4e00\u4e2a\u6307\u5411\u7279\u5b9a\u51fd\u6570\u7684\u5b57\u7b26\u4e32\uff0c\u8fd9\u4e2a\u51fd\u6570\u5c06\u5728\u547d\u4ee4\u884c\u8c03\u7528\u65f6\u6267\u884c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6e<code>setup.py<\/code><\/strong>\uff1a\u5728\u9879\u76ee\u7684<code>setup.py<\/code>\u4e2d\uff0c\u4f7f\u7528<code>entry_points<\/code>\u53c2\u6570\u6307\u5b9a\u5165\u53e3\u70b9\u3002\u5165\u53e3\u70b9\u901a\u5e38\u4ee5<code>console_scripts<\/code>\u4e3a\u524d\u7f00\uff0c\u8868\u793a\u8fd9\u662f\u4e00\u4e2a\u547d\u4ee4\u884c\u811a\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5\u548c\u8c03\u7528<\/strong>\uff1a\u5b89\u88c5\u9879\u76ee\u540e\uff0c\u6307\u5b9a\u7684\u5165\u53e3\u70b9\u5c06\u521b\u5efa\u4e00\u4e2a\u53ef\u6267\u884c\u7684\u547d\u4ee4\u884c\u811a\u672c\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u8be5\u811a\u672c\u76f4\u63a5\u8fd0\u884c\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a<code>setup.py<\/code>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from setuptools import setup<\/p>\n<p>setup(<\/p>\n<p>    name=&#39;myproject&#39;,<\/p>\n<p>    version=&#39;0.1&#39;,<\/p>\n<p>    packages=[&#39;myproject&#39;],<\/p>\n<p>    entry_points={<\/p>\n<p>        &#39;console_scripts&#39;: [<\/p>\n<p>            &#39;mycommand=myproject.module:main&#39;,<\/p>\n<p>        ],<\/p>\n<p>    },<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>mycommand<\/code>\u662f\u547d\u4ee4\u884c\u547d\u4ee4\uff0c\u5b83\u4f1a\u8c03\u7528<code>myproject.module<\/code>\u6a21\u5757\u4e2d\u7684<code>main<\/code>\u51fd\u6570\u4f5c\u4e3a\u5165\u53e3\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4e09\u79cd\u65b9\u6cd5\uff0cPython\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u7684\u590d\u6742\u7a0b\u5ea6\u548c\u9700\u6c42\u7075\u6d3b\u5730\u5b9a\u4e49\u542f\u52a8\u5165\u53e3\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u7ec4\u7ec7\u548c\u7ba1\u7406\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bc6\u522b\u811a\u672c\u7684\u542f\u52a8\u5165\u53e3\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u542f\u52a8\u5165\u53e3\u901a\u5e38\u662f\u901a\u8fc7<code>if __name__ == &quot;__main__&quot;:<\/code>\u6765\u8bc6\u522b\u7684\u3002\u8fd9\u6bb5\u4ee3\u7801\u786e\u4fdd\u53ea\u6709\u5728\u811a\u672c\u88ab\u76f4\u63a5\u8fd0\u884c\u65f6\uff0c\u540e\u9762\u7684\u4ee3\u7801\u624d\u4f1a\u88ab\u6267\u884c\u3002\u5982\u679c\u8be5\u811a\u672c\u4f5c\u4e3a\u6a21\u5757\u5bfc\u5165\u5230\u5176\u4ed6\u811a\u672c\u4e2d\uff0c\u8fd9\u90e8\u5206\u4ee3\u7801\u5c06\u4e0d\u4f1a\u88ab\u6267\u884c\u3002\u4f60\u53ef\u4ee5\u5728\u8fd9\u4e2a\u6761\u4ef6\u4e0b\u653e\u7f6e\u4f60\u7684\u4e3b\u7a0b\u5e8f\u903b\u8f91\u3002<\/p>\n<p><strong>\u5982\u4f55\u901a\u8fc7\u547d\u4ee4\u884c\u542f\u52a8Python\u811a\u672c\uff1f<\/strong><br \/>\u8981\u5728\u547d\u4ee4\u884c\u4e2d\u542f\u52a8Python\u811a\u672c\uff0c\u4f60\u53ea\u9700\u6253\u5f00\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\uff0c\u5bfc\u822a\u5230\u811a\u672c\u6240\u5728\u7684\u76ee\u5f55\uff0c\u7136\u540e\u4f7f\u7528<code>python script_name.py<\/code>\u547d\u4ee4\uff08\u5c06<code>script_name.py<\/code>\u66ff\u6362\u4e3a\u4f60\u7684\u811a\u672c\u6587\u4ef6\u540d\uff09\u6765\u6267\u884c\u3002\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u5df2\u6b63\u786e\u914d\u7f6e\uff0c\u5e76\u4e14\u811a\u672c\u4e2d\u5305\u542b\u4e86\u542f\u52a8\u5165\u53e3\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6e\u591a\u4e2a\u542f\u52a8\u5165\u53e3\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u9700\u8981\u5728\u540c\u4e00\u4e2a\u6587\u4ef6\u4e2d\u8bbe\u7f6e\u591a\u4e2a\u542f\u52a8\u5165\u53e3\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e0d\u540c\u7684\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u5728<code>if __name__ == &quot;__main__&quot;:<\/code>\u4e0b\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u6765\u9009\u62e9\u6267\u884c\u54ea\u4e2a\u51fd\u6570\uff0c\u8fd9\u6837\u53ef\u4ee5\u589e\u52a0\u811a\u672c\u7684\u7075\u6d3b\u6027\u548c\u529f\u80fd\u6027\u3002\u53ef\u4ee5\u4f7f\u7528<code>argparse<\/code>\u6a21\u5757\u6765\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u4f7f\u5f97\u4f60\u7684\u811a\u672c\u66f4\u52a0\u7528\u6237\u53cb\u597d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8c03\u53d6\u542f\u52a8\u5165\u53e3\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u4e3b\u51fd\u6570\u3001\u4f7f\u7528if __name__ == &quot;__mAIn [&hellip;]","protected":false},"author":3,"featured_media":955376,"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\/955365"}],"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=955365"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/955365\/revisions"}],"predecessor-version":[{"id":955379,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/955365\/revisions\/955379"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/955376"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=955365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=955365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=955365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}