{"id":50782,"date":"2024-12-03T15:33:07","date_gmt":"2024-12-03T07:33:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/50782\/"},"modified":"2024-12-03T15:33:07","modified_gmt":"2024-12-03T07:33:07","slug":"mastering-pythons-__name__-and-__main__-understanding-script-execution-and-module-imports","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/50782\/","title":{"rendered":"Mastering Python\u2019s __name__ and __main__: Understanding Script Execution and Module Imports"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Mastering Python\u2019s __name__ and __main__: Understanding Script Execution and Module Imports<\/h1>\n<p>\u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662f\u6587\u7ae0\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300aMastering Python\u2019s __name__ and __main__: Understanding Script Execution and Module Imports\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241127\/17327120906747169a652c2.jpg\" class=\"aligncenter\" title=\"Mastering Python\u2019s __name__ and __main__: Understanding Script Execution and Module Imports\u63d2\u56fe\" alt=\"Mastering Python\u2019s __name__ and __main__: Understanding Script Execution and Module Imports\u63d2\u56fe\" \/><\/p>\n<p>python \u7684 <strong>name<\/strong> \u548c <strong>main<\/strong> \u662f\u4e24\u4e2a\u7ecf\u5e38\u88ab\u8bef\u89e3\u7684\u6982\u5ff5\uff0c\u5b83\u4eec\u5728\u811a\u672c\u7684\u6267\u884c\u65b9\u5f0f\u548c\u6a21\u5757\u7684\u5bfc\u5165\u65b9\u5f0f\u4e2d\u53d1\u6325\u7740\u81f3\u5173\u91cd\u8981\u7684\u4f5c\u7528\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u89e3\u91ca <strong>name<\/strong> \u548c <strong>main<\/strong> \u7684\u5b9e\u9645\u542b\u4e49\u3001\u5b83\u4eec\u4e3a\u4f55\u5982\u6b64\u91cd\u8981\uff0c\u4ee5\u53ca\u5982\u4f55\u5229\u7528\u5b83\u4eec\u6765\u7f16\u5199\u66f4\u52a0\u6a21\u5757\u5316\u3001\u53ef\u6d4b\u8bd5\u548c\u53ef\u91cd\u7528\u7684\u4ee3\u7801\u3002\u6700\u540e\uff0c\u60a8\u5c06\u719f\u6089\u8fd9\u4e2a\u57fa\u672c\u6982\u5ff5\uff0c\u5e76\u51c6\u5907\u597d\u4f7f\u7528\u5b83\u6765\u6709\u6548\u5730\u6784\u5efa\u60a8\u81ea\u5df1\u7684\u9879\u76ee\u3002<\/p>\n<p>\u7ec8\u6781 python \u7a0b\u5e8f\u5458\u5b9e\u8df5\u6d4b\u8bd5<\/p>\n<p>\u5728 python \u4e2d\uff0c\u6bcf\u4e2a\u6a21\u5757\uff08python \u6587\u4ef6\uff09\u90fd\u6709\u4e00\u4e2a\u540d\u4e3a <strong>name<\/strong> \u7684\u5185\u7f6e\u5c5e\u6027\u3002\u6b64\u5c5e\u6027\u5e2e\u52a9 python \u786e\u5b9a\u6587\u4ef6\u7684\u4f7f\u7528\u65b9\u5f0f\uff1a\u4f5c\u4e3a\u72ec\u7acb\u811a\u672c\u6216\u4f5c\u4e3a\u5bfc\u5165\u6a21\u5757\u3002<\/p>\n<ul>\n<li>\n<p>\u76f4\u63a5\u8fd0\u884c python \u6587\u4ef6\uff08\u5982 python my_script.py\uff09\u65f6\uff0c<strong>name<\/strong> \u53d8\u91cf\u4f1a\u81ea\u52a8\u8bbe\u7f6e\u4e3a <strong>main<\/strong>.<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u6587\u4ef6\u5728\u53e6\u4e00\u4e2a\u811a\u672c\u4e2d\u4f5c\u4e3a\u6a21\u5757\u5bfc\u5165\uff0c\u5219 <strong>name<\/strong> \u8bbe\u7f6e\u4e3a\u6a21\u5757\u7684\u540d\u79f0\uff08\u4e0d\u5e26 .py \u6269\u5c55\u540d\u7684\u6587\u4ef6\u540d\uff09\u3002<br \/> \u8fd9\u79cd\u533a\u522b\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u60a8\u63a7\u5236\u6587\u4ef6\u5bfc\u5165\u65f6\u548c\u76f4\u63a5\u6267\u884c\u65f6\u5e94\u8fd0\u884c\u6587\u4ef6\u7684\u54ea\u4e9b\u90e8\u5206\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u4e3a\u4e86\u5229\u7528\u6b64\u529f\u80fd\uff0cpython \u5f00\u53d1\u4eba\u5458\u901a\u5e38\u4f7f\u7528 if \u8bed\u53e5\u6765\u68c0\u67e5 <strong>name<\/strong> \u7684\u503c\u3002\u8fd9\u4f7f\u60a8\u53ef\u4ee5\u63a7\u5236\u4ee3\u7801\u7684\u54ea\u4e9b\u90e8\u5206\u4ec5\u5728\u6587\u4ef6\u4f5c\u4e3a\u72ec\u7acb\u811a\u672c\u8fd0\u884c\u65f6\u624d\u6267\u884c\u3002<\/p>\n<p>\u8fd9\u662f\u57fa\u672c\u7ed3\u6784\uff1a<\/p>\n<pre>def main():\n    print(\"running as a standalone script\")\n\nif __name__ == \"__main__\":\n    main()\n<\/pre>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u5982\u679c\u8fd0\u884c python my_script.py\uff0c\u5b83\u5c06\u6253\u5370\u201c\u4f5c\u4e3a\u72ec\u7acb\u811a\u672c\u8fd0\u884c\u201d\uff0c\u56e0\u4e3a <strong>name<\/strong> \u8bbe\u7f6e\u4e3a <strong>main<\/strong>\u3002\u4f46\u662f\uff0c\u5982\u679c\u60a8\u5c06\u6b64\u6587\u4ef6\u4f5c\u4e3a\u6a21\u5757\u5bfc\u5165\uff08\u4f8b\u5982\u5bfc\u5165 my_script\uff09\uff0c\u5219\u4e0d\u4f1a\u6253\u5370\u4efb\u4f55\u5185\u5bb9\uff0c\u56e0\u4e3a\u4e0d\u6ee1\u8db3 if \u6761\u4ef6\u3002<\/p>\n<p>if <strong>name<\/strong> == &#8220;<strong>main<\/strong>&#8221; \u6a21\u5f0f\u6709\u51e0\u4e2a\u4e3b\u8981\u4f18\u70b9\uff1a<\/p>\n<ol>\n<li>\u7ec4\u7ec7\u4ee3\u7801\u4ee5\u5b9e\u73b0\u53ef\u91cd\u7528\u6027\uff1a\u60a8\u53ef\u4ee5\u5c06\u51fd\u6570\u3001\u7c7b\u6216\u5e38\u91cf\u653e\u5165\u6a21\u5757\u4e2d\uff0c\u5e76\u5728\u5176\u4ed6\u811a\u672c\u4e2d\u4f7f\u7528\u5b83\u4eec\uff0c\u800c\u65e0\u9700\u8fd0\u884c\u4e0d\u5fc5\u8981\u7684\u4ee3\u7801\u3002<\/li>\n<li>\u652f\u6301\u5355\u5143\u6d4b\u8bd5\uff1a\u65e0\u9700\u6267\u884c\u4e0d\u76f8\u5173\u7684\u4ee3\u7801\u5373\u53ef\u66f4\u8f7b\u677e\u5730\u6d4b\u8bd5\u6a21\u5757\u3002<\/li>\n<li>\u66f4\u597d\u7684\u4ee3\u7801\u7ed3\u6784\uff1a\u5c06\u6267\u884c\u4ee3\u7801\u4e0e\u5b9a\u4e49\uff08\u51fd\u6570\u3001\u7c7b\uff09\u5206\u5f00\uff0c\u63d0\u9ad8\u4e86\u53ef\u8bfb\u6027\u548c\u6a21\u5757\u5316\u6027\u3002<\/li>\n<\/ol>\n<ol>\n<li>\n<p>\u4f7f\u7528\u53ef\u91cd\u7528\u4ee3\u7801\u7f16\u5199\u72ec\u7acb\u811a\u672c<br \/> \u5982\u679c\u60a8\u6b63\u5728\u5f00\u53d1\u4e00\u4e2a\u65e2\u5177\u6709\u72ec\u7acb\u529f\u80fd\u53c8\u5177\u6709\u5bf9\u5176\u4ed6\u811a\u672c\u6709\u7528\u7684\u90e8\u5206\u7684\u5de5\u5177\uff0c\u90a3\u4e48\u8fd9\u79cd\u6a21\u5f0f\u81f3\u5173\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u6570\u636e\u5904\u7406\u811a\u672c\u53ef\u80fd\u5305\u542b\u7528\u4e8e\u6570\u636e\u6e05\u7406\u7684\u53ef\u91cd\u7528\u51fd\u6570\uff0c\u60a8\u4e5f\u5e0c\u671b\u5c06\u5176\u5bfc\u5165\u5230\u53e6\u4e00\u4e2a\u6570\u636e\u5206\u6790\u811a\u672c\u4e2d\u3002<\/p>\n<\/li>\n<li>\n<p>\u6784\u5efa cli\uff08\u547d\u4ee4\u884c\u754c\u9762\uff09\u5de5\u5177<br \/> \u8fd9\u79cd\u6a21\u5f0f\u5728\u6784\u5efa cli \u5de5\u5177\u65f6\u7279\u522b\u6709\u7528\u3002\u901a\u8fc7\u5b9a\u4e49\u4f5c\u4e3a\u5165\u53e3\u70b9\u7684 main() \u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u5e72\u51c0\u3001\u66f4\u7ed3\u6784\u5316\u3002<\/p>\n<\/li>\n<\/ol>\n<pre># cli_tool.py\nimport argparse\n\ndef main():\n    parser = argparse.ArgumentParser(description=\"A CLI tool example\")\n    parser.add_argument(\"name\", type=str, help=\"Your name\")\n    args = parser.parse_args()\n    print(f\"Hello, {args.name}!\")\n\nif __name__ == \"__main__\":\n    main()\n\n<\/pre>\n<p>\u73b0\u5728\u4f60\u53ef\u4ee5\u4f7f\u7528 python cli_tool.py john \u4ece\u547d\u4ee4\u884c\u8fd0\u884c\u8fd9\u4e2a\u5de5\u5177\uff0c\u5b83\u4f1a\u7528\u201chello, john!\u201d\u5411\u7528\u6237\u6253\u62db\u547c\u3002\u5982\u679c\u9700\u8981\uff0c\u8be5\u811a\u672c\u8fd8\u53ef\u4ee5\u4f5c\u4e3a\u5176\u4ed6 python \u7a0b\u5e8f\u4e2d\u7684\u6a21\u5757\u5bfc\u5165\u3002<\/p>\n<p>\u7406\u89e3 <strong>name<\/strong> \u548c <strong>main<\/strong> \u5141\u8bb8\u60a8\u521b\u5efa\u6a21\u5757\u5316\u3001\u53ef\u91cd\u7528\u7684 python \u4ee3\u7801\u3002\u901a\u8fc7\u5728\u811a\u672c\u4e2d\u4f7f\u7528 if <strong>name<\/strong> == &#8220;<strong>main<\/strong>&#8220;\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u811a\u672c\u7684\u8fd0\u884c\u65b9\u5f0f\u6765\u63a7\u5236\u5e94\u6267\u884c\u4ee3\u7801\u7684\u54ea\u4e9b\u90e8\u5206\u3002\u8fd9\u79cd\u6a21\u5f0f\u5bf9\u4e8e\u7f16\u5199\u53ef\u6d4b\u8bd5\u7684\u3001\u6709\u7ec4\u7ec7\u7684\u548c\u901a\u7528\u7684\u4ee3\u7801\u81f3\u5173\u91cd\u8981\uff0c\u65e0\u8bba\u662f\u4e00\u6b21\u6027\u811a\u672c\u8fd8\u662f\u8ba1\u5212\u5728\u9879\u76ee\u4e2d\u91cd\u7528\u7684\u6a21\u5757\u3002<\/p>\n<p>\u7ec8\u6781 python \u7a0b\u5e8f\u5458\u5b9e\u8df5\u6d4b\u8bd5<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aMastering Python\u2019s __name__ and __main__: Understanding Script Execution and Module Imports\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mastering Python\u2019s __name__ and __main__: Understanding Script Execution and Module Imports \u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662f\u6587\u7ae0\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300aMastering Python\u2019s __name__ and __main__: Understanding Script Execution and Module Imports\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 python \u7684 name \u548c main \u662f\u4e24\u4e2a\u7ecf\u5e38\u88ab\u8bef\u89e3\u7684\u6982\u5ff5\uff0c\u5b83\u4eec\u5728\u811a\u672c\u7684\u6267\u884c\u65b9\u5f0f\u548c\u6a21\u5757\u7684\u5bfc\u5165\u65b9\u5f0f\u4e2d\u53d1\u6325\u7740\u81f3\u5173\u91cd\u8981\u7684\u4f5c\u7528\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u89e3\u91ca name \u548c main \u7684\u5b9e\u9645\u542b\u4e49\u3001\u5b83\u4eec\u4e3a\u4f55\u5982\u6b64\u91cd\u8981\uff0c\u4ee5\u53ca\u5982\u4f55\u5229\u7528\u5b83\u4eec\u6765\u7f16\u5199\u66f4\u52a0\u6a21\u5757\u5316\u3001\u53ef\u6d4b\u8bd5\u548c\u53ef\u91cd\u7528\u7684\u4ee3\u7801\u3002\u6700\u540e\uff0c\u60a8\u5c06\u719f\u6089\u8fd9\u4e2a\u57fa\u672c\u6982\u5ff5\uff0c\u5e76\u51c6\u5907\u597d\u4f7f\u7528\u5b83\u6765\u6709\u6548\u5730\u6784\u5efa\u60a8\u81ea\u5df1\u7684\u9879\u76ee\u3002 \u7ec8\u6781 python \u7a0b\u5e8f\u5458\u5b9e\u8df5\u6d4b\u8bd5 \u5728 python \u4e2d\uff0c\u6bcf\u4e2a\u6a21\u5757\uff08python \u6587\u4ef6\uff09\u90fd\u6709\u4e00\u4e2a\u540d\u4e3a name \u7684\u5185\u7f6e\u5c5e\u6027\u3002\u6b64\u5c5e\u6027\u5e2e\u52a9 python \u786e\u5b9a\u6587\u4ef6\u7684\u4f7f\u7528\u65b9\u5f0f\uff1a\u4f5c\u4e3a\u72ec\u7acb\u811a\u672c\u6216\u4f5c\u4e3a\u5bfc\u5165\u6a21\u5757\u3002 \u76f4\u63a5\u8fd0\u884c python \u6587\u4ef6\uff08\u5982 python my_script.py\uff09\u65f6\uff0cname \u53d8\u91cf\u4f1a\u81ea\u52a8\u8bbe\u7f6e\u4e3a main. \u5982\u679c\u6587\u4ef6\u5728\u53e6\u4e00\u4e2a\u811a\u672c\u4e2d\u4f5c\u4e3a\u6a21\u5757\u5bfc\u5165\uff0c\u5219 name \u8bbe\u7f6e\u4e3a\u6a21\u5757\u7684\u540d\u79f0\uff08\u4e0d\u5e26 .py \u6269\u5c55\u540d\u7684\u6587\u4ef6\u540d\uff09\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-50782","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50782","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=50782"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50782\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=50782"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=50782"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=50782"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}