{"id":1049316,"date":"2024-12-31T13:58:26","date_gmt":"2024-12-31T05:58:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1049316.html"},"modified":"2024-12-31T13:58:27","modified_gmt":"2024-12-31T05:58:27","slug":"python%e5%a6%82%e4%bd%95%e5%bc%95%e5%85%a5%e5%85%b6%e4%bb%96%e6%96%87%e4%bb%b6%e4%b8%ad%e7%9a%84%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1049316.html","title":{"rendered":"python\u5982\u4f55\u5f15\u5165\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/093aa3ff-834e-4a32-bb4d-46b0e2b2e4d7.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5f15\u5165\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\" \/><\/p>\n<p><p> <strong>Python\u5f15\u5165\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7import\u8bed\u53e5\u3001from&#8230;import\u8bed\u53e5\u3001sys\u6a21\u5757\u6dfb\u52a0\u8def\u5f84\u3001\u4f7f\u7528\u7edd\u5bf9\u6216\u76f8\u5bf9\u8def\u5f84\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\uff0c\u63a8\u8350\u4f7f\u7528import\u548cfrom&#8230;import\u8bed\u53e5<\/strong>\u3002\u8fd9\u91cc\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u79cd\u65b9\u6cd5\uff0c\u5e76\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5f15\u5165\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001import\u8bed\u53e5\u5f15\u5165\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570<\/p>\n<\/p>\n<p><p>import\u8bed\u53e5\u662fPython\u4e2d\u6700\u5e38\u89c1\u7684\u5f15\u5165\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u7684\u65b9\u6cd5\u3002\u901a\u8fc7import\u8bed\u53e5\uff0c\u53ef\u4ee5\u5f15\u5165\u6574\u4e2a\u6a21\u5757\uff0c\u7136\u540e\u901a\u8fc7\u6a21\u5757\u540d.\u51fd\u6570\u540d\u7684\u65b9\u5f0f\u8c03\u7528\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u6709\u4e00\u4e2a\u6587\u4ef6\u540d\u4e3a<code>module1.py<\/code>\uff0c\u5176\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>hello<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module1.py<\/p>\n<p>def hello():<\/p>\n<p>    print(&quot;Hello from module1&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u73b0\u5728\u6211\u4eec\u5728\u53e6\u4e00\u4e2a\u6587\u4ef6\u4e2d\u5f15\u5165<code>module1<\/code>\u5e76\u8c03\u7528<code>hello<\/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 module1<\/p>\n<p>module1.hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5f15\u5165\u548c\u8c03\u7528\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001from&#8230;import\u8bed\u53e5\u5f15\u5165\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570<\/p>\n<\/p>\n<p><p>from&#8230;import\u8bed\u53e5\u5141\u8bb8\u6211\u4eec\u76f4\u63a5\u5f15\u5165\u6a21\u5757\u4e2d\u7684\u67d0\u4e2a\u51fd\u6570\u6216\u53d8\u91cf\uff0c\u800c\u4e0d\u9700\u8981\u901a\u8fc7\u6a21\u5757\u540d\u6765\u8bbf\u95ee\u3002\u8fd9\u79cd\u65b9\u5f0f\u66f4\u52a0\u7b80\u6d01\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4ecd\u7136\u4f7f\u7528\u4e0a\u9762\u7684<code>module1.py<\/code>\uff0c\u6211\u4eec\u5728<code>main.py<\/code>\u4e2d\u5f15\u5165\u5e76\u8c03\u7528<code>hello<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from module1 import hello<\/p>\n<p>hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u4f46\u5728\u5927\u578b\u9879\u76ee\u4e2d\u53ef\u80fd\u4f1a\u5f15\u5165\u547d\u540d\u51b2\u7a81\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528sys\u6a21\u5757\u6dfb\u52a0\u8def\u5f84<\/p>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u52a8\u6001\u5730\u6dfb\u52a0\u6a21\u5757\u8def\u5f84\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528sys\u6a21\u5757\u3002sys\u6a21\u5757\u5141\u8bb8\u6211\u4eec\u5728\u8fd0\u884c\u65f6\u4fee\u6539Python\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u6709\u4e00\u4e2a\u6587\u4ef6\u5939\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>    main.py<\/p>\n<p>    subdir\/<\/p>\n<p>        module2.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6211\u4eec\u5e0c\u671b\u5728<code>main.py<\/code>\u4e2d\u5f15\u5165<code>subdir\/module2.py<\/code>\u4e2d\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7sys\u6a21\u5757\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import sys<\/p>\n<p>import os<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u5939\u8def\u5f84<\/strong><\/h2>\n<p>current_dir = os.path.dirname(os.path.abspath(__file__))<\/p>\n<h2><strong>\u6dfb\u52a0\u5b50\u76ee\u5f55\u5230sys.path<\/strong><\/h2>\n<p>sys.path.append(os.path.join(current_dir, &#39;subdir&#39;))<\/p>\n<p>import module2<\/p>\n<p>module2.hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u4e0d\u540c\u76ee\u5f55\u4e4b\u95f4\u5f15\u5165\u6a21\u5757\u4e86\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u7edd\u5bf9\u6216\u76f8\u5bf9\u8def\u5f84<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u578b\u9879\u76ee\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528\u5305\u548c\u5b50\u5305\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u7edd\u5bf9\u6216\u76f8\u5bf9\u8def\u5f84\u6765\u5f15\u5165\u6a21\u5757\u3002\u7edd\u5bf9\u8def\u5f84\u4ece\u9879\u76ee\u6839\u76ee\u5f55\u5f00\u59cb\uff0c\u5f15\u5165\u6a21\u5757\u65f6\u663e\u5f97\u66f4\u52a0\u76f4\u89c2\uff1b\u76f8\u5bf9\u8def\u5f84\u5219\u76f8\u5bf9\u4e8e\u5f53\u524d\u6587\u4ef6\u6240\u5728\u76ee\u5f55\u8fdb\u884c\u5f15\u5165\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u6709\u4e00\u4e2a\u6587\u4ef6\u5939\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>    main.py<\/p>\n<p>    package\/<\/p>\n<p>        __init__.py<\/p>\n<p>        module3.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>main.py<\/code>\u4e2d\u53ef\u4ee5\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u5f15\u5165<code>package\/module3.py<\/code>\u4e2d\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from package import module3<\/p>\n<p>module3.hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u5728<code>package\/__init__.py<\/code>\u4e2d\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u5f15\u5165<code>module3<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># package\/__init__.py<\/p>\n<p>from . import module3<\/p>\n<p>module3.hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528importlib\u6a21\u5757\u52a8\u6001\u52a0\u8f7d\u6a21\u5757<\/p>\n<\/p>\n<p><p>importlib\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u7684\u529f\u80fd\u3002\u901a\u8fc7importlib\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6839\u636e\u9700\u8981\u52a0\u8f7d\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u6709\u4e00\u4e2a\u6587\u4ef6\u540d\u4e3a<code>module4.py<\/code>\uff0c\u5176\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>hello<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module4.py<\/p>\n<p>def hello():<\/p>\n<p>    print(&quot;Hello from module4&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5728<code>main.py<\/code>\u4e2d\u4f7f\u7528importlib\u52a8\u6001\u52a0\u8f7d<code>module4<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import importlib<\/p>\n<p>module4 = importlib.import_module(&#39;module4&#39;)<\/p>\n<p>module4.hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u9002\u7528\u4e8e\u9700\u8981\u5728\u8fd0\u884c\u65f6\u6839\u636e\u6761\u4ef6\u52a0\u8f7d\u4e0d\u540c\u6a21\u5757\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5f15\u5165\u5305\u4e2d\u7684\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u5e38\u5e38\u9700\u8981\u5f15\u5165\u5305\u4e2d\u7684\u6a21\u5757\u3002\u5305\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u6587\u4ef6\u5939\uff0c\u5e76\u4e14\u6587\u4ef6\u5939\u4e2d\u5fc5\u987b\u5305\u542b\u4e00\u4e2a<code>__init__.py<\/code>\u6587\u4ef6\u3002\u901a\u8fc7\u5f15\u5165\u5305\uff0c\u6211\u4eec\u53ef\u4ee5\u7ec4\u7ec7\u548c\u7ba1\u7406\u4ee3\u7801\uff0c\u4f7f\u5176\u66f4\u52a0\u7ed3\u6784\u5316\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u6709\u4e00\u4e2a\u6587\u4ef6\u5939\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>    main.py<\/p>\n<p>    mypackage\/<\/p>\n<p>        __init__.py<\/p>\n<p>        submodule.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>submodule.py<\/code>\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570<code>hello<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># mypackage\/submodule.py<\/p>\n<p>def hello():<\/p>\n<p>    print(&quot;Hello from submodule&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>main.py<\/code>\u4e2d\u5f15\u5165<code>submodule<\/code>\u5e76\u8c03\u7528<code>hello<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from mypackage import submodule<\/p>\n<p>submodule.hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u5f15\u5165\u5305\u4e2d\u7684\u6a21\u5757\u6765\u7ec4\u7ec7\u548c\u7ba1\u7406\u4ee3\u7801\u4e86\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u5f15\u5165\u6a21\u5757\u4e2d\u7684\u7c7b\u548c\u53d8\u91cf<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u5f15\u5165\u51fd\u6570\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5f15\u5165\u6a21\u5757\u4e2d\u7684\u7c7b\u548c\u53d8\u91cf\u3002\u65b9\u6cd5\u4e0e\u5f15\u5165\u51fd\u6570\u7c7b\u4f3c\uff0c\u4f7f\u7528import\u8bed\u53e5\u6216from&#8230;import\u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u6709\u4e00\u4e2a\u6587\u4ef6\u540d\u4e3a<code>module5.py<\/code>\uff0c\u5176\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b<code>MyClass<\/code>\u548c\u4e00\u4e2a\u53d8\u91cf<code>my_variable<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module5.py<\/p>\n<p>class MyClass:<\/p>\n<p>    def greet(self):<\/p>\n<p>        print(&quot;Hello from MyClass&quot;)<\/p>\n<p>my_variable = &quot;Hello from my_variable&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>main.py<\/code>\u4e2d\u5f15\u5165<code>MyClass<\/code>\u548c<code>my_variable<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from module5 import MyClass, my_variable<\/p>\n<h2><strong>\u521b\u5efaMyClass\u7684\u5b9e\u4f8b\u5e76\u8c03\u7528greet\u65b9\u6cd5<\/strong><\/h2>\n<p>my_instance = MyClass()<\/p>\n<p>my_instance.greet()<\/p>\n<h2><strong>\u8f93\u51famy_variable\u7684\u503c<\/strong><\/h2>\n<p>print(my_variable)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5f15\u5165\u548c\u4f7f\u7528\u6a21\u5757\u4e2d\u7684\u7c7b\u548c\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u5728\u5927\u578b\u9879\u76ee\u4e2d\u7684\u6a21\u5757\u5f15\u5165\u7b56\u7565<\/p>\n<\/p>\n<p><p>\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u5408\u7406\u7684\u6a21\u5757\u5f15\u5165\u7b56\u7565\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4fdd\u6301\u6a21\u5757\u7684\u72ec\u7acb\u6027<\/strong>\uff1a\u6bcf\u4e2a\u6a21\u5757\u5e94\u8be5\u53ea\u8d1f\u8d23\u4e00\u4e2a\u7279\u5b9a\u7684\u529f\u80fd\uff0c\u907f\u514d\u6a21\u5757\u4e4b\u95f4\u7684\u5f3a\u8026\u5408\u3002<\/li>\n<li><strong>\u4f7f\u7528\u5305\u7ec4\u7ec7\u4ee3\u7801<\/strong>\uff1a\u5c06\u76f8\u5173\u7684\u6a21\u5757\u7ec4\u7ec7\u5230\u540c\u4e00\u4e2a\u5305\u4e2d\uff0c\u4f7f\u7528\u5305\u7ba1\u7406\u4ee3\u7801\u7ed3\u6784\uff0c\u4f7f\u5176\u66f4\u52a0\u6e05\u6670\u3002<\/li>\n<li><strong>\u907f\u514d\u5faa\u73af\u5f15\u7528<\/strong>\uff1a\u5faa\u73af\u5f15\u7528\u4f1a\u5bfc\u81f4\u5bfc\u5165\u9519\u8bef\uff0c\u5c3d\u91cf\u907f\u514d\u6a21\u5757\u4e4b\u95f4\u7684\u76f8\u4e92\u4f9d\u8d56\u3002<\/li>\n<li><strong>\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u5f15\u5165<\/strong>\uff1a\u7edd\u5bf9\u8def\u5f84\u5f15\u5165\u6a21\u5757\u66f4\u52a0\u76f4\u89c2\uff0c\u907f\u514d\u4e86\u76f8\u5bf9\u8def\u5f84\u53ef\u80fd\u5e26\u6765\u7684\u6df7\u6dc6\u3002<\/li>\n<li><strong>\u5408\u7406\u4f7f\u7528__init__.py<\/strong>\uff1a\u5728\u5305\u7684<code>__init__.py<\/code>\u4e2d\uff0c\u53ef\u4ee5\u5f15\u5165\u5e38\u7528\u7684\u6a21\u5757\uff0c\u7b80\u5316\u5916\u90e8\u4ee3\u7801\u7684\u5f15\u5165\u3002<\/li>\n<\/ol>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5f15\u5165\u5176\u4ed6\u6587\u4ef6\u4e2d\u51fd\u6570\u7684\u65b9\u6cd5\uff0c\u5305\u62ecimport\u8bed\u53e5\u3001from&#8230;import\u8bed\u53e5\u3001sys\u6a21\u5757\u6dfb\u52a0\u8def\u5f84\u3001\u4f7f\u7528\u7edd\u5bf9\u6216\u76f8\u5bf9\u8def\u5f84\u3001importlib\u52a8\u6001\u52a0\u8f7d\u6a21\u5757\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\u3002\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u9075\u5faa\u6a21\u5757\u5316\u3001\u5305\u7ba1\u7406\u548c\u907f\u514d\u5faa\u73af\u5f15\u7528\u7b49\u539f\u5219\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7ec4\u7ec7\u548c\u7ba1\u7406\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5f15\u5165\u6a21\u5757\u6216\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5f15\u5165\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u6216\u6a21\u5757\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a<code>my_functions.py<\/code>\u7684\u6587\u4ef6\uff0c\u91cc\u9762\u5b9a\u4e49\u4e86\u4e00\u4e9b\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u5728\u53e6\u4e00\u4e2a\u6587\u4ef6\u4e2d\u901a\u8fc7<code>import my_functions<\/code>\u6765\u5f15\u5165\u8fd9\u4e2a\u6a21\u5757\u3002\u4e4b\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>my_functions.function_name()<\/code>\u7684\u65b9\u5f0f\u6765\u8c03\u7528\u5176\u4e2d\u7684\u51fd\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528from&#8230;import\u8bed\u53e5\u5f15\u5165\u7279\u5b9a\u51fd\u6570\uff1f<\/strong><br \/>\u5982\u679c\u53ea\u60f3\u5f15\u5165\u67d0\u4e2a\u7279\u5b9a\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>from<\/code>\u8bed\u53e5\u3002\u4f8b\u5982\uff0c<code>from my_functions import function_name<\/code>\u3002\u8fd9\u6837\uff0c\u4f60\u5c31\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7<code>function_name()<\/code>\u6765\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u800c\u4e0d\u9700\u8981\u52a0\u4e0a\u6a21\u5757\u540d\u3002<\/p>\n<p><strong>\u5728\u4e0d\u540c\u76ee\u5f55\u4e2d\u5982\u4f55\u5f15\u5165\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\uff1f<\/strong><br \/>\u5f53\u4f60\u7684\u6587\u4ef6\u5206\u5e03\u5728\u4e0d\u540c\u76ee\u5f55\u4e2d\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys.path<\/code>\u6765\u6dfb\u52a0\u9700\u8981\u5f15\u5165\u7684\u6a21\u5757\u8def\u5f84\u3002\u9996\u5148\uff0c\u5bfc\u5165<code>sys<\/code>\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>sys.path.append(&#39;\/path\/to\/your\/module&#39;)<\/code>\u6dfb\u52a0\u8def\u5f84\u3002\u63a5\u7740\uff0c\u5c31\u53ef\u4ee5\u6309\u7167\u5e38\u89c4\u65b9\u5f0f\u5f15\u5165\u8be5\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u4e86\u3002\u786e\u4fdd\u8def\u5f84\u6b63\u786e\u4e14\u6587\u4ef6\u5b58\u5728\uff0c\u4ee5\u907f\u514d\u5bfc\u5165\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5f15\u5165\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7import\u8bed\u53e5\u3001from&#8230;import\u8bed\u53e5\u3001sys\u6a21\u5757\u6dfb [&hellip;]","protected":false},"author":3,"featured_media":1049323,"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\/1049316"}],"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=1049316"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1049316\/revisions"}],"predecessor-version":[{"id":1049324,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1049316\/revisions\/1049324"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1049323"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1049316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1049316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1049316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}