{"id":1095045,"date":"2025-01-08T14:47:11","date_gmt":"2025-01-08T06:47:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1095045.html"},"modified":"2025-01-08T14:47:13","modified_gmt":"2025-01-08T06:47:13","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e5%bd%93%e4%b8%ad%e8%b0%83%e7%94%a8%e5%85%b6%e4%bb%96-py%e6%96%87%e4%bb%b6-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1095045.html","title":{"rendered":"\u5982\u4f55\u5728python\u5f53\u4e2d\u8c03\u7528\u5176\u4ed6 py\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210903\/d5ad337b-75ee-4803-81ff-05a1ebaa79d8.webp\" alt=\"\u5982\u4f55\u5728python\u5f53\u4e2d\u8c03\u7528\u5176\u4ed6 py\u6587\u4ef6\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u8c03\u7528\u5176\u4ed6py\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c<strong>import\u8bed\u53e5\u3001exec\u51fd\u6570\u3001sys\u6a21\u5757<\/strong>\u662f\u5176\u4e2d\u5e38\u7528\u7684\u65b9\u6cd5\u3002<strong>import\u8bed\u53e5<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u5c06\u5176\u4ed6py\u6587\u4ef6\u4f5c\u4e3a\u6a21\u5757\u5bfc\u5165\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u5176\u4e2d\u7684\u51fd\u6570\u548c\u53d8\u91cf\uff0c\u8fd9\u6837\u4e0d\u4ec5\u4ee3\u7801\u7ed3\u6784\u6e05\u6670\uff0c\u8fd8\u80fd\u591f\u63d0\u9ad8\u4ee3\u7801\u7684\u590d\u7528\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001IMPORT\u8bed\u53e5<\/h3>\n<\/p>\n<p><h4>1. \u5bfc\u5165\u6574\u4e2a\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528import\u8bed\u53e5\u53ef\u4ee5\u5c06\u6574\u4e2apy\u6587\u4ef6\u5bfc\u5165\uff0c\u8fd9\u6837\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u5176\u4e2d\u7684\u6240\u6709\u51fd\u6570\u548c\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u6709\u4e00\u4e2a\u6587\u4ef6<code>module.py<\/code>\uff0c\u5176\u4e2d\u5305\u542b\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module.py<\/p>\n<p>def greet():<\/p>\n<p>    print(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u53e6\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u4f8b\u5982<code>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/code>\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528import\u8bed\u53e5\u5bfc\u5165<code>module.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import module<\/p>\n<p>module.greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528<code>module.py<\/code>\u4e2d\u7684<code>greet<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>2. \u5bfc\u5165\u7279\u5b9a\u51fd\u6570\u6216\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u53ea\u9700\u8981\u4f7f\u7528<code>module.py<\/code>\u4e2d\u7684\u67d0\u4e9b\u51fd\u6570\u6216\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528from&#8230;import\u8bed\u53e5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from module import greet<\/p>\n<p>greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528\u522b\u540d<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u53ef\u4ee5\u4e3a\u6a21\u5757\u6216\u51fd\u6570\u6307\u5b9a\u522b\u540d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import module as mod<\/p>\n<p>mod.greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001EXEC\u51fd\u6570<\/h3>\n<\/p>\n<p><h4>1. \u6267\u884c\u6574\u4e2a\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528exec\u51fd\u6570\u53ef\u4ee5\u52a8\u6001\u5730\u6267\u884c\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684Python\u4ee3\u7801\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>exec(open(&#39;module.py&#39;).read())<\/p>\n<p>greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u76f4\u63a5\u6267\u884c<code>module.py<\/code>\u4e2d\u7684\u4ee3\u7801\uff0c\u5e76\u8c03\u7528\u5176\u4e2d\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>2. \u6267\u884c\u7279\u5b9a\u4ee3\u7801\u6bb5<\/h4>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u4f7f\u7528exec\u51fd\u6570\u6267\u884c\u7279\u5b9a\u7684\u4ee3\u7801\u6bb5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>code = &quot;&quot;&quot;<\/p>\n<p>def greet():<\/p>\n<p>    print(&quot;Hello, World!&quot;)<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>exec(code)<\/p>\n<p>greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001SYS\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1. \u4fee\u6539sys.path<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u9700\u8981\u52a8\u6001\u5730\u5bfc\u5165\u6a21\u5757\uff0c\u53ef\u4ee5\u901a\u8fc7sys\u6a21\u5757\u4fee\u6539\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\/directory&#39;)<\/p>\n<p>import module<\/p>\n<p>module.greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u5c06\u5305\u542b<code>module.py<\/code>\u7684\u76ee\u5f55\u6dfb\u52a0\u5230sys.path\u4e2d\uff0c\u4f7f\u5f97Python\u80fd\u591f\u627e\u5230\u5e76\u5bfc\u5165\u8be5\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528importlib<\/h4>\n<\/p>\n<p><p>Python\u7684importlib\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u5bfc\u5165\u65b9\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import importlib<\/p>\n<p>module = importlib.import_module(&#39;module&#39;)<\/p>\n<p>module.greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6848\u4f8b\u5206\u6790<\/h3>\n<\/p>\n<p><h4>1. \u9879\u76ee\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6709\u4e00\u4e2a\u9879\u76ee\uff0c\u5305\u542b\u4ee5\u4e0b\u6587\u4ef6\u548c\u76ee\u5f55\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>|-- main.py<\/p>\n<p>|-- utils\/<\/p>\n<p>    |-- __init__.py<\/p>\n<p>    |-- helper.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>helper.py<\/code>\u6587\u4ef6\u5305\u542b\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># helper.py<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>def subtract(a, b):<\/p>\n<p>    return a - b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8c03\u7528utils\u4e2d\u7684\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5728<code>main.py<\/code>\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u8c03\u7528<code>helper.py<\/code>\u4e2d\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><h5>\u76f4\u63a5\u5bfc\u5165<\/h5>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from utils.helper import add, subtract<\/p>\n<p>print(add(5, 3))<\/p>\n<p>print(subtract(5, 3))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>\u4f7f\u7528importlib<\/h5>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import importlib<\/p>\n<p>helper = importlib.import_module(&#39;utils.helper&#39;)<\/p>\n<p>print(helper.add(5, 3))<\/p>\n<p>print(helper.subtract(5, 3))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8c03\u7528\u5176\u4ed6py\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c<strong>import\u8bed\u53e5<\/strong>\u662f\u6700\u5e38\u7528\u4e14\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u7b80\u5355\u6613\u7528\uff0c\u8fd8\u80fd\u4fdd\u8bc1\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ed3\u6784\u6027\u3002<strong>exec\u51fd\u6570<\/strong>\u9002\u7528\u4e8e\u52a8\u6001\u6267\u884c\u4ee3\u7801\uff0c\u4f46\u5e94\u8c28\u614e\u4f7f\u7528\u4ee5\u907f\u514d\u5b89\u5168\u95ee\u9898\u3002<strong>sys\u6a21\u5757<\/strong>\u548c<strong>importlib<\/strong>\u63d0\u4f9b\u4e86\u66f4\u591a\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7ec4\u7ec7\u548c\u7ba1\u7406Python\u9879\u76ee\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u590d\u7528\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5bfc\u5165\u5176\u4ed6.py\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u6216\u7c7b\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u6765\u5bfc\u5165\u5176\u4ed6.py\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u6216\u7c7b\u3002\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u540d\u4e3a<code>module.py<\/code>\u7684\u6587\u4ef6\uff0c\u60a8\u53ef\u4ee5\u5728\u4e3b\u6587\u4ef6\u4e2d\u8fd9\u6837\u5bfc\u5165\uff1a  <\/p>\n<pre><code class=\"language-python\">from module import function_name\n<\/code><\/pre>\n<p>\u6216\u8005\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\uff1a  <\/p>\n<pre><code class=\"language-python\">import module\n<\/code><\/pre>\n<p>\u8fd9\u6837\u60a8\u5c31\u53ef\u4ee5\u4f7f\u7528<code>module.function_name()<\/code>\u6765\u8c03\u7528\u8be5\u51fd\u6570\u3002<\/p>\n<p><strong>\u5728\u8c03\u7528\u5176\u4ed6.py\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u7ba1\u7406\u6a21\u5757\u7684\u641c\u7d22\u8def\u5f84\uff1f<\/strong><br \/>Python\u4f1a\u6839\u636e\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf<code>PYTHONPATH<\/code>\u548c\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u6765\u67e5\u627e\u6a21\u5757\u3002\u5982\u679c\u60a8\u7684.py\u6587\u4ef6\u4e0d\u5728\u9ed8\u8ba4\u8def\u5f84\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys.path.append()<\/code>\u65b9\u6cd5\u6dfb\u52a0\u8def\u5f84\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import sys\nsys.path.append(&#39;\/path\/to\/your\/module&#39;)\nimport module\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u6210\u529f\u5bfc\u5165\u4f4d\u4e8e\u6307\u5b9a\u8def\u5f84\u7684\u6a21\u5757\u3002<\/p>\n<p><strong>\u5982\u4f55\u907f\u514d\u5728\u5bfc\u5165\u5176\u4ed6.py\u6587\u4ef6\u65f6\u51fa\u73b0\u5faa\u73af\u5bfc\u5165\u7684\u95ee\u9898\uff1f<\/strong><br \/>\u5faa\u73af\u5bfc\u5165\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\uff0c\u901a\u5e38\u53d1\u751f\u5728\u4e24\u4e2a\u6a21\u5757\u76f8\u4e92\u5bfc\u5165\u65f6\u3002\u4e3a\u907f\u514d\u6b64\u95ee\u9898\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u5c06\u5bfc\u5165\u8bed\u53e5\u653e\u5728\u51fd\u6570\u6216\u7c7b\u5185\u90e8\uff0c\u6216\u91cd\u6784\u4ee3\u7801\u4ee5\u51cf\u5c11\u4f9d\u8d56\u5173\u7cfb\u3002\u5982\u679c\u53ef\u80fd\uff0c\u5c06\u5171\u4eab\u7684\u4ee3\u7801\u63d0\u53d6\u5230\u4e00\u4e2a\u65b0\u7684\u6a21\u5757\u4e2d\uff0c\u4ee5\u4fbf\u4e24\u4e2a\u6a21\u5757\u90fd\u53ef\u4ee5\u5bfc\u5165\u8fd9\u4e2a\u65b0\u6a21\u5757\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8c03\u7528\u5176\u4ed6py\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0cimport\u8bed\u53e5\u3001exec\u51fd\u6570\u3001sys\u6a21\u5757\u662f\u5176\u4e2d\u5e38\u7528\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1095050,"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\/1095045"}],"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=1095045"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095045\/revisions"}],"predecessor-version":[{"id":1095051,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095045\/revisions\/1095051"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1095050"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1095045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1095045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1095045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}