{"id":994010,"date":"2024-12-27T08:51:21","date_gmt":"2024-12-27T00:51:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/994010.html"},"modified":"2024-12-27T08:51:24","modified_gmt":"2024-12-27T00:51:24","slug":"python-%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e6%ba%90%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/994010.html","title":{"rendered":"python \u5982\u4f55\u4f7f\u7528\u6e90\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071159\/0163a318-c8cb-49d7-baaa-c4661bb15b6b.webp\" alt=\"python \u5982\u4f55\u4f7f\u7528\u6e90\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u4f7f\u7528\u6e90\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\uff1aimport\u8bed\u53e5\u3001exec\u51fd\u6570\u3001runpy\u6a21\u5757\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528import\u8bed\u53e5\u6765\u5bfc\u5165\u6a21\u5757\uff0c\u8fd9\u4e0d\u4ec5\u7b80\u5355\uff0c\u8fd8\u80fd\u6709\u6548\u5730\u7ec4\u7ec7\u4ee3\u7801\uff0c\u5e76\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5982\u4f55\u4f7f\u7528\u8fd9\u4e09\u79cd\u65b9\u6cd5\u6765\u5904\u7406Python\u6e90\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001IMPORT\u8bed\u53e5<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u901a\u8fc7<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u6e90\u6587\u4ef6\u3002<code>import<\/code>\u8bed\u53e5\u53ef\u4ee5\u8ba9\u6211\u4eec\u5728\u4e00\u4e2aPython\u6587\u4ef6\u4e2d\u4f7f\u7528\u53e6\u4e00\u4e2a\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u3001\u7c7b\u548c\u53d8\u91cf\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u7ec4\u7ec7\u6027\u548c\u53ef\u91cd\u7528\u6027\u3002<\/p>\n<\/p>\n<p><p>1.1\u3001\u57fa\u672c\u7528\u6cd5<\/p>\n<\/p>\n<p><p>\u8981\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u786e\u4fdd\u6e90\u6587\u4ef6\u5728Python\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84\u4e2d\u3002\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u5c06\u6e90\u6587\u4ef6\u653e\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u6709\u4e00\u4e2a<code>my_module.py<\/code>\u6587\u4ef6\uff0c\u91cc\u9762\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>greet<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># my_module.py<\/p>\n<p>def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u53e6\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u8fd9\u4e2a\u6a21\u5757\u5e76\u8c03\u7528<code>greet<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\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_module<\/p>\n<p>print(my_module.greet(&quot;Alice&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>1.2\u3001\u4ece\u6a21\u5757\u4e2d\u5bfc\u5165\u7279\u5b9a\u6210\u5458<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u6211\u4eec\u53ea\u60f3\u4ece\u6a21\u5757\u4e2d\u5bfc\u5165\u67d0\u4e2a\u7279\u5b9a\u7684\u51fd\u6570\u6216\u7c7b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>from ... import ...<\/code>\u8bed\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from my_module import greet<\/p>\n<p>print(greet(&quot;Bob&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>1.3\u3001\u5bfc\u5165\u6240\u6709\u6210\u5458<\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u6240\u6709\u6210\u5458\uff0c\u53ef\u4ee5\u4f7f\u7528\u661f\u53f7<code>*<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from my_module import *<\/p>\n<p>print(greet(&quot;Charlie&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u800c\uff0c\u4e0d\u5efa\u8bae\u8fd9\u6837\u505a\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u4f1a\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\uff0c\u4f7f\u4ee3\u7801\u96be\u4ee5\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><p>1.4\u3001\u6a21\u5757\u522b\u540d<\/p>\n<\/p>\n<p><p>\u5728\u5bfc\u5165\u6a21\u5757\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u6a21\u5757\u6307\u5b9a\u4e00\u4e2a\u522b\u540d\uff0c\u8fd9\u5bf9\u4e8e\u6a21\u5757\u540d\u8f83\u957f\u7684\u60c5\u51b5\u975e\u5e38\u6709\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import my_module as mm<\/p>\n<p>print(mm.greet(&quot;David&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001EXEC\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>exec<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u52a8\u6001\u5730\u6267\u884cPython\u4ee3\u7801\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4e32\u53c2\u6570\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3aPython\u4ee3\u7801\u6267\u884c\u3002\u8fd9\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\uff0c\u5c24\u5176\u662f\u5f53\u6211\u4eec\u9700\u8981\u5728\u8fd0\u884c\u65f6\u751f\u6210\u548c\u6267\u884c\u4ee3\u7801\u65f6\u3002<\/p>\n<\/p>\n<p><p>2.1\u3001\u57fa\u672c\u7528\u6cd5<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542bPython\u4ee3\u7801\u7684\u6587\u4ef6<code>code.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># code.py<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>result = add(2, 3)<\/p>\n<p>print(&quot;Result:&quot;, result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5728\u53e6\u4e00\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528<code>exec<\/code>\u51fd\u6570\u6267\u884c<code>code.py<\/code>\u7684\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;code.py&#39;) as file:<\/p>\n<p>    code = file.read()<\/p>\n<p>exec(code)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2.2\u3001\u4f7f\u7528\u547d\u540d\u7a7a\u95f4<\/p>\n<\/p>\n<p><p><code>exec<\/code>\u51fd\u6570\u5141\u8bb8\u6211\u4eec\u5728\u7279\u5b9a\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\u6267\u884c\u4ee3\u7801\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u6c61\u67d3\u5168\u5c40\u547d\u540d\u7a7a\u95f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">namespace = {}<\/p>\n<p>with open(&#39;code.py&#39;) as file:<\/p>\n<p>    exec(file.read(), namespace)<\/p>\n<p>print(&quot;Result from namespace:&quot;, namespace[&#39;result&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001RUNPY\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>runpy<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u8fd0\u884cPython\u6a21\u5757\u7684\u65b9\u5f0f\uff0c\u800c\u4e0d\u5fc5\u5c06\u5b83\u4eec\u5bfc\u5165\u5230\u5f53\u524d\u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u8fd9\u5728\u8fd0\u884c\u811a\u672c\u6216\u6a21\u5757\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>3.1\u3001\u57fa\u672c\u7528\u6cd5<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>runpy<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fd0\u884c\u4e00\u4e2aPython\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import runpy<\/p>\n<p>runpy.run_module(&#39;module_name&#39;, run_name=&#39;__main__&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3.2\u3001\u8fd0\u884cPython\u811a\u672c<\/p>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>runpy.run_path<\/code>\u8fd0\u884cPython\u811a\u672c\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">runpy.run_path(&#39;path\/to\/script.py&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u7ba1\u7406\u548c\u4f7f\u7528\u6e90\u6587\u4ef6\u7684\u65b9\u5f0f\u591a\u79cd\u591a\u6837\u3002<strong>\u901a\u8fc7<code>import<\/code>\u8bed\u53e5\u8fdb\u884c\u6a21\u5757\u7684\u5bfc\u5165\u662f\u6700\u5e38\u7528\u548c\u63a8\u8350\u7684\u65b9\u5f0f<\/strong>\uff0c\u56e0\u4e3a\u5b83\u80fd\u6709\u6548\u5730\u7ba1\u7406\u4ee3\u7801\u7ed3\u6784\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<code>exec<\/code>\u51fd\u6570\u548c<code>runpy<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7075\u6d3b\u6027\uff0c\u9002\u7528\u4e8e\u7279\u5b9a\u7684\u9700\u6c42\u548c\u573a\u666f\u3002\u4e86\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5e94\u7528\u573a\u666f\u548c\u9650\u5236\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7ec4\u7ec7\u548c\u4f7f\u7528Python\u4ee3\u7801\u3002\u65e0\u8bba\u54ea\u79cd\u65b9\u6cd5\uff0c\u5408\u7406\u7684\u6a21\u5757\u5316\u8bbe\u8ba1\u90fd\u662f\u6210\u529f\u5f00\u53d1\u7684\u5173\u952e\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u662f\u6bcf\u4e2a\u5f00\u53d1\u8005\u5e94\u5f53\u5173\u6ce8\u7684\u91cd\u70b9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165\u6e90\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u6765\u5bfc\u5165\u6e90\u6587\u4ef6\u3002\u786e\u4fdd\u6e90\u6587\u4ef6\u4e0e\u5f53\u524d\u811a\u672c\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\uff0c\u6216\u8005\u5c06\u6e90\u6587\u4ef6\u7684\u8def\u5f84\u6dfb\u52a0\u5230<code>sys.path<\/code>\u4e2d\u3002\u8fd9\u6837\uff0c\u4f60\u5c31\u53ef\u4ee5\u901a\u8fc7<code>import your_module_name<\/code>\u7684\u65b9\u5f0f\u6765\u4f7f\u7528\u6e90\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u548c\u7c7b\u3002<\/p>\n<p><strong>\u6e90\u6587\u4ef6\u7684\u7ed3\u6784\u5e94\u8be5\u662f\u4ec0\u4e48\u6837\u7684\uff1f<\/strong><br \/>\u4e00\u4e2a\u6807\u51c6\u7684Python\u6e90\u6587\u4ef6\u901a\u5e38\u5305\u542b\u51fd\u6570\u5b9a\u4e49\u3001\u7c7b\u5b9a\u4e49\u548c\u53d8\u91cf\u7b49\u3002\u6587\u4ef6\u7684\u6269\u5c55\u540d\u5e94\u4e3a<code>.py<\/code>\u3002\u5efa\u8bae\u5728\u6587\u4ef6\u5f00\u5934\u6dfb\u52a0\u6587\u6863\u5b57\u7b26\u4e32\uff08docstring\uff09\uff0c\u4ee5\u8bf4\u660e\u6587\u4ef6\u7684\u529f\u80fd\u3002\u6b64\u5916\uff0c\u5408\u7406\u7684\u6ce8\u91ca\u548c\u4ee3\u7801\u683c\u5f0f\u5c06\u5e2e\u52a9\u4ed6\u4eba\u66f4\u597d\u5730\u7406\u89e3\u6e90\u6587\u4ef6\u7684\u5185\u5bb9\u3002<\/p>\n<p><strong>\u5982\u679c\u6e90\u6587\u4ef6\u4e2d\u6709\u591a\u4e2a\u51fd\u6570\uff0c\u6211\u8be5\u5982\u4f55\u8c03\u7528\u7279\u5b9a\u7684\u51fd\u6570\uff1f<\/strong><br \/>\u5728\u5bfc\u5165\u6e90\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>\u6a21\u5757\u540d.\u51fd\u6570\u540d()<\/code>\u7684\u65b9\u5f0f\u6765\u8c03\u7528\u7279\u5b9a\u7684\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6e90\u6587\u4ef6\u540d\u4e3a<code>my_module.py<\/code>\uff0c\u5176\u4e2d\u6709\u4e00\u4e2a\u540d\u4e3a<code>my_function<\/code>\u7684\u51fd\u6570\uff0c\u5219\u8c03\u7528\u8be5\u51fd\u6570\u7684\u65b9\u5f0f\u4e3a<code>my_module.my_function()<\/code>\u3002\u786e\u4fdd\u4f20\u9012\u5fc5\u8981\u7684\u53c2\u6570\u4ee5\u83b7\u5f97\u6b63\u786e\u7684\u8f93\u51fa\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4f7f\u7528\u6e90\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\uff1aimport\u8bed\u53e5\u3001exec\u51fd\u6570\u3001runpy\u6a21\u5757\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528imp [&hellip;]","protected":false},"author":3,"featured_media":994016,"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\/994010"}],"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=994010"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994010\/revisions"}],"predecessor-version":[{"id":994018,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994010\/revisions\/994018"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/994016"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=994010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=994010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=994010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}