{"id":1108282,"date":"2025-01-08T16:58:33","date_gmt":"2025-01-08T08:58:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1108282.html"},"modified":"2025-01-08T16:58:36","modified_gmt":"2025-01-08T08:58:36","slug":"python%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%8f%a6%e4%b8%80%e4%b8%aa%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1108282.html","title":{"rendered":"python\u5982\u4f55\u4f7f\u7528\u53e6\u4e00\u4e2a\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072002\/48da0e54-a091-400e-a33e-ccf11b774c73.webp\" alt=\"python\u5982\u4f55\u4f7f\u7528\u53e6\u4e00\u4e2a\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u4f7f\u7528\u53e6\u4e00\u4e2a\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\u5bfc\u5165\u6a21\u5757\u3001\u4f7f\u7528import\u8bed\u53e5\u3001\u5bfc\u5165\u7279\u5b9a\u51fd\u6570\u6216\u7c7b\u3001\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528import\u8bed\u53e5\u662f\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7import\u8bed\u53e5\u53ef\u4ee5\u5c06\u53e6\u4e00\u4e2a\u6587\u4ef6\u4f5c\u4e3a\u6a21\u5757\u5bfc\u5165\u5230\u5f53\u524d\u811a\u672c\u4e2d\uff0c\u5e76\u8bbf\u95ee\u5176\u5185\u90e8\u7684\u51fd\u6570\u3001\u7c7b\u548c\u53d8\u91cf\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u53e6\u4e00\u4e2a\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001IMPORT\u8bed\u53e5<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\uff0cimport\u8bed\u53e5\u7528\u4e8e\u5bfc\u5165\u6a21\u5757\u3002\u6a21\u5757\u662f\u5305\u542bPython\u4ee3\u7801\u7684\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u5c31\u662f\u6a21\u5757\u540d\u3002\u901a\u8fc7import\u8bed\u53e5\uff0c\u53ef\u4ee5\u5c06\u53e6\u4e00\u4e2a\u6587\u4ef6\u4f5c\u4e3a\u6a21\u5757\u5bfc\u5165\u5230\u5f53\u524d\u811a\u672c\u4e2d\uff0c\u5e76\u8bbf\u95ee\u5176\u5185\u90e8\u7684\u51fd\u6570\u3001\u7c7b\u548c\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h2>\u4f7f\u7528import\u8bed\u53e5\u5bfc\u5165\u6574\u4e2a\u6a21\u5757<\/h2>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a<code>module1.py<\/code>\u7684\u6587\u4ef6\uff0c\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\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>\u6211\u4eec\u53ef\u4ee5\u5728\u53e6\u4e00\u4e2aPython\u6587\u4ef6\u4e2d\u4f7f\u7528import\u8bed\u53e5\u5bfc\u5165<code>module1<\/code>\u6a21\u5757\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>\u8fd0\u884c<code>main.py<\/code>\u540e\uff0c\u5c06\u8f93\u51fa<code>Hello from module1<\/code>\u3002\u901a\u8fc7import\u8bed\u53e5\uff0c\u6211\u4eec\u6210\u529f\u5730\u5c06<code>module1<\/code>\u4f5c\u4e3a\u6a21\u5757\u5bfc\u5165\uff0c\u5e76\u8c03\u7528\u4e86\u5176\u5185\u90e8\u7684<code>hello<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4f7f\u7528import\u8bed\u53e5\u5bfc\u5165\u7279\u5b9a\u51fd\u6570\u6216\u7c7b<\/h2>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u67d0\u4e2a\u7279\u5b9a\u51fd\u6570\u6216\u7c7b\uff0c\u800c\u4e0d\u662f\u6574\u4e2a\u6a21\u5757\u3002\u6b64\u65f6\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 module1 import hello<\/p>\n<p>hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c\u6211\u4eec\u53ea\u5bfc\u5165\u4e86<code>module1<\/code>\u4e2d\u7684<code>hello<\/code>\u51fd\u6570\uff0c\u5e76\u76f4\u63a5\u8c03\u7528\u5b83\uff0c\u800c\u4e0d\u9700\u8981\u901a\u8fc7<code>module1<\/code>\u6a21\u5757\u540d\u6765\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001AS\u5173\u952e\u5b57<\/strong><\/h2>\n<p><h2>\u4f7f\u7528as\u5173\u952e\u5b57\u91cd\u547d\u540d\u6a21\u5757<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u6a21\u5757\u540d\u8fc7\u957f\u6216\u8005\u4e0e\u5f53\u524d\u811a\u672c\u4e2d\u7684\u53d8\u91cf\u540d\u51b2\u7a81\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528as\u5173\u952e\u5b57\u7ed9\u6a21\u5757\u91cd\u547d\u540d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import module1 as m1<\/p>\n<p>m1.hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7as\u5173\u952e\u5b57\uff0c\u6211\u4eec\u5c06<code>module1<\/code>\u91cd\u547d\u540d\u4e3a<code>m1<\/code>\uff0c\u8fd9\u6837\u5728\u5f53\u524d\u811a\u672c\u4e2d\u5c31\u53ef\u4ee5\u4f7f\u7528<code>m1<\/code>\u6765\u4ee3\u66ff<code>module1<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u4f7f\u7528as\u5173\u952e\u5b57\u91cd\u547d\u540d\u7279\u5b9a\u51fd\u6570\u6216\u7c7b<\/h2>\n<\/p>\n<p><p>\u540c\u6837\u5730\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528as\u5173\u952e\u5b57\u7ed9\u5bfc\u5165\u7684\u7279\u5b9a\u51fd\u6570\u6216\u7c7b\u91cd\u547d\u540d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from module1 import hello as hi<\/p>\n<p>hi()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7as\u5173\u952e\u5b57\uff0c\u6211\u4eec\u5c06<code>hello<\/code>\u51fd\u6570\u91cd\u547d\u540d\u4e3a<code>hi<\/code>\uff0c\u8fd9\u6837\u5728\u5f53\u524d\u811a\u672c\u4e2d\u5c31\u53ef\u4ee5\u4f7f\u7528<code>hi<\/code>\u6765\u4ee3\u66ff<code>hello<\/code>\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001RELATIVE IMPORT<\/strong><\/h2>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u901a\u5e38\u4f1a\u5c06\u4ee3\u7801\u7ec4\u7ec7\u6210\u591a\u4e2a\u5305\uff08package\uff09\uff0c\u6bcf\u4e2a\u5305\u5305\u542b\u591a\u4e2a\u6a21\u5757\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\uff08relative import\uff09\u6765\u5bfc\u5165\u540c\u4e00\u5305\u4e2d\u7684\u5176\u4ed6\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h2>\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u5bfc\u5165\u540c\u4e00\u5305\u4e2d\u7684\u6a21\u5757<\/h2>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u5982\u4e0b\u76ee\u5f55\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>    \u251c\u2500\u2500 package1\/<\/p>\n<p>    \u2502   \u251c\u2500\u2500 __init__.py<\/p>\n<p>    \u2502   \u251c\u2500\u2500 module1.py<\/p>\n<p>    \u2502   \u2514\u2500\u2500 module2.py<\/p>\n<p>    \u2514\u2500\u2500 main.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>module1.py<\/code>\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\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><code>module2.py<\/code>\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module2.py<\/p>\n<p>from .module1 import hello<\/p>\n<p>def greet():<\/p>\n<p>    hello()<\/p>\n<p>    print(&quot;Greetings from module2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>main.py<\/code>\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from package1 import module2<\/p>\n<p>module2.greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>module2.py<\/code>\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165<code>from .module1 import hello<\/code>\u5c06\u540c\u4e00\u5305\u4e2d\u7684<code>module1<\/code>\u6a21\u5757\u5bfc\u5165\uff0c\u5e76\u5728<code>greet<\/code>\u51fd\u6570\u4e2d\u8c03\u7528\u4e86<code>hello<\/code>\u51fd\u6570\u3002\u5728<code>main.py<\/code>\u4e2d\uff0c\u6211\u4eec\u5bfc\u5165\u4e86<code>package1<\/code>\u5305\u4e2d\u7684<code>module2<\/code>\u6a21\u5757\uff0c\u5e76\u8c03\u7528\u4e86<code>module2<\/code>\u4e2d\u7684<code>greet<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u5bfc\u5165\u7236\u5305\u4e2d\u7684\u6a21\u5757<\/h2>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u5982\u4e0b\u76ee\u5f55\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>    \u251c\u2500\u2500 package1\/<\/p>\n<p>    \u2502   \u251c\u2500\u2500 __init__.py<\/p>\n<p>    \u2502   \u251c\u2500\u2500 module1.py<\/p>\n<p>    \u2502   \u2514\u2500\u2500 subpackage1\/<\/p>\n<p>    \u2502       \u251c\u2500\u2500 __init__.py<\/p>\n<p>    \u2502       \u2514\u2500\u2500 module2.py<\/p>\n<p>    \u2514\u2500\u2500 main.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>module1.py<\/code>\u6587\u4ef6\u5185\u5bb9\u4e0e\u524d\u9762\u76f8\u540c\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><code>module2.py<\/code>\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module2.py<\/p>\n<p>from ..module1 import hello<\/p>\n<p>def greet():<\/p>\n<p>    hello()<\/p>\n<p>    print(&quot;Greetings from module2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>main.py<\/code>\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from package1.subpackage1 import module2<\/p>\n<p>module2.greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>module2.py<\/code>\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165<code>from ..module1 import hello<\/code>\u5c06\u7236\u5305<code>package1<\/code>\u4e2d\u7684<code>module1<\/code>\u6a21\u5757\u5bfc\u5165\uff0c\u5e76\u5728<code>greet<\/code>\u51fd\u6570\u4e2d\u8c03\u7528\u4e86<code>hello<\/code>\u51fd\u6570\u3002\u5728<code>main.py<\/code>\u4e2d\uff0c\u6211\u4eec\u5bfc\u5165\u4e86<code>package1.subpackage1<\/code>\u5305\u4e2d\u7684<code>module2<\/code>\u6a21\u5757\uff0c\u5e76\u8c03\u7528\u4e86<code>module2<\/code>\u4e2d\u7684<code>greet<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001__IMPORT__\u51fd\u6570<\/strong><\/h2>\n<p><p>\u9664\u4e86\u4f7f\u7528import\u8bed\u53e5\u548cfrom&#8230;import\u8bed\u53e5\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u5185\u7f6e\u51fd\u6570<code>__import__<\/code>\uff0c\u53ef\u4ee5\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h2>\u4f7f\u7528__import__\u51fd\u6570\u5bfc\u5165\u6a21\u5757<\/h2>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a<code>module1.py<\/code>\u7684\u6587\u4ef6\uff0c\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\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>\u6211\u4eec\u53ef\u4ee5\u5728\u53e6\u4e00\u4e2aPython\u6587\u4ef6\u4e2d\u4f7f\u7528<code>__import__<\/code>\u51fd\u6570\u52a8\u6001\u5bfc\u5165<code>module1<\/code>\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>module1 = __import__(&#39;module1&#39;)<\/p>\n<p>module1.hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>__import__<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u52a8\u6001\u5bfc\u5165\u4e86<code>module1<\/code>\u6a21\u5757\uff0c\u5e76\u8c03\u7528\u4e86\u5176\u5185\u90e8\u7684<code>hello<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u51fd\u6570\u6216\u7c7b<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u51fd\u6570\u6216\u7c7b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>getattr<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>module1 = __import__(&#39;module1&#39;)<\/p>\n<p>hello = getattr(module1, &#39;hello&#39;)<\/p>\n<p>hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>getattr<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u4ece\u52a8\u6001\u5bfc\u5165\u7684<code>module1<\/code>\u6a21\u5757\u4e2d\u83b7\u53d6\u4e86<code>hello<\/code>\u51fd\u6570\uff0c\u5e76\u8c03\u7528\u5b83\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u5305\u548c\u6a21\u5757\u7684\u7ec4\u7ec7<\/strong><\/h2>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u5408\u7406\u7ec4\u7ec7\u4ee3\u7801\u7ed3\u6784\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u901a\u5e38\uff0c\u6211\u4eec\u4f1a\u5c06\u76f8\u5173\u7684\u6a21\u5757\u653e\u5728\u540c\u4e00\u4e2a\u5305\u4e2d\uff0c\u4e0d\u76f8\u5173\u7684\u6a21\u5757\u653e\u5728\u4e0d\u540c\u7684\u5305\u4e2d\u3002\u6bcf\u4e2a\u5305\u4e0b\u90fd\u53ef\u4ee5\u6709\u591a\u4e2a\u6a21\u5757\uff0c\u6bcf\u4e2a\u6a21\u5757\u90fd\u53ef\u4ee5\u6709\u591a\u4e2a\u51fd\u6570\u548c\u7c7b\u3002<\/p>\n<\/p>\n<p><h2>\u521b\u5efa\u5305\u548c\u6a21\u5757<\/h2>\n<\/p>\n<p><p>\u4e00\u4e2a\u5305\u662f\u4e00\u4e2a\u5305\u542b<code>__init__.py<\/code>\u6587\u4ef6\u7684\u76ee\u5f55\uff0c\u8be5\u6587\u4ef6\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u4f46\u5fc5\u987b\u5b58\u5728\u3002\u5047\u8bbe\u6211\u4eec\u6709\u5982\u4e0b\u76ee\u5f55\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>    \u251c\u2500\u2500 package1\/<\/p>\n<p>    \u2502   \u251c\u2500\u2500 __init__.py<\/p>\n<p>    \u2502   \u251c\u2500\u2500 module1.py<\/p>\n<p>    \u2502   \u2514\u2500\u2500 module2.py<\/p>\n<p>    \u2514\u2500\u2500 main.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>module1.py<\/code>\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\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><code>module2.py<\/code>\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module2.py<\/p>\n<p>def greet():<\/p>\n<p>    print(&quot;Greetings from module2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>main.py<\/code>\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from package1 import module1, module2<\/p>\n<p>module1.hello()<\/p>\n<p>module2.greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u5c06\u76f8\u5173\u7684\u6a21\u5757<code>module1<\/code>\u548c<code>module2<\/code>\u653e\u5728\u540c\u4e00\u4e2a\u5305<code>package1<\/code>\u4e2d\uff0c\u5e76\u5728<code>main.py<\/code>\u4e2d\u5bfc\u5165\u8fd9\u4e24\u4e2a\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h2>\u521b\u5efa\u5b50\u5305\u548c\u6a21\u5757<\/h2>\n<\/p>\n<p><p>\u5b50\u5305\u662f\u5305\u4e2d\u7684\u5305\uff0c\u5b83\u4eec\u7684\u521b\u5efa\u65b9\u5f0f\u4e0e\u5305\u76f8\u540c\u3002\u5047\u8bbe\u6211\u4eec\u6709\u5982\u4e0b\u76ee\u5f55\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>    \u251c\u2500\u2500 package1\/<\/p>\n<p>    \u2502   \u251c\u2500\u2500 __init__.py<\/p>\n<p>    \u2502   \u251c\u2500\u2500 module1.py<\/p>\n<p>    \u2502   \u2514\u2500\u2500 subpackage1\/<\/p>\n<p>    \u2502       \u251c\u2500\u2500 __init__.py<\/p>\n<p>    \u2502       \u2514\u2500\u2500 module2.py<\/p>\n<p>    \u2514\u2500\u2500 main.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>module1.py<\/code>\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\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><code>module2.py<\/code>\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module2.py<\/p>\n<p>def greet():<\/p>\n<p>    print(&quot;Greetings from module2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>main.py<\/code>\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from package1 import module1<\/p>\n<p>from package1.subpackage1 import module2<\/p>\n<p>module1.hello()<\/p>\n<p>module2.greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u5c06<code>module2<\/code>\u653e\u5728<code>subpackage1<\/code>\u5b50\u5305\u4e2d\uff0c\u5e76\u5728<code>main.py<\/code>\u4e2d\u5206\u522b\u5bfc\u5165<code>package1<\/code>\u5305\u4e2d\u7684<code>module1<\/code>\u548c<code>subpackage1<\/code>\u5b50\u5305\u4e2d\u7684<code>module2<\/code>\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u5bfc\u5165\u8def\u5f84<\/strong><\/h2>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6a21\u5757\u53ef\u80fd\u4e0d\u5728\u6807\u51c6\u8def\u5f84\u4e2d\uff0c\u5bfc\u81f4\u65e0\u6cd5\u5bfc\u5165\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys.path<\/code>\u6765\u6dfb\u52a0\u6a21\u5757\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h2>\u4f7f\u7528sys.path\u6dfb\u52a0\u6a21\u5757\u8def\u5f84<\/h2>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u5982\u4e0b\u76ee\u5f55\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>    \u251c\u2500\u2500 libs\/<\/p>\n<p>    \u2502   \u2514\u2500\u2500 module1.py<\/p>\n<p>    \u2514\u2500\u2500 main.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>module1.py<\/code>\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\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><code>main.py<\/code>\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\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;libs&#39;)<\/p>\n<p>import module1<\/p>\n<p>module1.hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5c06<code>libs<\/code>\u76ee\u5f55\u6dfb\u52a0\u5230<code>sys.path<\/code>\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6210\u529f\u5bfc\u5165<code>libs<\/code>\u76ee\u5f55\u4e2d\u7684<code>module1<\/code>\u6a21\u5757\uff0c\u5e76\u8c03\u7528\u5176\u5185\u90e8\u7684<code>hello<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4f7f\u7528\u73af\u5883\u53d8\u91cfPYTHONPATH<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528<code>sys.path<\/code>\uff0c\u8fd8\u53ef\u4ee5\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<code>PYTHONPATH<\/code>\u6765\u6307\u5b9a\u6a21\u5757\u8def\u5f84\u3002\u5047\u8bbe\u6211\u4eec\u6709\u5982\u4e0b\u76ee\u5f55\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>    \u251c\u2500\u2500 libs\/<\/p>\n<p>    \u2502   \u2514\u2500\u2500 module1.py<\/p>\n<p>    \u2514\u2500\u2500 main.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5728\u8fd0\u884c<code>main.py<\/code>\u4e4b\u524d\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<code>PYTHONPATH<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">export PYTHONPATH=libs<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728<code>main.py<\/code>\u4e2d\u76f4\u63a5\u5bfc\u5165<code>module1<\/code>\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import module1<\/p>\n<p>module1.hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<code>PYTHONPATH<\/code>\uff0c\u6211\u4eec\u6210\u529f\u6307\u5b9a\u4e86\u6a21\u5757\u8def\u5f84\uff0c\u5e76\u5bfc\u5165\u4e86<code>libs<\/code>\u76ee\u5f55\u4e2d\u7684<code>module1<\/code>\u6a21\u5757\u3002<\/p>\n<\/p>\n<h2><strong>\u4e03\u3001\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5<\/strong><\/h2>\n<p><h2>\u6a21\u5757\u627e\u4e0d\u5230<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u5bfc\u5165\u6a21\u5757\u65f6\u51fa\u73b0<code>ModuleNotFoundError<\/code>\uff0c\u901a\u5e38\u662f\u56e0\u4e3a\u6a21\u5757\u8def\u5f84\u4e0d\u6b63\u786e\u3002\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u6a21\u5757\u8def\u5f84\uff0c\u4f7f\u7528<code>sys.path<\/code>\u6216\u8bbe\u7f6e<code>PYTHONPATH<\/code>\u6765\u89e3\u51b3\u3002<\/p>\n<\/p>\n<p><h2>\u5faa\u73af\u5bfc\u5165<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u6a21\u5757\u4e4b\u95f4\u76f8\u4e92\u5bfc\u5165\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5faa\u73af\u5bfc\u5165\u9519\u8bef\u3002\u53ef\u4ee5\u901a\u8fc7\u91cd\u6784\u4ee3\u7801\uff0c\u907f\u514d\u5faa\u73af\u5bfc\u5165\u3002\u901a\u5e38\u7684\u505a\u6cd5\u662f\u5c06\u516c\u5171\u7684\u4ee3\u7801\u653e\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u6a21\u5757\u4e2d\uff0c\u4f9b\u5176\u4ed6\u6a21\u5757\u5bfc\u5165\u3002<\/p>\n<\/p>\n<p><h2>\u547d\u540d\u51b2\u7a81<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u5bfc\u5165\u7684\u6a21\u5757\u6216\u51fd\u6570\u4e0e\u5f53\u524d\u811a\u672c\u4e2d\u7684\u53d8\u91cf\u540d\u51b2\u7a81\uff0c\u53ef\u4ee5\u4f7f\u7528as\u5173\u952e\u5b57\u91cd\u547d\u540d\u6a21\u5757\u6216\u51fd\u6570\uff0c\u907f\u514d\u547d\u540d\u51b2\u7a81\u3002<\/p>\n<\/p>\n<h2><strong>\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u8bb2\u89e3\u4e86\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u53e6\u4e00\u4e2a\u6587\u4ef6\uff0c\u5305\u62ec\u4f7f\u7528import\u8bed\u53e5\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\u548c\u7279\u5b9a\u51fd\u6570\u6216\u7c7b\uff0c\u4f7f\u7528as\u5173\u952e\u5b57\u91cd\u547d\u540d\u6a21\u5757\u6216\u51fd\u6570\uff0c\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u5bfc\u5165\u540c\u4e00\u5305\u6216\u7236\u5305\u4e2d\u7684\u6a21\u5757\uff0c\u4f7f\u7528<code>__import__<\/code>\u51fd\u6570\u52a8\u6001\u5bfc\u5165\u6a21\u5757\uff0c\u5408\u7406\u7ec4\u7ec7\u5305\u548c\u6a21\u5757\uff0c\u6dfb\u52a0\u6a21\u5757\u8def\u5f84\uff0c\u89e3\u51b3\u5e38\u89c1\u95ee\u9898\u7b49\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u4e2d\u7684\u6a21\u5757\u548c\u5305\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165\u5176\u4ed6\u6587\u4ef6\u7684\u529f\u80fd\u6216\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u6765\u5bfc\u5165\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u529f\u80fd\u6216\u53d8\u91cf\u3002\u53ea\u9700\u786e\u4fdd\u8981\u5bfc\u5165\u7684\u6587\u4ef6\u4e0e\u5f53\u524d\u6587\u4ef6\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\uff0c\u6216\u8005\u5728Python\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84\u4e2d\u3002\u4f7f\u7528<code>from filename import function_name<\/code>\u53ef\u4ee5\u53ea\u5bfc\u5165\u7279\u5b9a\u7684\u529f\u80fd\u6216\u53d8\u91cf\uff0c\u800c<code>import filename<\/code>\u5219\u4f1a\u5bfc\u5165\u6574\u4e2a\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u5185\u5bb9\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5bfc\u5165\u6587\u4ef6\u65f6\u7684\u8def\u5f84\u95ee\u9898\uff1f<\/strong><br \/>\u5f53\u8981\u5bfc\u5165\u7684\u6587\u4ef6\u4e0d\u5728\u540c\u4e00\u76ee\u5f55\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys.path.append<\/code>\u65b9\u6cd5\u6765\u6dfb\u52a0\u6587\u4ef6\u7684\u8def\u5f84\u3002\u8fd9\u6837\uff0cPython\u5c31\u80fd\u627e\u5230\u5e76\u5bfc\u5165\u8be5\u6587\u4ef6\u3002\u6b64\u5916\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u6216\u7edd\u5bf9\u8def\u5f84\u6765\u786e\u4fdd\u6b63\u786e\u5bfc\u5165\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u907f\u514d\u5faa\u73af\u5bfc\u5165\u7684\u95ee\u9898\uff1f<\/strong><br \/>\u5faa\u73af\u5bfc\u5165\u53d1\u751f\u5728\u4e24\u4e2a\u6216\u591a\u4e2a\u6a21\u5757\u76f8\u4e92\u5bfc\u5165\u65f6\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u95ee\u9898\uff0c\u53ef\u4ee5\u91cd\u65b0\u7ec4\u7ec7\u4ee3\u7801\uff0c\u5c06\u5171\u540c\u4f9d\u8d56\u7684\u529f\u80fd\u653e\u5165\u4e00\u4e2a\u5355\u72ec\u7684\u6a21\u5757\u4e2d\uff0c\u6216\u8005\u4f7f\u7528\u5c40\u90e8\u5bfc\u5165\uff08\u5728\u51fd\u6570\u5185\u90e8\u5bfc\u5165\uff09\u6765\u5ef6\u8fdf\u5bfc\u5165\u7684\u65f6\u673a\uff0c\u4ece\u800c\u907f\u514d\u5faa\u73af\u4f9d\u8d56\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4f7f\u7528\u53e6\u4e00\u4e2a\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\u5bfc\u5165\u6a21\u5757\u3001\u4f7f\u7528import\u8bed\u53e5\u3001\u5bfc\u5165\u7279\u5b9a\u51fd\u6570\u6216\u7c7b\u3001\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u7b49\u3002 \u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1108290,"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\/1108282"}],"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=1108282"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108282\/revisions"}],"predecessor-version":[{"id":1108292,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108282\/revisions\/1108292"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1108290"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1108282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1108282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1108282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}