{"id":1186173,"date":"2025-01-15T19:49:38","date_gmt":"2025-01-15T11:49:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1186173.html"},"modified":"2025-01-15T19:49:41","modified_gmt":"2025-01-15T11:49:41","slug":"%e5%a6%82%e4%bd%95%e7%90%86%e8%a7%a3python%e4%b8%ad%e7%9a%84main","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1186173.html","title":{"rendered":"\u5982\u4f55\u7406\u89e3python\u4e2d\u7684main"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25135057\/0e9f4967-d007-44e9-94fe-c44ff84917d5.webp\" alt=\"\u5982\u4f55\u7406\u89e3python\u4e2d\u7684main\" \/><\/p>\n<p><p> \u5728Python\u7f16\u7a0b\u4e2d\uff0c<strong>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n\u51fd\u6570\u7528\u4e8e\u5b9a\u4e49\u7a0b\u5e8f\u7684\u4e3b\u5165\u53e3\u70b9<\/strong>\uff0c<strong>\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6a21\u5757\u5316<\/strong>\uff0c<strong>\u786e\u4fdd\u4ee3\u7801\u5728\u88ab\u5bfc\u5165\u65f6\u4e0d\u4f1a\u88ab\u6267\u884c<\/strong>\u3002Python\u4e2d\u7684<code>main<\/code>\u51fd\u6570\u901a\u5e38\u901a\u8fc7 <code>if __name__ == &quot;__main__&quot;:<\/code> \u8fd9\u4e00\u60ef\u7528\u6cd5\u6765\u5b9e\u73b0\u3002\u8fd9\u4e2a\u7ed3\u6784\u5e2e\u52a9\u7a0b\u5e8f\u5458\u533a\u5206\u4ee3\u7801\u662f\u88ab\u76f4\u63a5\u8fd0\u884c\u8fd8\u662f\u88ab\u4f5c\u4e3a\u6a21\u5757\u5bfc\u5165\u3002<strong>\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6a21\u5757\u5316<\/strong>\u8fd9\u4e00\u70b9\u5c24\u4e3a\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u4f7f\u5f97\u4ee3\u7801\u66f4\u6613\u4e8e\u7ef4\u62a4\u548c\u91cd\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001<code>if __name__ == &quot;__main__&quot;:<\/code> \u7684\u542b\u4e49<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6240\u6709\u6a21\u5757\u90fd\u6709\u4e00\u4e2a\u5185\u7f6e\u7684\u5c5e\u6027 <code>__name__<\/code>\u3002\u5f53\u4e00\u4e2a\u6a21\u5757\u88ab\u76f4\u63a5\u8fd0\u884c\u65f6\uff0c<code>__name__<\/code> \u7684\u503c\u88ab\u8bbe\u7f6e\u4e3a <code>&quot;__main__&quot;<\/code>\uff1b\u800c\u5f53\u6a21\u5757\u88ab\u5bfc\u5165\u65f6\uff0c<code>__name__<\/code> \u7684\u503c\u88ab\u8bbe\u7f6e\u4e3a\u6a21\u5757\u7684\u540d\u79f0\u3002\u56e0\u6b64\uff0c\u901a\u8fc7\u68c0\u67e5 <code>__name__<\/code> \u7684\u503c\uff0c\u53ef\u4ee5\u5224\u65ad\u6a21\u5757\u662f\u88ab\u76f4\u63a5\u8fd0\u884c\u8fd8\u662f\u88ab\u5bfc\u5165\u3002<\/p>\n<\/p>\n<p><pre><code>def main():<\/p>\n<p>    print(&quot;This is the main function&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u53ea\u6709\u5f53\u811a\u672c\u88ab\u76f4\u63a5\u8fd0\u884c\u65f6\uff0c<code>main<\/code> \u51fd\u6570\u624d\u4f1a\u88ab\u8c03\u7528\u3002\u5982\u679c\u8fd9\u4e2a\u811a\u672c\u88ab\u4f5c\u4e3a\u6a21\u5757\u5bfc\u5165\u5230\u5176\u4ed6\u811a\u672c\u4e2d\uff0c<code>main<\/code> \u51fd\u6570\u4e0d\u4f1a\u88ab\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6a21\u5757\u5316<\/h3>\n<\/p>\n<p><p>\u5c06\u4ee3\u7801\u5c01\u88c5\u5728 <code>main<\/code> \u51fd\u6570\u4e2d\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6a21\u5757\u5316\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u6709\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4ee3\u7801\u7ec4\u7ec7\u66f4\u6e05\u6670<\/strong>\uff1a\u5c06\u7a0b\u5e8f\u7684\u4e3b\u8981\u903b\u8f91\u653e\u5728 <code>main<\/code> \u51fd\u6570\u4e2d\uff0c\u4f7f\u5f97\u4ee3\u7801\u7ed3\u6784\u66f4\u52a0\u6e05\u6670\uff0c\u4fbf\u4e8e\u7406\u89e3\u3002<\/li>\n<li><strong>\u63d0\u9ad8\u590d\u7528\u6027<\/strong>\uff1a\u5176\u4ed6\u6a21\u5757\u53ef\u4ee5\u5bfc\u5165\u8fd9\u4e2a\u6587\u4ef6\u800c\u4e0d\u4f1a\u610f\u5916\u6267\u884c\u5176\u4e3b\u903b\u8f91\uff0c\u53ea\u9700\u8c03\u7528\u9700\u8981\u7684\u51fd\u6570\u5373\u53ef\u3002<\/li>\n<li><strong>\u4fbf\u4e8e\u6d4b\u8bd5<\/strong>\uff1a\u4f7f\u7528 <code>if __name__ == &quot;__main__&quot;:<\/code> \u7ed3\u6784\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5728\u4e0d\u6267\u884c\u4e3b\u903b\u8f91\u7684\u60c5\u51b5\u4e0b\u6d4b\u8bd5\u5404\u4e2a\u51fd\u6570\u3002<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u793a\u4f8b\u548c\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u53ef\u4ee5\u5e2e\u52a9\u66f4\u597d\u5730\u7406\u89e3 <code>main<\/code> \u51fd\u6570\u7684\u7528\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code>def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p>def main():<\/p>\n<p>    print(greet(&quot;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\u5f53\u811a\u672c\u88ab\u76f4\u63a5\u8fd0\u884c\u65f6\uff0c<code>main<\/code> \u51fd\u6570\u4f1a\u88ab\u8c03\u7528\uff0c\u8f93\u51fa &quot;Hello, World!&quot;\u3002\u5982\u679c\u8fd9\u4e2a\u811a\u672c\u88ab\u4f5c\u4e3a\u6a21\u5757\u5bfc\u5165\uff0c<code>main<\/code> \u51fd\u6570\u5219\u4e0d\u4f1a\u88ab\u6267\u884c\uff0c\u5176\u4ed6\u6a21\u5757\u53ef\u4ee5\u8c03\u7528 <code>greet<\/code> \u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u590d\u6742\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>\u5728\u66f4\u590d\u6742\u7684\u5e94\u7528\u4e2d\uff0c<code>main<\/code> \u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u521d\u59cb\u5316\u7a0b\u5e8f\u3001\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u3001\u914d\u7f6e\u65e5\u5fd7\u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code>import argparse<\/p>\n<p>import logging<\/p>\n<p>def setup_logging():<\/p>\n<p>    logging.basicConfig(level=logging.INFO)<\/p>\n<p>def parse_args():<\/p>\n<p>    parser = argparse.ArgumentParser(description=&quot;Example script&quot;)<\/p>\n<p>    parser.add_argument(&#39;--name&#39;, type=str, default=&quot;World&quot;, help=&quot;Name to greet&quot;)<\/p>\n<p>    return parser.parse_args()<\/p>\n<p>def greet(name):<\/p>\n<p>    logging.info(f&quot;Greeting {name}&quot;)<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p>def main():<\/p>\n<p>    setup_logging()<\/p>\n<p>    args = parse_args()<\/p>\n<p>    print(greet(args.name))<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5728 <code>main<\/code> \u51fd\u6570\u4e2d\u8fdb\u884c\u66f4\u590d\u6742\u7684\u521d\u59cb\u5316\u548c\u914d\u7f6e\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><h4>1\u3001\u9632\u6b62\u4ee3\u7801\u88ab\u610f\u5916\u6267\u884c<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 <code>if __name__ == &quot;__main__&quot;:<\/code> \u7ed3\u6784\u53ef\u4ee5\u9632\u6b62\u4ee3\u7801\u5728\u88ab\u5bfc\u5165\u65f6\u610f\u5916\u6267\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code># module.py<\/p>\n<p>def some_function():<\/p>\n<p>    print(&quot;This function can be called from other modules.&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    # This code will not be executed when the module is imported<\/p>\n<p>    print(&quot;This code runs only when the module is executed directly.&quot;)<\/p>\n<p>    some_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u76f4\u63a5\u6267\u884c <code>module.py<\/code> \u4f1a\u6253\u5370\u4e24\u884c\u4fe1\u606f\uff0c\u800c\u5bfc\u5165 <code>module.py<\/code> \u5219\u4e0d\u4f1a\u6267\u884c <code>if __name__ == &quot;__main__&quot;:<\/code> \u4e0b\u9762\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u63d0\u9ad8\u4ee3\u7801\u7684\u590d\u7528\u6027<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5c06\u4e3b\u8981\u903b\u8f91\u653e\u5728 <code>main<\/code> \u51fd\u6570\u4e2d\uff0c\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u5728\u5176\u4ed6\u811a\u672c\u4e2d\u590d\u7528\u4ee3\u7801\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\uff0c\u5176\u4ed6\u811a\u672c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u6a21\u5757\u4e2d\u7684\u51fd\u6570\uff0c\u800c\u4e0d\u5fc5\u62c5\u5fc3\u610f\u5916\u6267\u884c\u4e3b\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684<code>main<\/code>\u51fd\u6570\u901a\u8fc7 <code>if __name__ == &quot;__main__&quot;:<\/code> \u7ed3\u6784\u5b9e\u73b0\uff0c\u5176\u4e3b\u8981\u4f5c\u7528\u662f<strong>\u5b9a\u4e49\u7a0b\u5e8f\u7684\u4e3b\u5165\u53e3\u70b9<\/strong>\uff0c<strong>\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6a21\u5757\u5316<\/strong>\uff0c\u4ee5\u53ca<strong>\u786e\u4fdd\u4ee3\u7801\u5728\u88ab\u5bfc\u5165\u65f6\u4e0d\u4f1a\u88ab\u6267\u884c<\/strong>\u3002\u8fd9\u79cd\u7ed3\u6784\u4f7f\u5f97\u4ee3\u7801\u66f4\u6613\u4e8e\u7ef4\u62a4\u548c\u91cd\u7528\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u590d\u6742\u5e94\u7528\u573a\u666f\u65f6\u5c24\u4e3a\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528 Python \u4e2d\u7684 <code>main<\/code> \u51fd\u6570\uff0c\u4ece\u800c\u7f16\u5199\u51fa\u66f4\u52a0\u6e05\u6670\u3001\u6a21\u5757\u5316\u548c\u9ad8\u6548\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4ec0\u4e48\u662fPython\u4e2d\u7684main\u51fd\u6570\uff0c\u5b83\u6709\u4ec0\u4e48\u4f5c\u7528\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0cmain\u5e76\u4e0d\u662f\u4e00\u4e2a\u5185\u7f6e\u7684\u5173\u952e\u5b57\u6216\u51fd\u6570\uff0c\u800c\u662f\u4e00\u4e2a\u7ea6\u5b9a\u4fd7\u6210\u7684\u540d\u79f0\uff0c\u7528\u4e8e\u8868\u793a\u7a0b\u5e8f\u7684\u4e3b\u5165\u53e3\u70b9\u3002\u901a\u8fc7\u5728\u4ee3\u7801\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3amain\u7684\u51fd\u6570\uff0c\u5e76\u5728\u811a\u672c\u7684\u6700\u540e\u901a\u8fc7if <strong>name<\/strong> == &quot;<strong>main<\/strong>&quot;: \u8c03\u7528\u5b83\uff0c\u53ef\u4ee5\u786e\u4fdd\u53ea\u6709\u5f53\u8be5\u811a\u672c\u88ab\u76f4\u63a5\u8fd0\u884c\u65f6\uff0cmain\u51fd\u6570\u4e2d\u7684\u4ee3\u7801\u624d\u4f1a\u88ab\u6267\u884c\u3002\u8fd9\u79cd\u505a\u6cd5\u4f7f\u5f97\u4ee3\u7801\u66f4\u5177\u53ef\u91cd\u7528\u6027\uff0c\u7279\u522b\u662f\u5728\u5c06\u4ee3\u7801\u6a21\u5757\u5316\u65f6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u811a\u672c\u4e2d\u6b63\u786e\u4f7f\u7528main\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u811a\u672c\u4e2d\uff0c\u5efa\u8bae\u5c06\u4e3b\u8981\u6267\u884c\u903b\u8f91\u653e\u5165\u4e00\u4e2a\u540d\u4e3amain\u7684\u51fd\u6570\u4e2d\uff0c\u5e76\u901a\u8fc7__name__\u5c5e\u6027\u6765\u68c0\u67e5\u5f53\u524d\u6a21\u5757\u662f\u5426\u662f\u4e3b\u7a0b\u5e8f\u3002\u8fd9\u6837\uff0c\u5f53\u8be5\u6a21\u5757\u88ab\u5176\u4ed6\u6a21\u5757\u5bfc\u5165\u65f6\uff0cmain\u51fd\u6570\u5185\u7684\u4ee3\u7801\u4e0d\u4f1a\u88ab\u6267\u884c\u3002\u4e00\u4e2a\u5178\u578b\u7684\u7ed3\u6784\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">def main():\n    # \u4e3b\u8981\u4ee3\u7801\u903b\u8f91\n    print(&quot;Hello, World!&quot;)\n\nif __name__ == &quot;__main__&quot;:\n    main()\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u7ed3\u6784\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u4e5f\u4fbf\u4e8e\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u548c\u6a21\u5757\u5316\u5f00\u53d1\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python\u4e2d\u7684main\u51fd\u6570\u53c2\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0cmain\u51fd\u6570\u53ef\u4ee5\u63a5\u6536\u53c2\u6570\uff0c\u901a\u5e38\u662f\u901a\u8fc7\u547d\u4ee4\u884c\u4f20\u5165\u7684\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u53c2\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528sys\u6a21\u5757\u4e2d\u7684argv\u5c5e\u6027\u3002argv\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u4e86\u547d\u4ee4\u884c\u53c2\u6570\u3002\u4f60\u53ef\u4ee5\u5728main\u51fd\u6570\u4e2d\u89e3\u6790\u8fd9\u4e9b\u53c2\u6570\uff0c\u4ee5\u4fbf\u6839\u636e\u7528\u6237\u8f93\u5165\u6267\u884c\u4e0d\u540c\u7684\u903b\u8f91\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import sys\n\ndef main(args):\n    if len(args) &gt; 1:\n        print(f&quot;Arguments provided: {args[1:]}&quot;)\n    else:\n        print(&quot;No arguments provided.&quot;)\n\nif __name__ == &quot;__main__&quot;:\n    main(sys.argv)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u8fd0\u884c\u811a\u672c\u65f6\u4f20\u5165\u81ea\u5b9a\u4e49\u53c2\u6570\uff0c\u7a0b\u5e8f\u4f1a\u6839\u636e\u8f93\u5165\u7684\u53c2\u6570\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u7f16\u7a0b\u4e2d\uff0cmAIn\u51fd\u6570\u7528\u4e8e\u5b9a\u4e49\u7a0b\u5e8f\u7684\u4e3b\u5165\u53e3\u70b9\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6a21\u5757\u5316\uff0c\u786e\u4fdd\u4ee3\u7801\u5728\u88ab\u5bfc\u5165\u65f6\u4e0d\u4f1a\u88ab\u6267 [&hellip;]","protected":false},"author":3,"featured_media":1186185,"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\/1186173"}],"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=1186173"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1186173\/revisions"}],"predecessor-version":[{"id":1186191,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1186173\/revisions\/1186191"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1186185"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1186173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1186173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1186173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}