{"id":1115763,"date":"2025-01-08T18:09:51","date_gmt":"2025-01-08T10:09:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1115763.html"},"modified":"2025-01-08T18:09:53","modified_gmt":"2025-01-08T10:09:53","slug":"python%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5%e4%b8%80%e4%b8%aa%e8%84%9a%e6%9c%ac%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1115763.html","title":{"rendered":"python\u5982\u4f55\u5bfc\u5165\u4e00\u4e2a\u811a\u672c\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080359\/913ba2e3-4ed9-4a3b-97e7-8c6860d13f36.webp\" alt=\"python\u5982\u4f55\u5bfc\u5165\u4e00\u4e2a\u811a\u672c\u6587\u4ef6\" \/><\/p>\n<p><p> \u8981\u5bfc\u5165\u4e00\u4e2aPython\u811a\u672c\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5982<strong>\u4f7f\u7528import\u8bed\u53e5\u3001\u4f7f\u7528from&#8230;import\u8bed\u53e5\u3001\u4f7f\u7528importlib\u6a21\u5757<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528import\u8bed\u53e5\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><p><strong>import\u8bed\u53e5<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528import\u8bed\u53e5\u662f\u6700\u7b80\u5355\u3001\u6700\u5e38\u7528\u7684\u5bfc\u5165Python\u811a\u672c\u6587\u4ef6\u7684\u65b9\u6cd5\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a<code>my_script.py<\/code>\u7684\u811a\u672c\u6587\u4ef6\uff0c\u91cc\u9762\u6709\u4e00\u4e2a\u51fd\u6570<code>my_function<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5bfc\u5165\u8fd9\u4e2a\u811a\u672c\u6587\u4ef6\u5e76\u8c03\u7528\u51fd\u6570\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>my_script.py<\/code>\u7684\u811a\u672c\u6587\u4ef6\uff0c\u5e76\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># my_script.py<\/p>\n<p>def my_function():<\/p>\n<p>    print(&quot;Hello from my_script!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5728\u53e6\u4e00\u4e2aPython\u811a\u672c\u6587\u4ef6\u4e2d\u5bfc\u5165<code>my_script.py<\/code>\u5e76\u8c03\u7528<code>my_function<\/code>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/p>\n<p>import my_script<\/p>\n<p>my_script.my_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5f53\u4f60\u8fd0\u884c<code>main.py<\/code>\u65f6\uff0c\u5b83\u5c06\u8f93\u51fa<code>Hello from my_script!<\/code>\u3002<\/p>\n<\/p>\n<p><p><strong>from&#8230;import\u8bed\u53e5<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528from&#8230;import\u8bed\u53e5\u53ef\u4ee5\u76f4\u63a5\u5bfc\u5165\u7279\u5b9a\u7684\u51fd\u6570\u3001\u7c7b\u6216\u53d8\u91cf\u3002\u5047\u8bbe\u6211\u4eec\u8fd8\u662f\u4f7f\u7528\u4e0a\u9762\u7684<code>my_script.py<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u5728<code>main.py<\/code>\u4e2d\u76f4\u63a5\u5bfc\u5165<code>my_function<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from my_script import my_function<\/p>\n<p>my_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528<code>my_function<\/code>\uff0c\u800c\u4e0d\u9700\u8981\u901a\u8fc7\u6a21\u5757\u540d\u8fdb\u884c\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><p><strong>importlib\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u5bfc\u5165\u6a21\u5757\uff0c\u8fd9\u65f6\u5019\u53ef\u4ee5\u4f7f\u7528<code>importlib<\/code>\u6a21\u5757\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u6839\u636e\u5b57\u7b26\u4e32\u540d\u79f0\u5bfc\u5165\u6a21\u5757\u7684\u60c5\u51b5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import importlib<\/p>\n<p>module_name = &quot;my_script&quot;<\/p>\n<p>my_script = importlib.import_module(module_name)<\/p>\n<p>my_script.my_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u6269\u5c55\u8bf4\u660e\uff1a\u6a21\u5757\u641c\u7d22\u8def\u5f84<\/strong><\/p>\n<\/p>\n<p><p>\u5f53\u4f60\u5bfc\u5165\u4e00\u4e2a\u6a21\u5757\u65f6\uff0cPython\u4f1a\u5728\u4e00\u7cfb\u5217\u76ee\u5f55\u4e2d\u641c\u7d22\u8fd9\u4e2a\u6a21\u5757\u3002\u8fd9\u4e9b\u76ee\u5f55\u5305\u62ec\u5f53\u524d\u76ee\u5f55\u3001\u6807\u51c6\u5e93\u76ee\u5f55\u4ee5\u53ca<code>sys.path<\/code>\u4e2d\u5217\u51fa\u7684\u76ee\u5f55\u3002\u5982\u679c\u4f60\u8981\u5bfc\u5165\u7684\u811a\u672c\u6587\u4ef6\u4e0d\u5728\u8fd9\u4e9b\u76ee\u5f55\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539<code>sys.path<\/code>\u6765\u6dfb\u52a0\u65b0\u7684\u641c\u7d22\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import sys<\/p>\n<p>sys.path.append(&#39;\/path\/to\/your\/module&#39;)<\/p>\n<p>import my_script<\/p>\n<p>my_script.my_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0cPython\u5c31\u4f1a\u5728<code>\/path\/to\/your\/module<\/code>\u76ee\u5f55\u4e2d\u641c\u7d22<code>my_script.py<\/code>\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>\u5c0f\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u5bfc\u5165Python\u811a\u672c\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u6700\u5e38\u7528\u7684\u662f\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u548c<code>from...import<\/code>\u8bed\u53e5\u3002\u6b64\u5916\uff0c<code>importlib<\/code>\u6a21\u5757\u53ef\u4ee5\u7528\u4e8e\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u3002\u901a\u8fc7\u4fee\u6539<code>sys.path<\/code>\uff0c\u4f60\u8fd8\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6a21\u5757\u7684\u641c\u7d22\u8def\u5f84\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u7ec4\u7ec7\u548c\u7ba1\u7406\u4f60\u7684Python\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a<code>my_script.py<\/code>\u7684\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5728\u53e6\u4e00\u4e2aPython\u6587\u4ef6\u4e2d\u4f7f\u7528<code>import my_script<\/code>\u6765\u5bfc\u5165\u5b83\u3002\u786e\u4fdd<code>my_script.py<\/code>\u6587\u4ef6\u4e0e\u8981\u5bfc\u5165\u7684\u811a\u672c\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\uff0c\u6216\u8005\u5728Python\u7684\u641c\u7d22\u8def\u5f84\u4e2d\u3002<\/p>\n<p><strong>\u5bfc\u5165\u811a\u672c\u540e\u5982\u4f55\u8c03\u7528\u5176\u4e2d\u7684\u51fd\u6570\u6216\u53d8\u91cf\uff1f<\/strong><br \/>\u5bfc\u5165\u811a\u672c\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u201c\u6a21\u5757\u540d.\u51fd\u6570\u540d\u201d\u6216\u201c\u6a21\u5757\u540d.\u53d8\u91cf\u540d\u201d\u7684\u5f62\u5f0f\u8c03\u7528\u5176\u4e2d\u7684\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u5982\u679c<code>my_script.py<\/code>\u4e2d\u6709\u4e00\u4e2a\u540d\u4e3a<code>my_function<\/code>\u7684\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<code>my_script.my_function()<\/code>\u6765\u8c03\u7528\u5b83\u3002\u540c\u6837\uff0c\u5982\u679c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53d8\u91cf<code>my_variable<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>my_script.my_variable<\/code>\u6765\u8bbf\u95ee\u5b83\u3002<\/p>\n<p><strong>\u5bfc\u5165\u811a\u672c\u65f6\u5982\u4f55\u5904\u7406\u547d\u540d\u51b2\u7a81\uff1f<\/strong><br \/>\u5728\u5bfc\u5165\u591a\u4e2a\u6a21\u5757\u65f6\uff0c\u5982\u679c\u5b83\u4eec\u4e4b\u95f4\u6709\u547d\u540d\u51b2\u7a81\uff0c\u53ef\u4ee5\u4f7f\u7528<code>import as<\/code>\u8bed\u53e5\u6765\u91cd\u547d\u540d\u6a21\u5757\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e24\u4e2a\u6a21\u5757<code>module1<\/code>\u548c<code>module2<\/code>\uff0c\u90fd\u5305\u542b\u540d\u4e3a<code>function<\/code>\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>import module1 as m1<\/code>\u548c<code>import module2 as m2<\/code>\u3002\u8fd9\u6837\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<code>m1.function()<\/code>\u548c<code>m2.function()<\/code>\u6765\u5206\u522b\u8c03\u7528\u5b83\u4eec\uff0c\u907f\u514d\u547d\u540d\u51b2\u7a81\u7684\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5bfc\u5165\u4e00\u4e2aPython\u811a\u672c\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5982\u4f7f\u7528import\u8bed\u53e5\u3001\u4f7f\u7528from&#8230;impor [&hellip;]","protected":false},"author":3,"featured_media":1115771,"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\/1115763"}],"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=1115763"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1115763\/revisions"}],"predecessor-version":[{"id":1115772,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1115763\/revisions\/1115772"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1115771"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1115763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1115763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1115763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}