{"id":1110777,"date":"2025-01-08T17:22:56","date_gmt":"2025-01-08T09:22:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1110777.html"},"modified":"2025-01-08T17:22:58","modified_gmt":"2025-01-08T09:22:58","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%9c%a8%e6%96%b0%e8%84%9a%e6%9c%ac%e4%b8%ad%e8%b0%83%e7%94%a8%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1110777.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5728\u65b0\u811a\u672c\u4e2d\u8c03\u7528\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073506\/82250046-0b0d-41af-a446-92804ad8e0a6.webp\" alt=\"python\u4e2d\u5982\u4f55\u5728\u65b0\u811a\u672c\u4e2d\u8c03\u7528\u51fd\u6570\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u5728\u65b0\u811a\u672c\u4e2d\u8c03\u7528\u51fd\u6570\u3002<strong>\u9996\u5148\uff0c\u786e\u4fdd\u51fd\u6570\u5b9a\u4e49\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\u3001\u4f7f\u7528import\u8bed\u53e5\u5bfc\u5165\u6a21\u5757\u3001\u4f7f\u7528from &#8230; import &#8230;\u8bed\u53e5\u5bfc\u5165\u7279\u5b9a\u51fd\u6570<\/strong>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u89e3\u91ca\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5c06\u51fd\u6570\u5b9a\u4e49\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u6709\u4e00\u4e2a\u5305\u542b\u51fd\u6570\u5b9a\u4e49\u7684Python\u811a\u672c\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a<code>mymodule.py<\/code>\u7684\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u4ee5\u4e0b\u51fd\u6570\u5b9a\u4e49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># mymodule.py<\/p>\n<p>def my_function():<\/p>\n<p>    print(&quot;Hello from my_function!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570<code>my_function<\/code>\uff0c\u5b83\u53ea\u6253\u5370\u4e00\u6761\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528import\u8bed\u53e5\u5bfc\u5165\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u60f3\u5728\u53e6\u4e00\u4e2a\u811a\u672c\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u4f60\u9700\u8981\u5bfc\u5165\u5305\u542b\u8be5\u51fd\u6570\u5b9a\u4e49\u7684\u6a21\u5757\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a<code>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n_script.py<\/code>\u7684\u65b0\u811a\u672c\u3002\u5728\u8fd9\u4e2a\u811a\u672c\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main_script.py<\/p>\n<p>import mymodule<\/p>\n<p>mymodule.my_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5bfc\u5165\u4e86<code>mymodule<\/code>\u6a21\u5757\uff0c\u5e76\u4f7f\u7528<code>mymodule.my_function()<\/code>\u8c03\u7528\u4e86<code>my_function<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528from &#8230; import &#8230;\u8bed\u53e5\u5bfc\u5165\u7279\u5b9a\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u53ea\u60f3\u5bfc\u5165\u7279\u5b9a\u7684\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u6574\u4e2a\u6a21\u5757\uff0c\u53ef\u4ee5\u4f7f\u7528<code>from ... import ...<\/code>\u8bed\u53e5\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main_script.py<\/p>\n<p>from mymodule import my_function<\/p>\n<p>my_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u53ea\u5bfc\u5165\u4e86<code>my_function<\/code>\u51fd\u6570\uff0c\u5e76\u76f4\u63a5\u8c03\u7528\u5b83\uff0c\u800c\u65e0\u9700\u4f7f\u7528\u6a21\u5757\u540d\u4f5c\u4e3a\u524d\u7f00\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u6a21\u5757\u522b\u540d<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6a21\u5757\u540d\u53ef\u80fd\u4f1a\u6bd4\u8f83\u957f\uff0c\u6216\u8005\u4f60\u5e0c\u671b\u4f7f\u7528\u66f4\u7b80\u77ed\u7684\u522b\u540d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>as<\/code>\u5173\u952e\u5b57\u4e3a\u6a21\u5757\u6307\u5b9a\u522b\u540d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main_script.py<\/p>\n<p>import mymodule as mm<\/p>\n<p>mm.my_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4e3a<code>mymodule<\/code>\u6a21\u5757\u6307\u5b9a\u4e86\u522b\u540d<code>mm<\/code>\uff0c\u5e76\u4f7f\u7528<code>mm.my_function()<\/code>\u8c03\u7528\u4e86\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5728\u5305\u4e2d\u7ec4\u7ec7\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u9879\u76ee\u4e2d\u6709\u591a\u4e2a\u6a21\u5757\uff0c\u53ef\u4ee5\u5c06\u5b83\u4eec\u7ec4\u7ec7\u5728\u4e00\u4e2a\u5305\u4e2d\u3002\u5305\u662f\u4e00\u4e2a\u5305\u542b<code>__init__.py<\/code>\u6587\u4ef6\u7684\u76ee\u5f55\uff0c\u6bcf\u4e2a\u5305\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u6a21\u5757\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u76ee\u5f55\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code>my_package\/<\/p>\n<p>    __init__.py<\/p>\n<p>    mymodule.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u5f0f\u5bfc\u5165\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main_script.py<\/p>\n<p>from my_package import mymodule<\/p>\n<p>mymodule.my_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u5bfc\u5165\u7279\u5b9a\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main_script.py<\/p>\n<p>from my_package.mymodule import my_function<\/p>\n<p>my_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5728\u540c\u4e00\u4e2a\u5305\u4e2d\u7684\u4e0d\u540c\u6a21\u5757\u4e4b\u95f4\u8c03\u7528\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u3002\u5047\u8bbe\u4f60\u7684\u76ee\u5f55\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>my_package\/<\/p>\n<p>    __init__.py<\/p>\n<p>    mymodule.py<\/p>\n<p>    another_module.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>another_module.py<\/code>\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u6765\u8c03\u7528<code>mymodule.py<\/code>\u4e2d\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># another_module.py<\/p>\n<p>from .mymodule import my_function<\/p>\n<p>my_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u52a8\u6001\u5bfc\u5165\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u52a8\u6001\u5bfc\u5165\u6a21\u5757\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7<code>importlib<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main_script.py<\/p>\n<p>import importlib<\/p>\n<p>mymodule = importlib.import_module(&#39;mymodule&#39;)<\/p>\n<p>mymodule.my_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8c03\u7528\u51fd\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u4f60\u7684\u9700\u6c42\u548c\u9879\u76ee\u7ed3\u6784\u3002<strong>\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528import\u8bed\u53e5\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\u3001\u4f7f\u7528from &#8230; import &#8230;\u8bed\u53e5\u5bfc\u5165\u7279\u5b9a\u51fd\u6570\u3001\u4f7f\u7528\u6a21\u5757\u522b\u540d\u3001\u5728\u5305\u4e2d\u7ec4\u7ec7\u6a21\u5757\u3001\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u3001\u52a8\u6001\u5bfc\u5165\u6a21\u5757<\/strong>\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728\u65b0\u811a\u672c\u4e2d\u8f7b\u677e\u8c03\u7528\u51fd\u6570\uff0c\u5e76\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728Python\u4e2d\u8c03\u7528\u51fd\u6570\u3002\u5982\u679c\u4f60\u6709\u4efb\u4f55\u95ee\u9898\u6216\u9700\u8981\u8fdb\u4e00\u6b65\u7684\u89e3\u91ca\uff0c\u8bf7\u968f\u65f6\u63d0\u95ee\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4ece\u4e00\u4e2a\u811a\u672c\u8c03\u7528\u53e6\u4e00\u4e2a\u811a\u672c\u4e2d\u7684\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u6765\u4ece\u4e00\u4e2a\u811a\u672c\u4e2d\u8c03\u7528\u53e6\u4e00\u4e2a\u811a\u672c\u7684\u51fd\u6570\u3002\u786e\u4fdd\u76ee\u6807\u811a\u672c\u7684\u540d\u79f0\u4e0d\u4ee5\u6570\u5b57\u5f00\u5934\uff0c\u5e76\u4e14\u4f4d\u4e8e\u540c\u4e00\u76ee\u5f55\u4e0b\u6216\u8005\u5728Python\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84\u4e2d\u3002\u4f7f\u7528<code>from script_name import function_name<\/code>\u7684\u683c\u5f0f\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u9700\u8981\u7684\u51fd\u6570\u3002<\/p>\n<p><strong>\u5728\u8c03\u7528\u51fd\u6570\u65f6\u662f\u5426\u9700\u8981\u786e\u4fdd\u4f20\u5165\u53c2\u6570\u7684\u6b63\u786e\u6027\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u8c03\u7528\u51fd\u6570\u65f6\u5fc5\u987b\u786e\u4fdd\u4f20\u5165\u7684\u53c2\u6570\u7c7b\u578b\u548c\u6570\u91cf\u4e0e\u5b9a\u4e49\u8be5\u51fd\u6570\u65f6\u6240\u8981\u6c42\u7684\u4e00\u81f4\u3002\u5982\u679c\u4e0d\u5339\u914d\uff0c\u5c06\u4f1a\u5f15\u53d1\u9519\u8bef\uff0c\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u6b63\u5e38\u6267\u884c\u3002\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u53ef\u4ee5\u5e2e\u52a9\u6355\u83b7\u8fd9\u4e9b\u6f5c\u5728\u7684\u9519\u8bef\uff0c\u4f7f\u7a0b\u5e8f\u66f4\u52a0\u5065\u58ee\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u8c03\u7528\u51fd\u6570\u540e\u83b7\u53d6\u8fd4\u56de\u503c\u5e76\u8fdb\u884c\u5904\u7406\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u503c\uff0c\u60a8\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u8fd4\u56de\u503c\u8d4b\u7ed9\u4e00\u4e2a\u53d8\u91cf\u3002\u8c03\u7528\u51fd\u6570\u540e\uff0c\u53ea\u9700\u5c06\u8fd4\u56de\u7ed3\u679c\u5b58\u50a8\u5728\u4e00\u4e2a\u53d8\u91cf\u4e2d\uff0c\u4f8b\u5982<code>result = function_name(arguments)<\/code>\uff0c\u7136\u540e\u53ef\u4ee5\u5bf9<code>result<\/code>\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5904\u7406\uff0c\u6bd4\u5982\u6253\u5370\u3001\u5b58\u50a8\u6216\u4f20\u9012\u5230\u5176\u4ed6\u51fd\u6570\u4e2d\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u5728\u65b0\u811a\u672c\u4e2d\u8c03\u7528\u51fd\u6570\u3002\u9996\u5148\uff0c\u786e\u4fdd\u51fd\u6570\u5b9a\u4e49\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\u3001\u4f7f\u7528import [&hellip;]","protected":false},"author":3,"featured_media":1110783,"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\/1110777"}],"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=1110777"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1110777\/revisions"}],"predecessor-version":[{"id":1110785,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1110777\/revisions\/1110785"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1110783"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1110777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1110777"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1110777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}