{"id":173366,"date":"2024-05-08T18:10:09","date_gmt":"2024-05-08T10:10:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/173366.html"},"modified":"2024-05-08T18:10:18","modified_gmt":"2024-05-08T10:10:18","slug":"python%e4%b8%ad-from-import-xxx%e7%9a%84%e9%82%a3%e4%b8%aa%e7%82%b9%e6%98%af%e8%a1%a8%e7%a4%ba%e5%bd%93%e5%89%8d%e5%8c%85%e5%90%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/173366.html","title":{"rendered":"python\u4e2d from . import \u00d7\u00d7\u00d7\u7684\u90a3\u4e2a\u70b9\u662f\u8868\u793a\u5f53\u524d\u5305\u5417"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27045251\/855918fb-aac6-4cd5-893e-28b1ec6e75d1.webp\" alt=\"python\u4e2d from . import \u00d7\u00d7\u00d7\u7684\u90a3\u4e2a\u70b9\u662f\u8868\u793a\u5f53\u524d\u5305\u5417\" \/><\/p>\n<p><p><strong>\u5728Python\u4e2d\uff0c<code>from . import xxx<\/code>\u8bed\u53e5\u4e2d\u7684\u70b9\u53f7(<code>.<\/code>)\u4ee3\u8868\u5f53\u524d\u6a21\u5757\u6240\u5728\u7684\u5305\u76ee\u5f55<\/strong>\u3001\u56e0\u6b64\u8fd9\u79cd\u5bfc\u5165\u65b9\u5f0f\u88ab\u7528\u6765\u5728\u5305\u5185\u8fdb\u884c\u76f8\u5bf9\u5bfc\u5165\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5305\u7ed3\u6784\uff0c\u5e76\u4e14\u60f3\u8981\u4ece\u540c\u4e00\u5305\u4e2d\u7684\u53e6\u4e00\u4e2a\u5b50\u6a21\u5757\u5bfc\u5165\u4e00\u4e9b\u5185\u5bb9\u65f6\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u8fd9\u79cd\u8bed\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u8be6\u7ec6\u8bf4\u660e\u8fd9\u4e00\u5bfc\u5165\u673a\u5236\uff0c\u5047\u8bbe\u4f60\u6709\u5982\u4e0b\u7684\u5305\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code>mypackage\/<\/p>\n<p>    __init__.py<\/p>\n<p>    subpackage1\/<\/p>\n<p>        __init__.py<\/p>\n<p>        module1.py<\/p>\n<p>    subpackage2\/<\/p>\n<p>        __init__.py<\/p>\n<p>        module2.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5728<code>mypackage\/subpackage1\/module1.py<\/code>\u4e2d\uff0c\u60f3\u8981\u5bfc\u5165<code>mypackage\/subpackage2\/module2.py<\/code>\u6a21\u5757\u4e2d\u7684\u4e00\u4e9b\u7c7b\u6216\u51fd\u6570\uff0c\u53ef\u4ee5\u5728<code>module1.py<\/code>\u6587\u4ef6\u4e2d\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\uff0c\u50cf\u8fd9\u6837\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from . import module2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u7684\u70b9(<code>.<\/code>)\u8868\u793a\u5f53\u524d\u7684<code>subpackage1<\/code>\u76ee\u5f55\uff0c\u56e0\u6b64\u4f60\u76f8\u5bf9\u4e8e\u5f53\u524d\u4f4d\u7f6e\u5bfc\u5165\u4e86\u540c\u4e00\u5305\u4e0b\u7684\u5176\u4ed6\u6a21\u5757\uff0c\u5373<code>subpackage2<\/code>\u4e2d\u7684<code>module2.py<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u76f8\u5bf9\u5bfc\u5165\u7684\u4f7f\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>\u7528\u9014\u4e0e\u539f\u7406<\/h4>\n<\/p>\n<p><p>\u76f8\u5bf9\u5bfc\u5165\u7528\u4e8e\u5305\u5185\u90e8\u5404\u6a21\u5757\u4e4b\u95f4\u7684\u76f8\u4e92\u8c03\u7528\uff0c\u5b83\u53ef\u4ee5\u63d0\u9ad8\u6a21\u5757\u95f4\u7684\u534f\u4f5c\u6027\u548c\u5305\u7684\u5c01\u88c5\u6027\u3002\u76f8\u5bf9\u5bfc\u5165\u662f\u6839\u636e\u5f53\u524d\u6a21\u5757\u7684\u6587\u4ef6\u8def\u5f84\u5173\u7cfb\u6765\u5b9a\u4f4d\u76ee\u6807\u6a21\u5757\u7684\u4f4d\u7f6e\u3002\u73b0\u4ee3Python\u5f00\u53d1\u4e2d\uff0c\u76f8\u5bf9\u5bfc\u5165\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5305\u4e0e\u6a21\u5757\u7684\u7ed3\u6784\u7ec4\u7ec7\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u5305\u7ed3\u6784\u7684\u4f18\u70b9<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u7684\u5305\u7ed3\u6784\u7f16\u6392\u6709\u51e0\u4e2a\u4f18\u70b9\uff1a\u9996\u5148\uff0c\u8fd9\u4f7f\u5f97\u5305\u7684\u91cd\u6784\u66f4\u4e3a\u5bb9\u6613\uff0c\u56e0\u4e3a\u5185\u90e8\u5f15\u7528\u662f\u76f8\u5bf9\u7684\uff0c\u5f53\u79fb\u52a8\u6a21\u5757\u65f6\uff0c\u4e0d\u9700\u8981\u91cd\u65b0\u914d\u7f6e\u5168\u90e8\u8def\u5f84\u3002\u5176\u6b21\uff0c\u8fd9\u79cd\u7ed3\u6784\u6709\u5229\u4e8e\u521b\u5efa\u66f4\u597d\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u907f\u514d\u4e86\u6a21\u5757\u95f4\u7684\u547d\u540d\u51b2\u7a81\u3002\u6700\u540e\uff0c\u5b83\u53ef\u4ee5\u51cf\u5c11\u5bfc\u5165\u65f6\u7684\u4e00\u4e9b\u5faa\u73af\u4f9d\u8d56\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5982\u4f55\u6b63\u786e\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165<\/h3>\n<\/p>\n<p><h4>\u76f8\u5bf9\u5bfc\u5165\u7684\u8bed\u6cd5<\/h4>\n<\/p>\n<p><p>\u8981\u6b63\u786e\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\uff0c\u9996\u5148\u9700\u8981\u7406\u89e3\u5176\u7b80\u5355\u7684\u8bed\u6cd5\u3002<code>from . import module<\/code>\u662f\u6700\u57fa\u7840\u7684\u5f62\u5f0f\uff0c\u5176\u4e2d\u7684\u70b9\u53f7(<code>.<\/code>)\u5c31\u8868\u793a\u5f53\u524d\u6a21\u5757\u6240\u5728\u7684\u5305\u3002\u5982\u679c\u8981\u5411\u4e0a\u4e00\u7ea7\u5bfc\u5165\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e24\u4e2a\u70b9\u53f7(<code>from .. import module<\/code>)\uff0c\u6bcf\u589e\u52a0\u4e00\u4e2a\u70b9\u53f7\uff0c\u5c31\u5411\u4e0a\u79fb\u52a8\u4e00\u7ea7\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h4>\u907f\u514d\u9519\u8bef<\/h4>\n<\/p>\n<p><p>\u8981\u907f\u514d\u5728\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u65f6\u51fa\u73b0\u9519\u8bef\uff0c\u4f60\u5fc5\u987b\u786e\u4fdd\u4f60\u7684\u6a21\u5757\u662f\u4f5c\u4e3a\u5305\u7684\u4e00\u90e8\u5206\u88ab\u5bfc\u5165\u7684\u3002\u8fd9\u610f\u5473\u7740\uff0c\u4f60\u4e0d\u80fd\u76f4\u63a5\u8fd0\u884c\u4e00\u4e2a\u4f7f\u7528\u4e86\u76f8\u5bf9\u5bfc\u5165\u7684\u6a21\u5757\u6587\u4ef6\uff0c\u56e0\u4e3a\u76f8\u5bf9\u8def\u5f84\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u662f\u4e0d\u786e\u5b9a\u7684\u3002\u76f8\u53cd\uff0c\u4f60\u5e94\u8be5\u8fd0\u884c\u5305\u7684\u9876\u7ea7\u5165\u53e3\uff0c\u6216\u8005\u4f7f\u7528\u6a21\u5757\u7684\u5b8c\u5168\u9650\u5b9a\u540d\u6765\u8fdb\u884c\u5bfc\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u76f8\u5bf9\u5bfc\u5165\u4e0e\u7edd\u5bf9\u5bfc\u5165\u7684\u5bf9\u6bd4<\/h3>\n<\/p>\n<p><h4>\u4f18\u52bf\u4e0e\u4e0d\u8db3<\/h4>\n<\/p>\n<p><p>\u76f8\u5bf9\u5bfc\u5165\u7684\u4e00\u4e2a\u6838\u5fc3\u4f18\u70b9\u662f\u5b83\u652f\u6301\u5c01\u88c5\u548c\u91cd\u6784\u3002\u5b83\u5141\u8bb8\u6a21\u5757\u88ab\u91cd\u65b0\u7ec4\u7ec7\u5728\u5305\u5185\u4e0d\u540c\u4f4d\u7f6e\u800c\u4e0d\u9700\u8981\u4fee\u6539\u5bfc\u5165\u8bed\u53e5\u3002\u7136\u800c\uff0c\u76f8\u5bf9\u5bfc\u5165\u4e5f\u6709\u6f5c\u5728\u7684\u7f3a\u70b9\uff0c\u5305\u62ec\u8def\u5f84\u53ef\u80fd\u4e0d\u591f\u660e\u786e\uff0c\u96be\u4ee5\u4e00\u773c\u770b\u51fa\u5bfc\u5165\u7684\u6a21\u5757\u7a76\u7adf\u5728\u54ea\u91cc\uff0c\u5c24\u5176\u662f\u5728\u590d\u6742\u7684\u5305\u7ed3\u6784\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u7edd\u5bf9\u5bfc\u5165\u7684\u9009\u62e9<\/h4>\n<\/p>\n<p><p>\u4e0e\u76f8\u5bf9\u5bfc\u5165\u76f8\u5bf9\u5e94\u7684\u662f\u7edd\u5bf9\u5bfc\u5165\uff0c\u5b83\u4f7f\u7528\u5b8c\u6574\u7684\u5305\u8def\u5f84\u6765\u5bfc\u5165\u6a21\u5757\u3002\u7edd\u5bf9\u5bfc\u5165\u901a\u5e38\u88ab\u89c6\u4e3a\u66f4\u6e05\u6670\u660e\u4e86\uff0c\u6613\u4e8e\u7406\u89e3\u3002\u4e00\u4e2a\u597d\u7684\u5b9e\u8df5\u662f\u5728\u5305\u7684\u5916\u90e8\u4f7f\u7528\u7edd\u5bf9\u5bfc\u5165\uff0c\u5728\u5305\u7684\u5185\u90e8\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6700\u4f73\u5b9e\u8df5\u4e0e\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><h4>\u5305\u7684\u521d\u59cb\u5316<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u65f6\uff0c\u5fc5\u987b\u786e\u4fdd\u4f60\u7684\u5305\u6709<code>__init__.py<\/code>\u6587\u4ef6\u6765\u521d\u59cb\u5316\u5305\u3002\u8fd9\u4e2a\u6587\u4ef6\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u4f46\u5b83\u544a\u8bc9Python\u89e3\u91ca\u5668\u8fd9\u4e2a\u76ee\u5f55\u5e94\u88ab\u89c6\u4e3aPython\u5305\u3002<\/p>\n<\/p>\n<p><h4>\u5bfc\u5165\u7684\u7ec4\u7ec7<\/h4>\n<\/p>\n<p><p>\u4ee3\u7801\u7ec4\u7ec7\u826f\u597d\u7684\u9879\u76ee\u5f80\u5f80\u6709\u6e05\u6670\u7684\u5bfc\u5165\u903b\u8f91\u3002\u5f53\u4f60\u7684\u5305\u5f88\u5c0f\u65f6\uff0c\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\uff0c\u4f46\u968f\u7740\u5305\u7684\u589e\u957f\u548c\u590d\u6742\uff0c\u7ef4\u62a4\u76f8\u5bf9\u8def\u5f84\u53ef\u80fd\u4f1a\u53d8\u5f97\u56f0\u96be\uff0c\u6240\u4ee5\u9700\u8981\u6743\u8861\u9879\u76ee\u7684\u89c4\u6a21\u548c\u590d\u6742\u6027\u6765\u51b3\u5b9a\u5bfc\u5165\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u7edd\u5bf9\u5bfc\u5165\u7684\u573a\u666f<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9879\u76ee\u975e\u5e38\u5927\uff0c\u6216\u8005\u4f60\u6b63\u5728\u4e0e\u591a\u4e2a\u5305\u6253\u4ea4\u9053\uff0c\u90a3\u4e48\u901a\u5e38\u63a8\u8350\u4f7f\u7528\u5b8c\u5168\u9650\u5b9a\u7684\u7edd\u5bf9\u5bfc\u5165\u65b9\u5f0f\uff0c\u8fd9\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6e05\u6670\u5ea6\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h4>\u6a21\u5757\u7684\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u65f6\uff0c\u6a21\u5757\u7684\u5355\u5143\u6d4b\u8bd5\u53ef\u80fd\u4f1a\u53d8\u5f97\u66f4\u52a0\u590d\u6742\u3002\u4f60\u901a\u5e38\u9700\u8981\u5c06\u6d4b\u8bd5\u4ee3\u7801\u653e\u5728\u5305\u7684\u7ed3\u6784\u5185\uff0c\u6216\u8005\u8c03\u6574<code>sys.path<\/code>\u4ee5\u786e\u4fdd\u6d4b\u8bd5\u80fd\u591f\u6b63\u786e\u5730\u5bfc\u5165\u8981\u6d4b\u8bd5\u7684\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7406\u89e3\u548c\u6b63\u786e\u5e94\u7528<code>from . import xxx<\/code>\uff0c\u4f60\u80fd\u591f\u66f4\u597d\u5730\u7ba1\u7406\u4f60\u7684Python\u9879\u76ee\u7684\u5185\u90e8\u4f9d\u8d56\u5173\u7cfb\uff0c\u4f7f\u5176\u4fdd\u6301\u7075\u6d3b\u4e14\u6613\u4e8e\u7ef4\u62a4\u3002\u76f8\u5bf9\u5bfc\u5165\u662fPython\u5305\u5185\u6a21\u5757\u7ec4\u7ec7\u7684\u5173\u952e\u7279\u6027\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u5927\u5927\u63d0\u5347\u5de5\u7a0b\u9879\u76ee\u7684\u6a21\u5757\u5316\u548c\u53ef\u91cd\u7528\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. Python\u4e2d\u7684&quot;from . import xxx&quot;\u8bed\u53e5\u4e2d\u7684\u70b9\u8868\u793a\u4ec0\u4e48\u610f\u601d\uff1f<\/strong><br \/>\n\u5728\u8fd9\u4e2a\u8bed\u53e5\u4e2d\uff0c\u70b9\u8868\u793a\u5f53\u524d\u5305\uff0c\u4e5f\u5c31\u662f\u5f53\u524d\u6a21\u5757\u6240\u5728\u7684\u5305\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u6b63\u5728\u4ece\u5f53\u524d\u6a21\u5757\u6240\u5728\u7684\u5305\u4e2d\u5bfc\u5165\u9700\u8981\u7684\u6a21\u5757\u6216\u53d8\u91cf\u3002<\/p>\n<p><strong>2. \u5728Python\u4e2d\uff0c&quot;from . import xxx&quot;\u8bed\u53e5\u4e2d\u7684\u70b9\u6709\u4ec0\u4e48\u4f5c\u7528\uff1f<\/strong><br \/>\n\u70b9\u7684\u4f5c\u7528\u662f\u6307\u793aPython\u89e3\u91ca\u5668\u4ece\u5f53\u524d\u5305\u4e2d\u5bfc\u5165\u9700\u8981\u7684\u6a21\u5757\u6216\u53d8\u91cf\u3002\u8fd9\u79cd\u5bfc\u5165\u65b9\u5f0f\u5728\u5c06\u5305\u4f5c\u4e3a\u72ec\u7acb\u6a21\u5757\u4f7f\u7528\u65f6\u975e\u5e38\u6709\u7528\uff0c\u7279\u522b\u662f\u5728\u76f8\u5bf9\u5bfc\u5165\u65f6\u3002<\/p>\n<p><strong>3. Python\u4e2d\u7684&quot;from . import xxx&quot;\u8bed\u53e5\u4e2d\u7684\u70b9\u6709\u4ec0\u4e48\u7279\u6b8a\u7528\u9014\uff1f<\/strong><br \/>\n\u8fd9\u4e2a\u70b9\u7684\u7279\u6b8a\u7528\u9014\u5728\u4e8e\u76f8\u5bf9\u5bfc\u5165\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u6765\u5bfc\u5165\u4f4d\u4e8e\u540c\u4e00\u5305\u4e2d\u7684\u5176\u4ed6\u6a21\u5757\u6216\u53d8\u91cf\u3002\u76f8\u5bf9\u5bfc\u5165\u5bf9\u4e8e\u66f4\u597d\u5730\u7ec4\u7ec7\u5305\u7ed3\u6784\u4ee5\u53ca\u5728\u5927\u578b\u9879\u76ee\u4e2d\u7ba1\u7406\u6a21\u5757\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0cfrom . import xxx\u8bed\u53e5\u4e2d\u7684\u70b9\u53f7(.)\u4ee3\u8868\u5f53\u524d\u6a21\u5757\u6240\u5728\u7684\u5305\u76ee\u5f55\u3001\u56e0\u6b64\u8fd9\u79cd\u5bfc\u5165\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":173382,"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\/173366"}],"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=173366"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/173366\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/173382"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=173366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=173366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=173366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}