{"id":960770,"date":"2024-12-27T03:52:42","date_gmt":"2024-12-26T19:52:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/960770.html"},"modified":"2024-12-27T03:52:45","modified_gmt":"2024-12-26T19:52:45","slug":"%e5%a6%82%e4%bd%95import%e8%87%aa%e5%b7%b1%e7%9a%84python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/960770.html","title":{"rendered":"\u5982\u4f55import\u81ea\u5df1\u7684python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103212\/64c5c3c9-ef17-4fbb-bd98-a72f09f32a47.webp\" alt=\"\u5982\u4f55import\u81ea\u5df1\u7684python\" \/><\/p>\n<p><p> <strong>\u8981import\u81ea\u5df1\u7684Python\u6a21\u5757\uff0c\u9700\u8981\u786e\u4fdd\u6a21\u5757\u6587\u4ef6\u5728Python\u89e3\u91ca\u5668\u7684\u641c\u7d22\u8def\u5f84\u4e2d\u3001\u4f7f\u7528\u6b63\u786e\u7684import\u8bed\u6cd5\u3001\u53ef\u4ee5\u901a\u8fc7sys\u6a21\u5757\u52a8\u6001\u8c03\u6574\u641c\u7d22\u8def\u5f84\u3002<\/strong>\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u786e\u4fdd\u6a21\u5757\u6587\u4ef6\u5728Python\u89e3\u91ca\u5668\u7684\u641c\u7d22\u8def\u5f84\u4e2d<br \/>Python\u89e3\u91ca\u5668\u5728\u6267\u884cimport\u8bed\u53e5\u65f6\uff0c\u4f1a\u5728\u4e00\u7cfb\u5217\u6307\u5b9a\u76ee\u5f55\u4e2d\u641c\u7d22\u6a21\u5757\u6587\u4ef6\u3002\u8fd9\u4e9b\u76ee\u5f55\u88ab\u79f0\u4e3aPython\u7684\u641c\u7d22\u8def\u5f84\u3002\u8981import\u81ea\u5df1\u7684\u6a21\u5757\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u6a21\u5757\u6587\u4ef6\u4f4d\u4e8e\u8fd9\u4e9b\u76ee\u5f55\u4e2d\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<p><p>1.1\u3001\u4f7f\u7528sys\u6a21\u5757\u67e5\u770bPython\u641c\u7d22\u8def\u5f84<br \/>\u53ef\u4ee5\u901a\u8fc7Python\u5185\u7f6e\u7684sys\u6a21\u5757\u6765\u67e5\u770b\u5f53\u524d\u7684\u641c\u7d22\u8def\u5f84\u3002\u5728Python\u89e3\u91ca\u5668\u4e2d\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(sys.path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u663e\u793a\u4e86\u6240\u6709\u641c\u7d22\u8def\u5f84\u3002\u4f60\u53ef\u4ee5\u5c06\u81ea\u5df1\u7684\u6a21\u5757\u6587\u4ef6\u653e\u5728\u8fd9\u4e9b\u8def\u5f84\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u3002<\/p>\n<\/p>\n<p><p>1.2\u3001\u5c06\u6a21\u5757\u6587\u4ef6\u653e\u5728\u540c\u4e00\u76ee\u5f55\u4e0b<br \/>\u5982\u679c\u4f60\u6b63\u5728\u5f00\u53d1\u4e00\u4e2a\u9879\u76ee\uff0c\u53ef\u4ee5\u5c06\u6a21\u5757\u6587\u4ef6\u653e\u5728\u9879\u76ee\u7684\u4e3b\u76ee\u5f55\u4e2d\uff0c\u7136\u540e\u901a\u8fc7import\u8bed\u53e5\u76f4\u63a5\u5bfc\u5165\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u6b63\u786e\u7684import\u8bed\u6cd5<br \/>\u5728\u786e\u4fdd\u6a21\u5757\u6587\u4ef6\u5728\u641c\u7d22\u8def\u5f84\u4e2d\u540e\uff0c\u9700\u8981\u4f7f\u7528\u6b63\u786e\u7684import\u8bed\u6cd5\u6765\u5bfc\u5165\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p>2.1\u3001import\u8bed\u53e5<br \/>\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u662f\u4f7f\u7528import\u8bed\u53e5\u76f4\u63a5\u5bfc\u5165\u6a21\u5757\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u7684\u6a21\u5757\u540d\u4e3amy_module.py\uff0c\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import my_module<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2.2\u3001from&#8230;import\u8bed\u53e5<br \/>\u5982\u679c\u53ea\u9700\u8981\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u67d0\u4e2a\u51fd\u6570\u6216\u7c7b\uff0c\u53ef\u4ee5\u4f7f\u7528from&#8230;import\u8bed\u53e5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from my_module import my_function<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u53ea\u5bfc\u5165my_function\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u6574\u4e2a\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528sys\u6a21\u5757\u52a8\u6001\u8c03\u6574\u641c\u7d22\u8def\u5f84<br \/>\u6709\u65f6\u5019\uff0c\u6a21\u5757\u6587\u4ef6\u4e0d\u5728Python\u9ed8\u8ba4\u7684\u641c\u7d22\u8def\u5f84\u4e2d\u3002\u8fd9\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528sys\u6a21\u5757\u52a8\u6001\u8c03\u6574\u641c\u7d22\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>3.1\u3001\u6dfb\u52a0\u81ea\u5b9a\u4e49\u8def\u5f84<br \/>\u53ef\u4ee5\u901a\u8fc7sys.path.append()\u65b9\u6cd5\u5c06\u81ea\u5b9a\u4e49\u8def\u5f84\u6dfb\u52a0\u5230\u641c\u7d22\u8def\u5f84\u4e2d\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6a21\u5757\u6587\u4ef6\u4f4d\u4e8e\/home\/user\/my_project\u76ee\u5f55\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>sys.path.append(&#39;\/home\/user\/my_project&#39;)<\/p>\n<p>import my_module<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3.2\u3001\u4f7f\u7528\u73af\u5883\u53d8\u91cf<br \/>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6ePYTHONPATH\u73af\u5883\u53d8\u91cf\u6765\u6307\u5b9a\u989d\u5916\u7684\u641c\u7d22\u8def\u5f84\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u5bfc\u5165\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u6a21\u5757\u7ec4\u7ec7\u548c\u5305\u7ba1\u7406<br \/>\u5728\u5f00\u53d1\u590d\u6742\u9879\u76ee\u65f6\uff0c\u6a21\u5757\u7ec4\u7ec7\u548c\u5305\u7ba1\u7406\u975e\u5e38\u91cd\u8981\u3002\u901a\u8fc7\u5408\u7406\u7684\u6a21\u5757\u7ec4\u7ec7\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>4.1\u3001\u4f7f\u7528\u5305\u7ba1\u7406\u591a\u4e2a\u6a21\u5757<br \/>\u53ef\u4ee5\u5c06\u591a\u4e2a\u6a21\u5757\u7ec4\u7ec7\u6210\u4e00\u4e2a\u5305\u3002\u5305\u662f\u4e00\u4e2a\u5305\u542b__init__.py\u6587\u4ef6\u7684\u76ee\u5f55\uff0c<strong>init<\/strong>.py\u6587\u4ef6\u53ef\u4ee5\u662f\u7a7a\u7684\u3002\u901a\u8fc7\u4f7f\u7528\u5305\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7ec4\u7ec7\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4ee5\u4e0b\u9879\u76ee\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code>my_project\/<\/p>\n<p>    my_package\/<\/p>\n<p>        __init__.py<\/p>\n<p>        module1.py<\/p>\n<p>        module2.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from my_package import module1<\/p>\n<p>from my_package.module2 import some_function<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>4.2\u3001\u547d\u540d\u7a7a\u95f4\u5305<br \/>\u5728Python 3\u4e2d\uff0c\u5f15\u5165\u4e86\u547d\u540d\u7a7a\u95f4\u5305\u7684\u6982\u5ff5\uff0c\u53ef\u4ee5\u521b\u5efa\u4e0d\u5305\u542b__init__.py\u6587\u4ef6\u7684\u5305\u3002\u8fd9\u79cd\u65b9\u5f0f\u5bf9\u4e8e\u5927\u578b\u9879\u76ee\u7684\u6a21\u5757\u7ec4\u7ec7\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0c\u8981import\u81ea\u5df1\u7684Python\u6a21\u5757\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u6a21\u5757\u6587\u4ef6\u5728Python\u89e3\u91ca\u5668\u7684\u641c\u7d22\u8def\u5f84\u4e2d\uff1b\u7136\u540e\u4f7f\u7528\u6b63\u786e\u7684import\u8bed\u6cd5\u6765\u5bfc\u5165\u6a21\u5757\uff1b\u5982\u679c\u6a21\u5757\u6587\u4ef6\u4e0d\u5728\u9ed8\u8ba4\u641c\u7d22\u8def\u5f84\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528sys\u6a21\u5757\u52a8\u6001\u8c03\u6574\u641c\u7d22\u8def\u5f84\uff1b\u6700\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5305\u7ba1\u7406\u6765\u7ec4\u7ec7\u591a\u4e2a\u6a21\u5757\uff0c\u4ee5\u63d0\u9ad8\u9879\u76ee\u7684\u7ed3\u6784\u5316\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5e0c\u671b\u8fd9\u4e9b\u4fe1\u606f\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55import\u81ea\u5df1\u7684Python\u6a21\u5757\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06\u81ea\u5df1\u7684Python\u6a21\u5757\u5bfc\u5165\u5230\u9879\u76ee\u4e2d\uff1f<\/strong><br \/>\u5c06\u81ea\u5df1\u7684Python\u6a21\u5757\u5bfc\u5165\u5230\u9879\u76ee\u4e2d\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684\u6a21\u5757\u6587\u4ef6\uff08\u4f8b\u5982<code>mymodule.py<\/code>\uff09\u4f4d\u4e8e\u9879\u76ee\u7684\u5de5\u4f5c\u76ee\u5f55\u4e0b\u3002\u7136\u540e\uff0c\u5728\u4f60\u7684\u4e3b\u7a0b\u5e8f\u6587\u4ef6\u4e2d\uff0c\u4f7f\u7528<code>import mymodule<\/code>\u6765\u5bfc\u5165\u6a21\u5757\u3002\u5982\u679c\u4f60\u7684\u6a21\u5757\u5728\u5b50\u76ee\u5f55\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>from subdirectory import mymodule<\/code>\u7684\u5f62\u5f0f\u3002\u540c\u65f6\uff0c\u786e\u4fdd\u5b50\u76ee\u5f55\u4e2d\u6709\u4e00\u4e2a<code>__init__.py<\/code>\u6587\u4ef6\uff0c\u8fd9\u6837Python\u624d\u80fd\u8bc6\u522b\u8fd9\u4e2a\u76ee\u5f55\u4e3a\u4e00\u4e2a\u5305\u3002<\/p>\n<p><strong>\u5728\u5bfc\u5165\u81ea\u5df1\u7684Python\u6a21\u5757\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u5bfc\u5165\u81ea\u5df1\u7684Python\u6a21\u5757\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u6a21\u5757\u547d\u540d\u7684\u552f\u4e00\u6027\uff0c\u4ee5\u907f\u514d\u4e0e\u6807\u51c6\u5e93\u6216\u5176\u4ed6\u7b2c\u4e09\u65b9\u5e93\u53d1\u751f\u51b2\u7a81\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u6a21\u5757\u6587\u4ef6\u7684\u8def\u5f84\u5305\u542b\u5728Python\u7684\u641c\u7d22\u8def\u5f84\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>sys.path<\/code>\u6765\u67e5\u770b\u548c\u4fee\u6539\u641c\u7d22\u8def\u5f84\u3002\u6a21\u5757\u4e2d\u4e0d\u5e94\u6709\u8bed\u6cd5\u9519\u8bef\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u5bfc\u5165\u5931\u8d25\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u4e0d\u540c\u6587\u4ef6\u5939\u4e2d\u5bfc\u5165\u81ea\u5df1\u7684Python\u6a21\u5757\uff1f<\/strong><br \/>\u82e5\u8981\u5728\u4e0d\u540c\u6587\u4ef6\u5939\u4e2d\u5bfc\u5165\u81ea\u5df1\u7684Python\u6a21\u5757\uff0c\u53ef\u4ee5\u4f7f\u7528\u7edd\u5bf9\u5bfc\u5165\u6216\u76f8\u5bf9\u5bfc\u5165\u7684\u65b9\u5f0f\u3002\u7edd\u5bf9\u5bfc\u5165\u9700\u8981\u6307\u5b9a\u5b8c\u6574\u7684\u6a21\u5757\u8def\u5f84\uff0c\u6bd4\u5982<code>from package.subpackage import mymodule<\/code>\u3002\u76f8\u5bf9\u5bfc\u5165\u5219\u53ef\u4ee5\u901a\u8fc7<code>.<\/code>\u548c<code>..<\/code>\u8868\u793a\u5f53\u524d\u6587\u4ef6\u5939\u548c\u4e0a\u7ea7\u6587\u4ef6\u5939\uff0c\u5982<code>from .. import mymodule<\/code>\u3002\u786e\u4fdd\u8fd0\u884c\u811a\u672c\u7684\u5de5\u4f5c\u76ee\u5f55\u6b63\u786e\uff0c\u4ee5\u4fbfPython\u80fd\u627e\u5230\u76ee\u6807\u6a21\u5757\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981import\u81ea\u5df1\u7684Python\u6a21\u5757\uff0c\u9700\u8981\u786e\u4fdd\u6a21\u5757\u6587\u4ef6\u5728Python\u89e3\u91ca\u5668\u7684\u641c\u7d22\u8def\u5f84\u4e2d\u3001\u4f7f\u7528\u6b63\u786e\u7684import [&hellip;]","protected":false},"author":3,"featured_media":960782,"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\/960770"}],"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=960770"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/960770\/revisions"}],"predecessor-version":[{"id":960784,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/960770\/revisions\/960784"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/960782"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=960770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=960770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=960770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}