{"id":1171353,"date":"2025-01-15T16:34:15","date_gmt":"2025-01-15T08:34:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1171353.html"},"modified":"2025-01-15T16:34:18","modified_gmt":"2025-01-15T08:34:18","slug":"python%e6%96%87%e4%bb%b6%e5%a6%82%e4%bd%95%e7%9b%b8%e4%ba%92%e8%b0%83%e7%94%a8-3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1171353.html","title":{"rendered":"python\u6587\u4ef6\u5982\u4f55\u76f8\u4e92\u8c03\u7528"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26073657\/30b81aea-0ee4-45ab-8d82-667ec794fd8c.webp\" alt=\"python\u6587\u4ef6\u5982\u4f55\u76f8\u4e92\u8c03\u7528\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u6587\u4ef6\u4e4b\u95f4\u7684\u76f8\u4e92\u8c03\u7528\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<strong>\u4f7f\u7528import\u3001\u4f7f\u7528from&#8230;import\u3001\u4f7f\u7528\u6a21\u5757\u8def\u5f84\u3001\u4f7f\u7528__init__.py\u6587\u4ef6<\/strong>\u7b49\u662f\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u4ee5import\u4e3a\u4f8b\u8be6\u7ec6\u63cf\u8ff0\uff1a\u4f7f\u7528import\u5173\u952e\u5b57\u53ef\u4ee5\u5f15\u5165\u53e6\u4e00\u4e2aPython\u6587\u4ef6\u4e2d\u7684\u6a21\u5757\u6216\u51fd\u6570\uff0c\u4ece\u800c\u5728\u5f53\u524d\u6587\u4ef6\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u529f\u80fd\u3002\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6709\u4e24\u4e2aPython\u6587\u4ef6\uff0cfile1.py \u548c file2.py\u3002\u5728file1.py\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e9b\u51fd\u6570\u6216\u7c7b\uff0c\u4f60\u60f3\u5728file2.py\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u5b9a\u4e49\u7684\u5185\u5bb9\uff0c\u53ea\u9700\u5728file2.py\u4e2d\u901a\u8fc7import\u5173\u952e\u5b57\u5f15\u5165file1.py\uff0c\u7136\u540e\u5c31\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528file1.py\u4e2d\u7684\u51fd\u6570\u6216\u7c7b\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># file1.py<\/p>\n<p>def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<h2><strong>file2.py<\/strong><\/h2>\n<p>import file1<\/p>\n<p>print(file1.greet(&quot;Alice&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\u4e14\u6613\u4e8e\u4f7f\u7528\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cimport\u8bed\u53e5\u4f1a\u6267\u884c\u88ab\u5f15\u5165\u7684\u6587\u4ef6\uff0c\u56e0\u6b64\u8981\u786e\u4fdd\u88ab\u5f15\u5165\u7684\u6587\u4ef6\u5728\u6267\u884c\u65f6\u4e0d\u4f1a\u6709\u610f\u5916\u7684\u526f\u4f5c\u7528\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u6587\u4ef6\u76f8\u4e92\u8c03\u7528\u7684\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528import\u5173\u952e\u5b57<\/h2>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>import\u5173\u952e\u5b57\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u6a21\u5757\u5f15\u5165\u65b9\u5f0f\u3002\u5b83\u53ef\u4ee5\u5f15\u5165\u6574\u4e2a\u6a21\u5757\uff0c\u4ece\u800c\u5728\u5f53\u524d\u6587\u4ef6\u4e2d\u4f7f\u7528\u8be5\u6a21\u5757\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u51fd\u6570\u548c\u7c7b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># file1.py<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<h2><strong>file2.py<\/strong><\/h2>\n<p>import file1<\/p>\n<p>result = file1.add(5, 3)<\/p>\n<p>print(result)  # \u8f93\u51fa: 8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cfile2.py\u901a\u8fc7import\u5173\u952e\u5b57\u5f15\u5165\u4e86file1.py\uff0c\u5e76\u8c03\u7528\u4e86file1.py\u4e2d\u7684add\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u907f\u514d\u547d\u540d\u51b2\u7a81<\/h3>\n<\/p>\n<p><p>\u5f53\u4e0d\u540c\u7684\u6a21\u5757\u4e2d\u5b58\u5728\u540c\u540d\u51fd\u6570\u6216\u53d8\u91cf\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u7ed9\u5f15\u5165\u7684\u6a21\u5757\u8d77\u522b\u540d\u6765\u907f\u514d\u547d\u540d\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># file1.py<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<h2><strong>file3.py<\/strong><\/h2>\n<p>def add(a, b):<\/p>\n<p>    return a * b<\/p>\n<h2><strong>file2.py<\/strong><\/h2>\n<p>import file1 as f1<\/p>\n<p>import file3 as f3<\/p>\n<p>result1 = f1.add(5, 3)<\/p>\n<p>result2 = f3.add(5, 3)<\/p>\n<p>print(result1)  # \u8f93\u51fa: 8<\/p>\n<p>print(result2)  # \u8f93\u51fa: 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cfile2.py\u901a\u8fc7\u7ed9file1.py\u548cfile3.py\u8d77\u522b\u540df1\u548cf3\uff0c\u907f\u514d\u4e86\u547d\u540d\u51b2\u7a81\uff0c\u5e76\u4e14\u53ef\u4ee5\u6e05\u695a\u5730\u77e5\u9053\u8c03\u7528\u7684\u662f\u54ea\u4e2a\u6a21\u5757\u4e2d\u7684add\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528from&#8230;import\u5173\u952e\u5b57<\/h2>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>from&#8230;import\u5173\u952e\u5b57\u53ef\u4ee5\u5f15\u5165\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u51fd\u6570\u6216\u7c7b\uff0c\u4ece\u800c\u76f4\u63a5\u5728\u5f53\u524d\u6587\u4ef6\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u6216\u7c7b\uff0c\u800c\u4e0d\u9700\u8981\u901a\u8fc7\u6a21\u5757\u540d\u6765\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># file1.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<h2><strong>file2.py<\/strong><\/h2>\n<p>from file1 import add<\/p>\n<p>result = add(5, 3)<\/p>\n<p>print(result)  # \u8f93\u51fa: 8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cfile2.py\u901a\u8fc7from file1 import add\u5f15\u5165\u4e86file1.py\u4e2d\u7684add\u51fd\u6570\uff0c\u5e76\u76f4\u63a5\u8c03\u7528\u4e86\u8be5\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u5f15\u5165\u591a\u4e2a\u51fd\u6570\u6216\u7c7b<\/h3>\n<\/p>\n<p><p>from&#8230;import\u5173\u952e\u5b57\u8fd8\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5f15\u5165\u6a21\u5757\u4e2d\u7684\u591a\u4e2a\u51fd\u6570\u6216\u7c7b\uff0c\u7528\u9017\u53f7\u5206\u9694\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># file1.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<h2><strong>file2.py<\/strong><\/h2>\n<p>from file1 import add, subtract<\/p>\n<p>result1 = add(5, 3)<\/p>\n<p>result2 = subtract(5, 3)<\/p>\n<p>print(result1)  # \u8f93\u51fa: 8<\/p>\n<p>print(result2)  # \u8f93\u51fa: -2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cfile2.py\u901a\u8fc7from file1 import add, subtract\u5f15\u5165\u4e86file1.py\u4e2d\u7684add\u548csubtract\u51fd\u6570\uff0c\u5e76\u76f4\u63a5\u8c03\u7528\u4e86\u8fd9\u4e24\u4e2a\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u6a21\u5757\u8def\u5f84<\/h2>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u5f53\u6a21\u5757\u4f4d\u4e8e\u4e0d\u540c\u7684\u76ee\u5f55\u4e2d\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6a21\u5757\u7684\u76f8\u5bf9\u8def\u5f84\u6216\u7edd\u5bf9\u8def\u5f84\u6765\u5f15\u5165\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># project\/<\/p>\n<h2><strong>\u251c\u2500\u2500 folder1\/<\/strong><\/h2>\n<h2><strong>\u2502   \u2514\u2500\u2500 file1.py<\/strong><\/h2>\n<h2><strong>\u2514\u2500\u2500 folder2\/<\/strong><\/h2>\n<h2><strong>    \u2514\u2500\u2500 file2.py<\/strong><\/h2>\n<h2><strong>folder1\/file1.py<\/strong><\/h2>\n<p>def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<h2><strong>folder2\/file2.py<\/strong><\/h2>\n<p>import sys<\/p>\n<p>sys.path.append(&#39;..\/folder1&#39;)<\/p>\n<p>import file1<\/p>\n<p>print(file1.greet(&quot;Alice&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cfile2.py\u901a\u8fc7sys.path.append(&#39;..\/folder1&#39;)\u5c06folder1\u76ee\u5f55\u6dfb\u52a0\u5230Python\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84\u4e2d\uff0c\u4ece\u800c\u80fd\u591f\u5f15\u5165file1\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528\u5305<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9879\u76ee\u7ed3\u6784\u8f83\u4e3a\u590d\u6742\uff0c\u53ef\u4ee5\u5c06\u76f8\u5173\u7684\u6a21\u5757\u7ec4\u7ec7\u6210\u5305\u3002\u5305\u662f\u4e00\u4e2a\u5305\u542b__init__.py\u6587\u4ef6\u7684\u76ee\u5f55\uff0c\u8be5\u6587\u4ef6\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u4e5f\u53ef\u4ee5\u5305\u542b\u5305\u7684\u521d\u59cb\u5316\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># project\/<\/p>\n<h2><strong>\u251c\u2500\u2500 package1\/<\/strong><\/h2>\n<h2><strong>\u2502   \u251c\u2500\u2500 __init__.py<\/strong><\/h2>\n<h2><strong>\u2502   \u2514\u2500\u2500 file1.py<\/strong><\/h2>\n<h2><strong>\u2514\u2500\u2500 package2\/<\/strong><\/h2>\n<h2><strong>    \u251c\u2500\u2500 __init__.py<\/strong><\/h2>\n<h2><strong>    \u2514\u2500\u2500 file2.py<\/strong><\/h2>\n<h2><strong>package1\/file1.py<\/strong><\/h2>\n<p>def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<h2><strong>package2\/file2.py<\/strong><\/h2>\n<p>from package1 import file1<\/p>\n<p>print(file1.greet(&quot;Alice&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cpackage1\u548cpackage2\u662f\u4e24\u4e2a\u5305\uff0c\u901a\u8fc7from package1 import file1\u53ef\u4ee5\u5f15\u5165package1\u5305\u4e2d\u7684file1\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528__init__.py\u6587\u4ef6<\/h2>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p><strong>init<\/strong>.py\u6587\u4ef6\u662f\u5305\u7684\u521d\u59cb\u5316\u6587\u4ef6\uff0c\u5f53\u5305\u88ab\u5f15\u5165\u65f6\uff0c\u8be5\u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\u4f1a\u88ab\u6267\u884c\u3002\u901a\u8fc7\u5728__init__.py\u6587\u4ef6\u4e2d\u5f15\u5165\u5305\u4e2d\u7684\u6a21\u5757\uff0c\u53ef\u4ee5\u7b80\u5316\u5305\u7684\u5f15\u5165\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># project\/<\/p>\n<h2><strong>\u251c\u2500\u2500 package1\/<\/strong><\/h2>\n<h2><strong>\u2502   \u251c\u2500\u2500 __init__.py<\/strong><\/h2>\n<h2><strong>\u2502   \u2514\u2500\u2500 file1.py<\/strong><\/h2>\n<h2><strong>\u2514\u2500\u2500 package2\/<\/strong><\/h2>\n<h2><strong>    \u251c\u2500\u2500 __init__.py<\/strong><\/h2>\n<h2><strong>    \u2514\u2500\u2500 file2.py<\/strong><\/h2>\n<h2><strong>package1\/__init__.py<\/strong><\/h2>\n<p>from .file1 import greet<\/p>\n<h2><strong>package1\/file1.py<\/strong><\/h2>\n<p>def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<h2><strong>package2\/file2.py<\/strong><\/h2>\n<p>from package1 import greet<\/p>\n<p>print(greet(&quot;Alice&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cpackage1\u7684__init__.py\u6587\u4ef6\u5f15\u5165\u4e86file1\u6a21\u5757\u4e2d\u7684greet\u51fd\u6570\uff0c\u4ece\u800c\u5728\u5f15\u5165package1\u5305\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528greet\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u7ec4\u7ec7\u5927\u578b\u9879\u76ee<\/h3>\n<\/p>\n<p><p>\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7__init__.py\u6587\u4ef6\u5c06\u5305\u4e2d\u7684\u6a21\u5757\u7ec4\u7ec7\u6210\u4e00\u4e2a\u66f4\u4e3a\u7b80\u6d01\u7684\u63a5\u53e3\uff0c\u4ece\u800c\u7b80\u5316\u5305\u7684\u5f15\u5165\u548c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># project\/<\/p>\n<h2><strong>\u251c\u2500\u2500 package1\/<\/strong><\/h2>\n<h2><strong>\u2502   \u251c\u2500\u2500 __init__.py<\/strong><\/h2>\n<h2><strong>\u2502   \u251c\u2500\u2500 file1.py<\/strong><\/h2>\n<h2><strong>\u2502   \u2514\u2500\u2500 file2.py<\/strong><\/h2>\n<h2><strong>\u2514\u2500\u2500 package2\/<\/strong><\/h2>\n<h2><strong>    \u251c\u2500\u2500 __init__.py<\/strong><\/h2>\n<h2><strong>    \u2514\u2500\u2500 file3.py<\/strong><\/h2>\n<h2><strong>package1\/file1.py<\/strong><\/h2>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<h2><strong>package1\/file2.py<\/strong><\/h2>\n<p>def subtract(a, b):<\/p>\n<p>    return a - b<\/p>\n<h2><strong>package1\/__init__.py<\/strong><\/h2>\n<p>from .file1 import add<\/p>\n<p>from .file2 import subtract<\/p>\n<h2><strong>package2\/file3.py<\/strong><\/h2>\n<p>from package1 import add, subtract<\/p>\n<p>result1 = add(5, 3)<\/p>\n<p>result2 = subtract(5, 3)<\/p>\n<p>print(result1)  # \u8f93\u51fa: 8<\/p>\n<p>print(result2)  # \u8f93\u51fa: -2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cpackage1\u7684__init__.py\u6587\u4ef6\u5f15\u5165\u4e86file1\u548cfile2\u6a21\u5757\u4e2d\u7684add\u548csubtract\u51fd\u6570\uff0c\u4ece\u800c\u5728\u5f15\u5165package1\u5305\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8fd9\u4e24\u4e2a\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165<\/h2>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u76f8\u5bf9\u5bfc\u5165\u662f\u6307\u5728\u540c\u4e00\u5305\u5185\u901a\u8fc7\u76f8\u5bf9\u8def\u5f84\u5f15\u5165\u6a21\u5757\u3002\u76f8\u5bf9\u5bfc\u5165\u4f7f\u7528\u70b9\u53f7\uff08.\uff09\u8868\u793a\u5f53\u524d\u76ee\u5f55\u548c\u4e0a\u7ea7\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># project\/<\/p>\n<h2><strong>\u251c\u2500\u2500 package1\/<\/strong><\/h2>\n<h2><strong>\u2502   \u251c\u2500\u2500 __init__.py<\/strong><\/h2>\n<h2><strong>\u2502   \u251c\u2500\u2500 file1.py<\/strong><\/h2>\n<h2><strong>\u2502   \u2514\u2500\u2500 file2.py<\/strong><\/h2>\n<h2><strong>\u2514\u2500\u2500 package2\/<\/strong><\/h2>\n<h2><strong>    \u251c\u2500\u2500 __init__.py<\/strong><\/h2>\n<h2><strong>    \u2514\u2500\u2500 file3.py<\/strong><\/h2>\n<h2><strong>package1\/file1.py<\/strong><\/h2>\n<p>def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<h2><strong>package1\/file2.py<\/strong><\/h2>\n<p>from .file1 import greet<\/p>\n<p>print(greet(&quot;Alice&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cfile2.py\u901a\u8fc7from .file1 import greet\u8fdb\u884c\u76f8\u5bf9\u5bfc\u5165\uff0c\u5f15\u5165\u4e86file1\u6a21\u5757\u4e2d\u7684greet\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528\u591a\u7ea7\u76f8\u5bf9\u5bfc\u5165<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u7ea7\u76ee\u5f55\u7ed3\u6784\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ea7\u76f8\u5bf9\u5bfc\u5165\uff0c\u901a\u8fc7\u591a\u4e2a\u70b9\u53f7\u8868\u793a\u4e0a\u7ea7\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># project\/<\/p>\n<h2><strong>\u251c\u2500\u2500 package1\/<\/strong><\/h2>\n<h2><strong>\u2502   \u251c\u2500\u2500 __init__.py<\/strong><\/h2>\n<h2><strong>\u2502   \u251c\u2500\u2500 subpackage\/<\/strong><\/h2>\n<h2><strong>\u2502   \u2502   \u251c\u2500\u2500 __init__.py<\/strong><\/h2>\n<h2><strong>\u2502   \u2502   \u2514\u2500\u2500 file1.py<\/strong><\/h2>\n<h2><strong>\u2502   \u2514\u2500\u2500 file2.py<\/strong><\/h2>\n<h2><strong>\u2514\u2500\u2500 package2\/<\/strong><\/h2>\n<h2><strong>    \u251c\u2500\u2500 __init__.py<\/strong><\/h2>\n<h2><strong>    \u2514\u2500\u2500 file3.py<\/strong><\/h2>\n<h2><strong>package1\/subpackage\/file1.py<\/strong><\/h2>\n<p>def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<h2><strong>package1\/file2.py<\/strong><\/h2>\n<p>from .subpackage.file1 import greet<\/p>\n<p>print(greet(&quot;Alice&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cfile2.py\u901a\u8fc7from .subpackage.file1 import greet\u8fdb\u884c\u591a\u7ea7\u76f8\u5bf9\u5bfc\u5165\uff0c\u5f15\u5165\u4e86subpackage\u5305\u4e2d\u7684file1\u6a21\u5757\u4e2d\u7684greet\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u52a8\u6001\u5bfc\u5165<\/h2>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528__import__\u51fd\u6570<\/h3>\n<\/p>\n<p><p>__import__\u51fd\u6570\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u3002\u8fd9\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\u5f53\u6a21\u5757\u540d\u5728\u8fd0\u884c\u65f6\u624d\u786e\u5b9a\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># file1.py<\/p>\n<p>def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<h2><strong>file2.py<\/strong><\/h2>\n<p>module_name = &#39;file1&#39;<\/p>\n<p>module = __import__(module_name)<\/p>\n<p>print(module.greet(&quot;Alice&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cfile2.py\u901a\u8fc7__import__(&#39;file1&#39;)\u52a8\u6001\u5bfc\u5165\u4e86file1\u6a21\u5757\uff0c\u5e76\u8c03\u7528\u4e86greet\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528importlib\u6a21\u5757<\/h3>\n<\/p>\n<p><p>importlib\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u4e3a\u9ad8\u7ea7\u7684\u52a8\u6001\u5bfc\u5165\u529f\u80fd\uff0c\u53ef\u4ee5\u901a\u8fc7import_module\u51fd\u6570\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># file1.py<\/p>\n<p>def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<h2><strong>file2.py<\/strong><\/h2>\n<p>import importlib<\/p>\n<p>module_name = &#39;file1&#39;<\/p>\n<p>module = importlib.import_module(module_name)<\/p>\n<p>print(module.greet(&quot;Alice&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cfile2.py\u901a\u8fc7importlib.import_module(&#39;file1&#39;)\u52a8\u6001\u5bfc\u5165\u4e86file1\u6a21\u5757\uff0c\u5e76\u8c03\u7528\u4e86greet\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u8de8\u6a21\u5757\u5171\u4eab\u6570\u636e<\/h2>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528\u5168\u5c40\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u5728\u591a\u4e2a\u6a21\u5757\u4e4b\u95f4\u5171\u4eab\u6570\u636e\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\u5b9a\u4e49\u5168\u5c40\u53d8\u91cf\uff0c\u5e76\u5728\u5176\u4ed6\u6a21\u5757\u4e2d\u5f15\u5165\u8be5\u6a21\u5757\u6765\u5b9e\u73b0\u6570\u636e\u5171\u4eab\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># config.py<\/p>\n<p>shared_data = {}<\/p>\n<h2><strong>file1.py<\/strong><\/h2>\n<p>import config<\/p>\n<p>config.shared_data[&#39;key&#39;] = &#39;value&#39;<\/p>\n<h2><strong>file2.py<\/strong><\/h2>\n<p>import config<\/p>\n<p>print(config.shared_data[&#39;key&#39;])  # \u8f93\u51fa: value<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cconfig.py\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5168\u5c40\u53d8\u91cfshared_data\uff0cfile1.py\u548cfile2.py\u901a\u8fc7\u5f15\u5165config\u6a21\u5757\u5b9e\u73b0\u4e86\u6570\u636e\u5171\u4eab\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f<\/h3>\n<\/p>\n<p><p>\u5355\u4f8b\u6a21\u5f0f\u662f\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u4fdd\u8bc1\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5168\u5c40\u8bbf\u95ee\u70b9\u3002\u53ef\u4ee5\u901a\u8fc7\u5355\u4f8b\u6a21\u5f0f\u5728\u591a\u4e2a\u6a21\u5757\u4e4b\u95f4\u5171\u4eab\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># singleton.py<\/p>\n<p>class Singleton:<\/p>\n<p>    _instance = None<\/p>\n<p>    def __new__(cls, *args, kwargs):<\/p>\n<p>        if not cls._instance:<\/p>\n<p>            cls._instance = super().__new__(cls, *args, kwargs)<\/p>\n<p>        return cls._instance<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.data = {}<\/p>\n<h2><strong>file1.py<\/strong><\/h2>\n<p>from singleton import Singleton<\/p>\n<p>singleton = Singleton()<\/p>\n<p>singleton.data[&#39;key&#39;] = &#39;value&#39;<\/p>\n<h2><strong>file2.py<\/strong><\/h2>\n<p>from singleton import Singleton<\/p>\n<p>singleton = Singleton()<\/p>\n<p>print(singleton.data[&#39;key&#39;])  # \u8f93\u51fa: value<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0csingleton.py\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5355\u4f8b\u7c7bSingleton\uff0cfile1.py\u548cfile2.py\u901a\u8fc7\u5f15\u5165Singleton\u7c7b\u5b9e\u73b0\u4e86\u6570\u636e\u5171\u4eab\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5404\u79cd\u65b9\u6cd5\uff0cPython\u6587\u4ef6\u53ef\u4ee5\u5b9e\u73b0\u76f8\u4e92\u8c03\u7528\uff0c\u4ece\u800c\u6784\u5efa\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u6a21\u5757\u4e4b\u95f4\u7684\u76f8\u4e92\u8c03\u7528\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u76f8\u4e92\u8c03\u7528\u6a21\u5757\u901a\u5e38\u901a\u8fc7\u5bfc\u5165\uff08import\uff09\u5b9e\u73b0\u3002\u4f60\u53ef\u4ee5\u5c06\u9700\u8981\u5171\u4eab\u7684\u529f\u80fd\u6216\u7c7b\u5b9a\u4e49\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\uff0c\u7136\u540e\u5728\u53e6\u4e00\u4e2a\u6a21\u5757\u4e2d\u901a\u8fc7<code>import<\/code>\u8bed\u53e5\u5f15\u5165\u5b83\u3002\u786e\u4fdd\u6a21\u5757\u7684\u8def\u5f84\u6b63\u786e\uff0c\u907f\u514d\u5faa\u73af\u5bfc\u5165\u7684\u95ee\u9898\u3002\u4f7f\u7528\u7edd\u5bf9\u5bfc\u5165\u6216\u76f8\u5bf9\u5bfc\u5165\u90fd\u53ef\u4ee5\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u9879\u76ee\u7684\u7ed3\u6784\u548c\u9700\u6c42\u3002<\/p>\n<p><strong>\u5982\u679c\u51fa\u73b0\u5faa\u73af\u5bfc\u5165\u7684\u95ee\u9898\uff0c\u5e94\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5faa\u73af\u5bfc\u5165\u662f\u6307\u4e24\u4e2a\u6a21\u5757\u76f8\u4e92\u5bfc\u5165\u5bf9\u65b9\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u9519\u8bef\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u8003\u8651\u5c06\u5171\u4eab\u7684\u529f\u80fd\u63d0\u53d6\u5230\u4e00\u4e2a\u72ec\u7acb\u7684\u6a21\u5757\u4e2d\uff0c\u6216\u8005\u5728\u6a21\u5757\u5185\u90e8\u8fdb\u884c\u5ef6\u8fdf\u5bfc\u5165\uff08\u5373\u5728\u51fd\u6570\u5185\u90e8\u5bfc\u5165\u6240\u9700\u7684\u6a21\u5757\uff09\uff0c\u8fd9\u6837\u53ef\u4ee5\u51cf\u5c11\u76f8\u4e92\u4f9d\u8d56\u7684\u98ce\u9669\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u8c03\u8bd5\u6a21\u5757\u4e4b\u95f4\u7684\u8c03\u7528\u95ee\u9898\uff1f<\/strong><br \/>\u8c03\u8bd5\u6a21\u5757\u4e4b\u95f4\u7684\u8c03\u7528\u95ee\u9898\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>print()<\/code>\u8bed\u53e5\u6216\u65e5\u5fd7\u5e93\uff08\u5982<code>logging<\/code>\uff09\u6765\u8ffd\u8e2a\u6bcf\u4e2a\u6a21\u5757\u7684\u6267\u884c\u6d41\u7a0b\u3002\u540c\u65f6\uff0c\u786e\u4fdd\u5728\u8c03\u7528\u51fd\u6570\u6216\u7c7b\u65f6\uff0c\u4f20\u9012\u7684\u53c2\u6570\u6b63\u786e\u3002\u4f7f\u7528Python\u7684\u5185\u7f6e\u8c03\u8bd5\u5de5\u5177\uff08\u5982<code>pdb<\/code>\uff09\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u4f60\u9010\u6b65\u8ddf\u8e2a\u4ee3\u7801\u6267\u884c\uff0c\u627e\u5230\u6f5c\u5728\u7684\u9519\u8bef\u6e90\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6587\u4ef6\u4e4b\u95f4\u7684\u76f8\u4e92\u8c03\u7528\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u4f7f\u7528import\u3001\u4f7f\u7528from&#8230;imp [&hellip;]","protected":false},"author":3,"featured_media":1171361,"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\/1171353"}],"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=1171353"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1171353\/revisions"}],"predecessor-version":[{"id":1171363,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1171353\/revisions\/1171363"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1171361"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1171353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1171353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1171353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}