{"id":204198,"date":"2025-05-29T09:34:20","date_gmt":"2025-05-29T01:34:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204198\/"},"modified":"2025-05-29T09:34:20","modified_gmt":"2025-05-29T01:34:20","slug":"python-%e5%b0%8f%e8%90%8c%e6%96%b0%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88%e6%88%91%e7%9a%84%e4%bb%a3%e7%a0%81%e6%8f%90%e7%a4%baattributeerror-module-__main__-has-no-attribute-studen","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204198\/","title":{"rendered":"Python \u5c0f\u840c\u65b0\uff1a\u4e3a\u4ec0\u4e48\u6211\u7684\u4ee3\u7801\u63d0\u793a\u201cAttributeError: module \\&#8217;__main__\\&#8217; has no attribute \\&#8217;Student\\&#8217;\u201d\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u5c0f\u840c\u65b0\uff1a\u4e3a\u4ec0\u4e48\u6211\u7684\u4ee3\u7801\u63d0\u793a\u201cAttributeError: module \\&#8217;__main__\\&#8217; has no attribute \\&#8217;Student\\&#8217;\u201d\uff1f<\/h1>\n<p>\u4f60\u5728\u5b66\u4e60\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPython \u5c0f\u840c\u65b0\uff1a\u4e3a\u4ec0\u4e48\u6211\u7684\u4ee3\u7801\u63d0\u793a\u201cAttributeError: module \\&#8217;__main__\\&#8217; has no attribute \\&#8217;Student\\&#8217;\u201d\uff1f\u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241031\/173037927367237e09bf92d.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python \u5c0f\u840c\u65b0\u906d\u9047 attributeerror \u9519\u8bef<\/strong><\/p>\n<p><strong>\u95ee\u9898\u80cc\u666f<\/strong><\/p>\n<p>\u4e00\u4f4d\u65b0\u624b\u5728\u7f16\u5199 python \u4ee3\u7801\u65f6\uff0c\u5c1d\u8bd5\u83b7\u53d6\u81ea\u5df1\u5b9a\u4e49\u7684\u6a21\u5757\u4e2d\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u4f46\u9047\u5230 attributeerror \u9519\u8bef\u3002<\/p>\n<p><strong>\u9519\u8bef\u8be6\u60c5<\/strong><\/p>\n<pre>attributeerror: module '__main__' has no attribute 'student'<\/pre>\n<p><strong>\u4ee3\u7801\u793a\u4f8b<\/strong><\/p>\n<pre>class student:\n    def __init__(self, name):\n        self.name = name\n\n\nclass manager:\n    def __init__(self, name):\n        self.name = name\n\n\ndef main():\n    # ...\n    if cls == 'student':\n        obj = student.cls(ret['username'])  # \u6839\u636e\u7c7b\u540d\u5b9e\u4f8b\u5316\u5bf9\u8c61\n    else:\n        obj = manager.cls(ret['username'])<\/pre>\n<p><strong>\u9519\u8bef\u5206\u6790<\/strong><\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u95ee\u9898\u51fa\u73b0\u5728\u7b2c 12 \u884c\u548c 14 \u884c\uff0c\u5373\u5728\u5b9e\u4f8b\u5316 student \u548c manager \u5bf9\u8c61\u65f6\u3002\u5728 python \u4e2d\uff0c\u7c7b\u5c5e\u6027\u548c\u65b9\u6cd5\u53ea\u80fd\u901a\u8fc7\u7c7b\u540d\u76f4\u63a5\u8bbf\u95ee\u3002\u4f46\u5728\u672c\u793a\u4f8b\u4e2d\uff0c\u5374\u4f7f\u7528 student.cls \u548c manager.cls \u6765\u8bbf\u95ee\u5b83\u4eec\u3002<\/p>\n<p>\u5728 python \u4e2d\uff0c__main__ \u6a21\u5757\u8868\u793a\u5f53\u524d\u6b63\u5728\u8fd0\u884c\u7684\u811a\u672c\uff0c\u5176\u5c5e\u6027\u548c\u65b9\u6cd5\u53ea\u80fd\u901a\u8fc7 __main__ \u6a21\u5757\u76f4\u63a5\u8bbf\u95ee\u3002\u56e0\u6b64\uff0c\u5728 __main__ \u6a21\u5757\u5916\uff0c\u65e0\u6cd5\u901a\u8fc7 student.cls \u6216 manager.cls \u8bbf\u95ee\u7c7b\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u4f1a\u5bfc\u81f4 attributeerror \u9519\u8bef\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u6b63\u786e\u7684\u5e94\u8be5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u4f8b\u5316\u5bf9\u8c61\uff1a<\/p>\n<pre>if cls == 'Student':\n    obj = Student(ret['username'])\nelse:\n    obj = Manager(ret['username'])<\/pre>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aPython \u5c0f\u840c\u65b0\uff1a\u4e3a\u4ec0\u4e48\u6211\u7684\u4ee3\u7801\u63d0\u793a\u201cAttributeError: module \\&#8217;__main__\\&#8217; has no attribute \\&#8217;Student\\&#8217;\u201d\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u5c0f\u840c\u65b0\uff1a\u4e3a\u4ec0\u4e48\u6211\u7684&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204198","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204198","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204198"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204198\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}