{"id":3184,"date":"2025-05-29T16:16:51","date_gmt":"2025-06-04T08:40:51","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3184"},"modified":"2025-06-04T16:16:51","modified_gmt":"2025-06-04T08:40:51","slug":"%e4%ba%86%e8%a7%a3python%e6%a8%a1%e7%b5%84%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e6%a8%a1%e7%b5%84%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u6a21\u7d44\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, module, Python \u6a21\u7d44\u6559\u5b78, Python \u6700\u65b0\u8a9e\u6cd5\"><\/p>\n<h1>Python \u6a21\u7d44\u7684\u57fa\u672c\u6982\u5ff5\u8207 module() \u51fd\u5f0f\u4ecb\u7d39<\/h1>\n<p>Python \u662f\u4e00\u7a2e\u975e\u5e38\u6d41\u884c\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5b83\u63d0\u4f9b\u4e86\u8a31\u591a\u6709\u7528\u7684\u529f\u80fd\u548c\u7d50\u69cb\u4f86\u5e6b\u52a9\u958b\u767c\u8005\u6709\u6548\u5730\u7ba1\u7406\u7a0b\u5f0f\u78bc\u3002\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f\u6a21\u7d44\uff08module\uff09\uff0c\u9019\u662f\u4e00\u500b\u5c07\u76f8\u95dc\u529f\u80fd\u7d44\u7e54\u5728\u4e00\u8d77\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5f0f\u78bc\u7684\u53ef\u91cd\u7528\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u4ecb\u7d39 Python \u6a21\u7d44\u7684\u57fa\u672c\u6982\u5ff5\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528 2025 \u6700\u65b0\u8a9e\u6cd5\u4f86\u5b9a\u7fa9\u548c\u4f7f\u7528\u6a21\u7d44\u3002<\/p>\n<h2>Python \u6a21\u7d44\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u6a21\u7d44\u662f\u5305\u542b Python \u4ee3\u78bc\u7684\u6a94\u6848\uff0c\u901a\u5e38\u662f\u4ee5 `.py` \u70ba\u526f\u6a94\u540d\u3002\u900f\u904e\u6a21\u7d44\uff0c\u60a8\u53ef\u4ee5\u5c07\u529f\u80fd\u5283\u5206\u70ba\u4e0d\u540c\u7684\u6a94\u6848\uff0c\u65b9\u4fbf\u7ba1\u7406\u548c\u91cd\u8907\u4f7f\u7528\u3002\u9019\u6a23\u7684\u7d50\u69cb\u4e0d\u50c5\u80fd\u63d0\u9ad8\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\uff0c\u9084\u80fd\u8b93\u5718\u968a\u5354\u4f5c\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<h2>module() \u51fd\u5f0f\u7684\u529f\u80fd\u8207\u4f7f\u7528\u65b9\u6cd5<\/h2>\n<p>\u5728 Python \u4e2d\uff0c\u4e26\u6c92\u6709\u540d\u70ba `module()` \u7684\u5167\u5efa\u51fd\u5f0f\u3002\u76f8\u53cd\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 `import` \u8a9e\u53e5\u4f86\u5c0e\u5165\u6a21\u7d44\uff0c\u4e26\u4f7f\u7528 `__import__()` \u51fd\u5f0f\u52d5\u614b\u5c0e\u5165\u6a21\u7d44\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u6b63\u78ba\u5730\u4f7f\u7528\u6a21\u7d44\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5c07 my_other_module \u5c0e\u5165<br \/>\nimport my_other_module<\/p>\n<p># \u5b9a\u7fa9\u4e00\u500b\u65b0\u7684\u6a21\u7d44 my_module<br \/>\ndef my_module():<br \/>\n    print(&#8220;\u9019\u662f my_module \u7684\u529f\u80fd&#8221;)<\/p>\n<p># \u5c07 my_module \u51fd\u5f0f\u6dfb\u52a0\u5230 my_other_module \u4e2d<br \/>\nmy_other_module.my_module = my_module<br \/>\n&#8220;`<\/p>\n<p>\u5728\u6b64\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5c0e\u5165\u4e86\u540d\u70ba `my_other_module` \u7684\u6a21\u7d44\uff0c\u7136\u5f8c\u5b9a\u7fa9\u4e86\u4e00\u500b\u65b0\u7684\u51fd\u5f0f `my_module()`\u3002\u63a5\u8457\uff0c\u6211\u5011\u5c07 `my_module()` \u51fd\u5f0f\u6dfb\u52a0\u5230 `my_other_module` \u6a21\u7d44\u4e2d\uff0c\u4f7f\u5176\u80fd\u5920\u8a2a\u554f\u9019\u500b\u65b0\u529f\u80fd\u3002<\/p>\n<h2>\u5be6\u4f5c\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u5be6\u4f5c\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5275\u5efa\u548c\u4f7f\u7528\u6a21\u7d44\uff1a<\/p>\n<p>1. \u5275\u5efa\u4e00\u500b\u540d\u70ba `math_utils.py` \u7684\u6a94\u6848\uff0c\u5167\u5bb9\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\ndef add(x, y):<br \/>\n    return x + y<\/p>\n<p>def subtract(x, y):<br \/>\n    return x &#8211; y<br \/>\n&#8220;`<\/p>\n<p>2. \u5728\u4e3b\u7a0b\u5f0f\u4e2d\u5c0e\u5165 `math_utils` \u6a21\u7d44\u4e26\u4f7f\u7528\u5176\u529f\u80fd\uff1a<\/p>\n<p>&#8220;`python<br \/>\nimport math_utils<\/p>\n<p>result_add = math_utils.add(5, 3)<br \/>\nresult_subtract = math_utils.subtract(5, 3)<\/p>\n<p>print(f&#8221;\u52a0\u6cd5\u7d50\u679c: {result_add}&#8221;)<br \/>\nprint(f&#8221;\u6e1b\u6cd5\u7d50\u679c: {result_subtract}&#8221;)<br \/>\n&#8220;`<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528\u6a21\u7d44\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u5e7e\u7a2e\u5e38\u898b\u932f\u8aa4\uff1a<\/p>\n<p>&#8211; **ModuleNotFoundError**\uff1a\u78ba\u4fdd\u6a21\u7d44\u6a94\u6848\u540d\u7a31\u6b63\u78ba\u4e14\u5b58\u5728\u65bc\u6b63\u78ba\u7684\u76ee\u9304\u4e2d\u3002<br \/>\n&#8211; **AttributeError**\uff1a\u5728\u4f7f\u7528\u6a21\u7d44\u7684\u5c6c\u6027\u6216\u65b9\u6cd5\u6642\uff0c\u6aa2\u67e5\u662f\u5426\u6b63\u78ba\u5c0e\u5165\u4e26\u4f7f\u7528\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u6a21\u7d44\u4e0d\u50c5\u9650\u65bc\u51fd\u5f0f\u7684\u7d44\u7e54\uff0c\u9084\u53ef\u4ee5\u7528\u4f86\u5b9a\u7fa9\u985e\u5225\u548c\u5e38\u6578\u3002\u5982\u679c\u60a8\u5728\u958b\u767c\u5927\u578b\u61c9\u7528\u7a0b\u5f0f\uff0c\u53ef\u4ee5\u8003\u616e\u5c07\u7a0b\u5f0f\u78bc\u5283\u5206\u70ba\u591a\u500b\u6a21\u7d44\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u7ba1\u7406\u548c\u64f4\u5c55\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u60a8\u4e5f\u53ef\u4ee5\u63a2\u7d22 [\u9019\u7bc7 Python \u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc\/article\/1234567890) \u4f86\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u9032\u968e\u61c9\u7528\u8207\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u6a21\u7d44\u662f Python \u4e2d\u4e00\u500b\u95dc\u9375\u7684\u6982\u5ff5\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u7d44\u7e54\u548c\u91cd\u7528\u7a0b\u5f0f\u78bc\u3002\u900f\u904e\u6b63\u78ba\u7684\u4f7f\u7528\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u5927\u5e45\u63d0\u5347\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\u548c\u7dad\u8b77\u6027\u3002\u4e86\u89e3\u6a21\u7d44\u7684\u57fa\u672c\u6982\u5ff5\u548c\u4f7f\u7528\u65b9\u6cd5\u5f8c\uff0c\u60a8\u5c07\u80fd\u5920\u66f4\u6709\u6548\u5730\u64b0\u5beb Python \u7a0b\u5f0f\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: \u5982\u4f55\u5275\u5efa\u4e00\u500b\u65b0\u7684 Python \u6a21\u7d44\uff1f**<br \/>\nA1: \u60a8\u53ef\u4ee5\u901a\u904e\u5275\u5efa\u4e00\u500b\u65b0\u7684 `.py` \u6a94\u6848\u4e26\u5728\u5176\u4e2d\u5b9a\u7fa9\u51fd\u5f0f\u3001\u985e\u5225\u6216\u8b8a\u6578\u4f86\u5275\u5efa\u4e00\u500b\u65b0\u7684 Python \u6a21\u7d44\u3002<\/p>\n<p>**Q2: \u6a21\u7d44\u4e4b\u9593\u5982\u4f55\u5171\u4eab\u6578\u64da\uff1f**<br \/>\nA2: \u60a8\u53ef\u4ee5\u900f\u904e\u5c0e\u5165\u6a21\u7d44\u4e26\u8abf\u7528\u5176\u51fd\u5f0f\u6216\u8a2a\u554f\u5176\u8b8a\u6578\u4f86\u5be6\u73fe\u6a21\u7d44\u4e4b\u9593\u7684\u6578\u64da\u5171\u4eab\u3002<\/p>\n<p>**Q3: \u5982\u679c\u6211\u5fd8\u8a18\u5c0e\u5165\u6a21\u7d44\u6703\u767c\u751f\u4ec0\u9ebc\uff1f**<br \/>\nA3: \u5982\u679c\u60a8\u5728\u4f7f\u7528\u6a21\u7d44\u4e4b\u524d\u6c92\u6709\u5c0e\u5165\u5b83\uff0cPython \u6703\u5f15\u767c `ModuleNotFoundError`\uff0c\u63d0\u793a\u60a8\u8a72\u6a21\u7d44\u4e0d\u5b58\u5728\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u6a21\u7d44\u662f\u4e00\u7a2e\u53ef\u4ee5\u91cd\u8907\u4f7f\u7528\u7684\u7a0b\u5f0f\u78bc\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5feb\u901f\u5730\u958b\u767c\u7a0b\u5f0f\uff0c\u4e26\u6e1b\u5c11\u91cd\u8907\u7a0b\u5f0f\u78bc\u7684\u6578\u91cf\u3002\u672c\u6587\u5c07\u4ecb\u7d39Python\u6a21\u7d44\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u5011\u4f86\u63d0\u9ad8\u7a0b\u5f0f\u958b\u767c\u7684\u6548\u7387\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3184","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-Pm","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3184","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3184"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3184\/revisions"}],"predecessor-version":[{"id":3185,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3184\/revisions\/3185"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}