{"id":1054682,"date":"2024-12-31T14:44:24","date_gmt":"2024-12-31T06:44:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1054682.html"},"modified":"2024-12-31T14:44:41","modified_gmt":"2024-12-31T06:44:41","slug":"python%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5%e5%90%8c%e7%ba%a7%e7%9b%ae%e5%bd%95%e4%b8%8b%e7%9a%84%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1054682.html","title":{"rendered":"python\u5982\u4f55\u5bfc\u5165\u540c\u7ea7\u76ee\u5f55\u4e0b\u7684\u7c7b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/b60d888a-cbee-4cf4-b11e-f74426b6f384.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5bfc\u5165\u540c\u7ea7\u76ee\u5f55\u4e0b\u7684\u7c7b\" \/><\/p>\n<p><p> <strong>\u8981\u5728 Python \u4e2d\u5bfc\u5165\u540c\u7ea7\u76ee\u5f55\u4e0b\u7684\u7c7b\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u6216\u8005\u7edd\u5bf9\u5bfc\u5165\u3001\u786e\u4fdd\u76ee\u5f55\u4e2d\u5305\u542b <code>__init__.py<\/code> \u6587\u4ef6\u3001\u4f7f\u7528 sys.path \u8c03\u6574\u5bfc\u5165\u8def\u5f84<\/strong>\u3002\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 <code>import<\/code> \u5173\u952e\u5b57\u548c\u6a21\u5757\u8def\u5f84\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u89e3\u91ca\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 import \u5173\u952e\u5b57\u548c\u6a21\u5757\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>import<\/code> \u5173\u952e\u5b57\u548c\u6a21\u5757\u8def\u5f84\u6765\u5bfc\u5165\u540c\u7ea7\u76ee\u5f55\u4e2d\u7684\u7c7b\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u76ee\u5f55\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>\u2502<\/p>\n<p>\u251c\u2500\u2500 module1.py<\/p>\n<p>\u251c\u2500\u2500 module2.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>module1.py<\/strong>:<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.value = &quot;Hello from MyClass&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>module2.py<\/strong>:<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from module1 import MyClass<\/p>\n<p>obj = MyClass()<\/p>\n<p>print(obj.value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728 <code>module2.py<\/code> \u4e2d\u4f7f\u7528 <code>from module1 import MyClass<\/code> \u5bfc\u5165 <code>module1.py<\/code> \u4e2d\u7684 <code>MyClass<\/code> \u7c7b\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <strong>init<\/strong>.py \u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u5982\u679c\u5c06\u76ee\u5f55\u8f6c\u6362\u6210\u5305\uff0c\u53ef\u4ee5\u5728\u76ee\u5f55\u4e2d\u6dfb\u52a0 <code>__init__.py<\/code> \u6587\u4ef6\u3002\u8fd9\u6837\u53ef\u4ee5\u66f4\u597d\u5730\u7ec4\u7ec7\u4ee3\u7801\u5e76\u8fdb\u884c\u5bfc\u5165\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u76ee\u5f55\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>\u2502<\/p>\n<p>\u251c\u2500\u2500 __init__.py<\/p>\n<p>\u251c\u2500\u2500 module1.py<\/p>\n<p>\u251c\u2500\u2500 module2.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>module1.py<\/strong>:<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.value = &quot;Hello from MyClass&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>module2.py<\/strong>:<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from .module1 import MyClass<\/p>\n<p>obj = MyClass()<\/p>\n<p>print(obj.value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728 <code>module2.py<\/code> \u4e2d\u4f7f\u7528 <code>from .module1 import MyClass<\/code> \u8fdb\u884c\u76f8\u5bf9\u5bfc\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 sys.path \u8c03\u6574\u5bfc\u5165\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u52a8\u6001\u5730\u8c03\u6574 Python \u7684\u641c\u7d22\u8def\u5f84\uff0c\u4ee5\u4fbf\u5bfc\u5165\u540c\u7ea7\u76ee\u5f55\u4e2d\u7684\u7c7b\u3002\u53ef\u4ee5\u4f7f\u7528 <code>sys.path<\/code> \u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u76ee\u5f55\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>\u2502<\/p>\n<p>\u251c\u2500\u2500 module1.py<\/p>\n<p>\u251c\u2500\u2500 module2.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>module1.py<\/strong>:<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.value = &quot;Hello from MyClass&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>module2.py<\/strong>:<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>import os<\/p>\n<p>sys.path.append(os.path.dirname(os.path.abspath(__file__)))<\/p>\n<p>from module1 import MyClass<\/p>\n<p>obj = MyClass()<\/p>\n<p>print(obj.value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728 <code>module2.py<\/code> \u4e2d\u4f7f\u7528 <code>sys.path.append(os.path.dirname(os.path.abspath(__file__)))<\/code> \u6dfb\u52a0\u5f53\u524d\u76ee\u5f55\u5230 Python \u641c\u7d22\u8def\u5f84\uff0c\u7136\u540e\u4f7f\u7528 <code>from module1 import MyClass<\/code> \u5bfc\u5165 <code>MyClass<\/code> \u7c7b\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165<\/h3>\n<\/p>\n<p><p>\u76f8\u5bf9\u5bfc\u5165\u662f\u6307\u4f7f\u7528\u70b9\u53f7\uff08.\uff09\u6765\u8868\u793a\u5f53\u524d\u76ee\u5f55\u6216\u7236\u76ee\u5f55\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u76ee\u5f55\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>\u2502<\/p>\n<p>\u251c\u2500\u2500 module1.py<\/p>\n<p>\u251c\u2500\u2500 module2.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>module1.py<\/strong>:<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.value = &quot;Hello from MyClass&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>module2.py<\/strong>:<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from .module1 import MyClass<\/p>\n<p>obj = MyClass()<\/p>\n<p>print(obj.value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728 <code>module2.py<\/code> \u4e2d\u4f7f\u7528 <code>from .module1 import MyClass<\/code> \u8fdb\u884c\u76f8\u5bf9\u5bfc\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5e38\u89c1\u5bfc\u5165\u9519\u8bef\u53ca\u89e3\u51b3\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u5bfc\u5165\u540c\u7ea7\u76ee\u5f55\u4e0b\u7684\u7c7b\u65f6\uff0c\u60a8\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u9519\u8bef\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u9519\u8bef\u53ca\u5176\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>ModuleNotFoundError<\/strong>: \u786e\u4fdd\u5bfc\u5165\u8def\u5f84\u6b63\u786e\uff0c\u5e76\u786e\u4fdd\u76ee\u5f55\u4e2d\u5305\u542b <code>__init__.py<\/code> \u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>ImportError<\/strong>: \u786e\u4fdd\u6587\u4ef6\u540d\u548c\u7c7b\u540d\u62fc\u5199\u6b63\u786e\uff0c\u5e76\u786e\u4fdd\u6587\u4ef6\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>NameError<\/strong>: \u786e\u4fdd\u5bfc\u5165\u7684\u7c7b\u6216\u51fd\u6570\u540d\u79f0\u6b63\u786e\uff0c\u5e76\u786e\u4fdd\u6ca1\u6709\u62fc\u5199\u9519\u8bef\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5bfc\u5165\u540c\u7ea7\u76ee\u5f55\u4e0b\u7684\u7c7b\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528 <code>import<\/code> \u5173\u952e\u5b57\u548c\u6a21\u5757\u8def\u5f84\u3001\u786e\u4fdd\u76ee\u5f55\u4e2d\u5305\u542b <code>__init__.py<\/code> \u6587\u4ef6\u3001\u4f7f\u7528 <code>sys.path<\/code> \u8c03\u6574\u5bfc\u5165\u8def\u5f84\u548c\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u3002\u9009\u62e9\u9002\u5408\u60a8\u9879\u76ee\u7ed3\u6784\u548c\u9700\u6c42\u7684\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7ec4\u7ec7\u4ee3\u7801\u5e76\u8fdb\u884c\u5bfc\u5165\u3002\u5728\u5bfc\u5165\u540c\u7ea7\u76ee\u5f55\u4e0b\u7684\u7c7b\u65f6\uff0c\u786e\u4fdd\u8def\u5f84\u6b63\u786e\u3001\u6587\u4ef6\u540d\u548c\u7c7b\u540d\u62fc\u5199\u6b63\u786e\uff0c\u5e76\u907f\u514d\u5e38\u89c1\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5728 Python \u9879\u76ee\u4e2d\u5bfc\u5165\u540c\u7ea7\u76ee\u5f55\u4e0b\u7684\u7c7b\u5e76\u4f7f\u7528\u5b83\u4eec\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u8ba9\u60a8\u5728 Python \u9879\u76ee\u4e2d\u66f4\u9ad8\u6548\u5730\u8fdb\u884c\u6a21\u5757\u5bfc\u5165\u548c\u7ec4\u7ec7\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u9009\u62e9\u9002\u5408\u60a8\u9700\u6c42\u7684\u65b9\u6cd5\uff0c\u5e76\u786e\u4fdd\u4ee3\u7801\u7ec4\u7ec7\u7ed3\u6784\u6e05\u6670\uff0c\u80fd\u591f\u5927\u5927\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u7ef4\u62a4\u6027\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u60a8\u5728 Python \u7f16\u7a0b\u4e2d\u53d6\u5f97\u66f4\u5927\u7684\u8fdb\u5c55\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6b63\u786e\u5bfc\u5165\u540c\u7ea7\u76ee\u5f55\u4e0b\u7684\u6a21\u5757\u6216\u7c7b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5bfc\u5165\u540c\u7ea7\u76ee\u5f55\u4e0b\u7684\u6a21\u5757\u6216\u7c7b\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a<code>my_class.py<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u4e14\u5176\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>MyClass<\/code>\u7684\u7c7b\uff0c\u4f60\u53ef\u4ee5\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\u7684\u53e6\u4e00\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528\u4ee5\u4e0b\u65b9\u5f0f\u5bfc\u5165\u5b83\uff1a  <\/p>\n<pre><code class=\"language-python\">from my_class import MyClass\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u5f53\u524d\u6587\u4ef6\u4e2d\u4f7f\u7528<code>MyClass<\/code>\u7c7b\u4e86\u3002<\/p>\n<p><strong>\u5982\u679c\u540c\u7ea7\u76ee\u5f55\u4e0b\u6709\u591a\u4e2a\u6587\u4ef6\uff0c\u5982\u4f55\u7ba1\u7406\u548c\u5bfc\u5165\u7c7b\uff1f<\/strong><br \/>\u5728\u5904\u7406\u591a\u4e2a\u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u5305\u6765\u7ba1\u7406\u3002\u786e\u4fdd\u5728\u540c\u7ea7\u76ee\u5f55\u4e0b\u6709\u4e00\u4e2a<code>__init__.py<\/code>\u6587\u4ef6\uff08\u5373\u4f7f\u662f\u7a7a\u6587\u4ef6\uff09\uff0c\u8fd9\u6837Python\u4f1a\u5c06\u8be5\u76ee\u5f55\u89c6\u4e3a\u4e00\u4e2a\u5305\u3002\u7136\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u65b9\u5f0f\u5bfc\u5165\u7c7b\uff1a  <\/p>\n<pre><code class=\"language-python\">from .my_class import MyClass\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u6e05\u695a\u5730\u6307\u660e\u4f60\u662f\u4ece\u540c\u4e00\u4e2a\u5305\u4e2d\u5bfc\u5165\u7684\u7c7b\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528Jupyter Notebook\u65f6\uff0c\u5982\u4f55\u5bfc\u5165\u540c\u7ea7\u76ee\u5f55\u4e0b\u7684\u7c7b\uff1f<\/strong><br \/>\u5728Jupyter Notebook\u4e2d\u5bfc\u5165\u540c\u7ea7\u76ee\u5f55\u4e0b\u7684\u7c7b\u540c\u6837\u53ef\u4ee5\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u3002\u4e0d\u8fc7\uff0c\u6709\u65f6\u9700\u8981\u786e\u4fddNotebook\u7684\u5de5\u4f5c\u76ee\u5f55\u8bbe\u7f6e\u6b63\u786e\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u67e5\u770b\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\nprint(os.getcwd())\n<\/code><\/pre>\n<p>\u5982\u679c\u5f53\u524d\u76ee\u5f55\u4e0d\u662f\u5305\u542b\u4f60\u7684\u6a21\u5757\u7684\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.chdir()<\/code>\u6765\u66f4\u6539\u5de5\u4f5c\u76ee\u5f55\u3002\u5bfc\u5165\u65b9\u5f0f\u4e0e\u811a\u672c\u76f8\u540c\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">from my_class import MyClass\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5728Notebook\u7684\u540c\u7ea7\u76ee\u5f55\u4e0b\u6709<code>my_class.py<\/code>\u6587\u4ef6\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u987a\u5229\u5bfc\u5165\u7c7b\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728 Python \u4e2d\u5bfc\u5165\u540c\u7ea7\u76ee\u5f55\u4e0b\u7684\u7c7b\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u6216\u8005\u7edd\u5bf9\u5bfc\u5165\u3001\u786e\u4fdd\u76ee\u5f55\u4e2d\u5305\u542b __init__.py [&hellip;]","protected":false},"author":3,"featured_media":1054717,"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\/1054682"}],"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=1054682"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1054682\/revisions"}],"predecessor-version":[{"id":1054719,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1054682\/revisions\/1054719"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1054717"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1054682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1054682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1054682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}