{"id":2878,"date":"2025-05-30T17:00:57","date_gmt":"2025-06-04T09:30:57","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2878"},"modified":"2025-06-04T17:00:57","modified_gmt":"2025-06-04T09:30:57","slug":"%e5%be%9e%e6%a8%a1%e5%a1%8a%e6%88%96%e5%8c%85%e4%b8%ad%e5%bc%95%e5%85%a5%e7%89%b9%e5%ae%9a%e5%87%bd%e6%95%b8%e6%88%96%e9%a1%9e%e5%9e%8b%ef%bc%9apython%e7%9a%84from%e8%aa%9e%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e5%be%9e%e6%a8%a1%e5%a1%8a%e6%88%96%e5%8c%85%e4%b8%ad%e5%bc%95%e5%85%a5%e7%89%b9%e5%ae%9a%e5%87%bd%e6%95%b8%e6%88%96%e9%a1%9e%e5%9e%8b%ef%bc%9apython%e7%9a%84from%e8%aa%9e%e5%8f%a5\/","title":{"rendered":"\u63d0\u5347Python\u7de8\u7a0b\u6548\u7387\uff1a\u6df1\u5165\u77ad\u89e3from\u8a9e\u53e5\u7684\u61c9\u7528\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, from, Python\u6559\u5b78, Python\u6a21\u584a\"><\/p>\n<h1>\u63d0\u5347Python\u7de8\u7a0b\u6548\u7387\uff1a\u6df1\u5165\u77ad\u89e3from\u8a9e\u53e5\u7684\u61c9\u7528\u8207\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>\u5728Python\u4e2d\uff0c`from`\u8a9e\u53e5\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u8b93\u6211\u5011\u9ad8\u6548\u5730\u5f9e\u6a21\u584a\u6216\u5305\u4e2d\u5f15\u5165\u7279\u5b9a\u7684\u51fd\u6578\u6216\u985e\u578b\u3002\u900f\u904e\u4f7f\u7528`from`\u8a9e\u53e5\uff0c\u6211\u5011\u53ef\u4ee5\u907f\u514d\u91cd\u8907\u64b0\u5beb\u5b8c\u6574\u7684\u6a21\u584a\u540d\u7a31\uff0c\u63d0\u5347\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\u548c\u7dad\u8b77\u6027\u3002\u672c\u6587\u5c07\u4ecb\u7d39`from`\u8a9e\u53e5\u7684\u57fa\u672c\u7528\u6cd5\u3001\u5be6\u4f5c\u7bc4\u4f8b\u4ee5\u53ca\u6700\u4f73\u5be6\u8e10\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u932f\u8aa4\u6392\u9664\u6280\u5de7\u8207\u5ef6\u4f38\u61c9\u7528\u3002<\/p>\n<p>## \u57fa\u672c\u8a9e\u6cd5<\/p>\n<p>\u4f7f\u7528`from`\u8a9e\u53e5\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfrom module_name import function_name<br \/>\n&#8220;`<\/p>\n<p>\u5176\u4e2d\uff0c`module_name`\u662f\u4f60\u8981\u5f15\u5165\u7684\u6a21\u584a\u540d\u7a31\uff0c`function_name`\u662f\u4f60\u60f3\u8981\u4f7f\u7528\u7684\u51fd\u6578\u540d\u7a31\u3002<\/p>\n<p>## \u5be6\u4f5c\u7bc4\u4f8b<\/p>\n<p>\u5047\u8a2d\u6211\u5011\u60f3\u8981\u4f7f\u7528Python\u4e2d\u7684`math`\u6a21\u584a\u4e2d\u7684`sqrt`\u51fd\u6578\uff0c\u6211\u5011\u53ef\u4ee5\u9019\u6a23\u5beb\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfrom math import sqrt<\/p>\n<p>x = sqrt(4)<br \/>\nprint(x)  # \u8f38\u51fa: 2.0<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528`sqrt`\u51fd\u6578\uff0c\u800c\u4e0d\u9700\u8981\u6bcf\u6b21\u90fd\u5beb\u51fa`math`\u6a21\u584a\u7684\u540d\u7a31\u3002<\/p>\n<p>### \u5f15\u5165\u6574\u500b\u6a21\u584a<\/p>\n<p>\u5982\u679c\u4f60\u5e0c\u671b\u4f7f\u7528\u6a21\u584a\u4e2d\u7684\u6240\u6709\u51fd\u6578\uff0c\u53ef\u4ee5\u9019\u6a23\u5beb\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfrom math import *<\/p>\n<p>x = sqrt(4)<br \/>\ny = sin(x)<br \/>\nprint(y)  # \u8f38\u51fa: 0.9092974268256817<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\uff0c\u4f60\u5c31\u53ef\u4ee5\u65b9\u4fbf\u5730\u8a2a\u554f`math`\u6a21\u584a\u4e2d\u7684\u6240\u6709\u51fd\u6578\uff0c\u800c\u7121\u9700\u91cd\u8907\u6a21\u584a\u540d\u7a31\u3002<\/p>\n<p>## \u6ce8\u610f\u4e8b\u9805\u8207\u6700\u4f73\u5be6\u8e10<\/p>\n<p>1. **\u540d\u7a31\u885d\u7a81**\uff1a\u7576\u4f7f\u7528`from module import *`\u6642\uff0c\u53ef\u80fd\u6703\u767c\u751f\u540d\u7a31\u885d\u7a81\uff0c\u56e0\u70ba\u4e0d\u540c\u6a21\u584a\u53ef\u80fd\u64c1\u6709\u76f8\u540c\u7684\u51fd\u6578\u540d\u7a31\u3002\u5efa\u8b70\u76e1\u91cf\u660e\u78ba\u6307\u5b9a\u9700\u8981\u5f15\u5165\u7684\u51fd\u6578\uff0c\u4ee5\u907f\u514d\u9019\u985e\u554f\u984c\u3002<\/p>\n<p>2. **\u53ef\u8b80\u6027**\uff1a\u96d6\u7136`from`\u8a9e\u53e5\u53ef\u4ee5\u7c21\u5316\u4ee3\u78bc\uff0c\u4f46\u904e\u5ea6\u4f7f\u7528\u53ef\u80fd\u6703\u9020\u6210\u4ee3\u78bc\u4e0d\u6613\u95b1\u8b80\u3002\u9069\u91cf\u4f7f\u7528\uff0c\u4fdd\u6301\u4ee3\u78bc\u6e05\u6670\u3002<\/p>\n<p>3. **\u932f\u8aa4\u6392\u9664**\uff1a\u5982\u679c\u5728\u4f7f\u7528`from`\u8a9e\u53e5\u6642\u51fa\u73fe`ImportError`\uff0c\u8acb\u6aa2\u67e5\u6a21\u584a\u662f\u5426\u6b63\u78ba\u5b89\u88dd\u548c\u53ef\u7528\uff0c\u6216\u78ba\u8a8d\u4f60\u5f15\u5165\u7684\u51fd\u6578\u540d\u7a31\u662f\u5426\u6b63\u78ba\u3002<\/p>\n<p>## \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u5f15\u5165\uff0c`from`\u8a9e\u53e5\u9084\u53ef\u4ee5\u8207\u5176\u4ed6Python\u7279\u6027\u7d50\u5408\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u4f7f\u7528`from`\u5f15\u5165\u591a\u500b\u51fd\u6578\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfrom math import sqrt, sin, cos<\/p>\n<p>x = sqrt(4)<br \/>\ny = sin(x)<br \/>\nz = cos(x)<br \/>\nprint(y, z)  # \u8f38\u51fa: 0.9092974268256817 -0.4161468365471424<br \/>\n&#8220;`<\/p>\n<p>\u9019\u4f7f\u5f97\u4ee3\u78bc\u66f4\u52a0\u7dca\u6e4a\uff0c\u800c\u4e0d\u5fc5\u91cd\u8907\u6a21\u584a\u540d\u7a31\u3002<\/p>\n<p>\u5982\u679c\u60f3\u4e86\u89e3\u66f4\u591a\u95dc\u65bcPython\u6a21\u584a\u7684\u4f7f\u7528\uff0c\u8acb\u53c3\u8003[\u9019\u7bc7\u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc\/article\/5f4b1c8efb5b41001ce487f9)\u4ee5\u7372\u53d6\u66f4\u591a\u6df1\u5165\u7684\u77e5\u8b58\u548c\u6280\u5de7\u3002<\/p>\n<p>&#8212;<\/p>\n<p>## Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: `from`\u8a9e\u53e5\u548c`import`\u6709\u4ec0\u9ebc\u5340\u5225\uff1f**<br \/>\nA1: `from`\u8a9e\u53e5\u7528\u65bc\u5f15\u5165\u6a21\u584a\u4e2d\u7684\u7279\u5b9a\u51fd\u6578\u6216\u985e\u578b\uff0c\u800c`import`\u5247\u662f\u5f15\u5165\u6574\u500b\u6a21\u584a\u3002\u4f7f\u7528`from`\u53ef\u4ee5\u4f7f\u4ee3\u78bc\u66f4\u7c21\u6f54\uff0c\u4f46\u4f7f\u7528`import`\u5247\u53ef\u4ee5\u4fdd\u7559\u6a21\u584a\u7684\u547d\u540d\u7a7a\u9593\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u8655\u7406\u540d\u7a31\u885d\u7a81\u7684\u554f\u984c\uff1f**<br \/>\nA2: \u70ba\u907f\u514d\u540d\u7a31\u885d\u7a81\uff0c\u5efa\u8b70\u660e\u78ba\u6307\u660e\u9700\u8981\u5f15\u5165\u7684\u51fd\u6578\uff0c\u800c\u4e0d\u662f\u4f7f\u7528`from module import *`\u3002\u5982\u679c\u4ecd\u7136\u9700\u8981\u4f7f\u7528\u591a\u500b\u51fd\u6578\uff0c\u53ef\u4ee5\u8003\u616e\u5c07\u6a21\u584a\u5225\u540d\u5316\uff0c\u4f7f\u7528`import module as alias`\u3002<\/p>\n<p>**Q3: \u5728\u4ec0\u9ebc\u60c5\u6cc1\u4e0b\u61c9\u8a72\u4f7f\u7528`from`\u8a9e\u53e5\uff1f**<br \/>\nA3: \u7576\u4f60\u53ea\u9700\u8981\u4f7f\u7528\u6a21\u584a\u4e2d\u7684\u5c11\u6578\u5e7e\u500b\u51fd\u6578\u6642\uff0c\u4f7f\u7528`from`\u8a9e\u53e5\u662f\u6709\u76ca\u7684\u3002\u4f46\u5982\u679c\u4f60\u9700\u8981\u4f7f\u7528\u6a21\u584a\u4e2d\u7684\u591a\u500b\u51fd\u6578\uff0c\u4f7f\u7528`import`\u6703\u66f4\u6e05\u6670\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u7684from\u8a9e\u53e5\u53ef\u4ee5\u5f9e\u6a21\u584a\u6216\u5305\u4e2d\u5f15\u5165\u7279\u5b9a\u7684\u51fd\u6578\u6216\u985e\u578b\uff0c\u4ee5\u6eff\u8db3\u61c9\u7528\u7a0b\u5e8f\u7684\u9700\u6c42\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39Python\u4e2d\u7684from\u8a9e\u53e5\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u793a\u4f8b\u4f86\u8aaa\u660e\u5982\u4f55\u4f7f\u7528\u5b83\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-2878","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-Kq","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2878","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=2878"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2878\/revisions"}],"predecessor-version":[{"id":2879,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2878\/revisions\/2879"}],"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=2878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}