{"id":1133993,"date":"2025-01-08T21:11:56","date_gmt":"2025-01-08T13:11:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1133993.html"},"modified":"2025-01-08T21:12:01","modified_gmt":"2025-01-08T13:12:01","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e5%85%b6%e4%bb%96%e8%b7%af%e5%be%84%e7%9a%84%e6%a8%a1%e5%9d%97%e5%af%bc%e5%85%a5python%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1133993.html","title":{"rendered":"\u5982\u4f55\u5c06\u5176\u4ed6\u8def\u5f84\u7684\u6a21\u5757\u5bfc\u5165python\u4e2d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103056\/3369f00a-f148-42ae-a04a-87e22694f92b.webp\" alt=\"\u5982\u4f55\u5c06\u5176\u4ed6\u8def\u5f84\u7684\u6a21\u5757\u5bfc\u5165python\u4e2d\" \/><\/p>\n<p><p> <strong>\u8981\u5c06\u5176\u4ed6\u8def\u5f84\u7684\u6a21\u5757\u5bfc\u5165Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528sys.path\u3001importlib\u6a21\u5757\u3001\u8bbe\u7f6ePYTHONPATH\u73af\u5883\u53d8\u91cf\u3001\u521b\u5efa.pth\u6587\u4ef6\u7b49\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u8ba9Python\u89e3\u91ca\u5668\u8bc6\u522b\u548c\u5bfc\u5165\u4e0d\u5728\u9ed8\u8ba4\u641c\u7d22\u8def\u5f84\u4e2d\u7684\u6a21\u5757\u6216\u5305\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528sys.path.append()\u662f\u6700\u5e38\u7528\u548c\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u89e3\u91ca\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528sys.path.append()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u7684sys\u6a21\u5757\u63d0\u4f9b\u4e86\u8bbf\u95ee\u89e3\u91ca\u5668\u53d8\u91cf\u548c\u51fd\u6570\u7684\u63a5\u53e3\uff0csys.path\u662f\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u4e32\u7684\u5217\u8868\uff0c\u8fd9\u4e9b\u5b57\u7b26\u4e32\u8868\u793a\u89e3\u91ca\u5668\u7684\u641c\u7d22\u8def\u5f84\u3002\u901a\u8fc7\u5728sys.path\u4e2d\u6dfb\u52a0\u65b0\u8def\u5f84\uff0c\u53ef\u4ee5\u8ba9Python\u89e3\u91ca\u5668\u8bc6\u522b\u8be5\u8def\u5f84\u4e0b\u7684\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h4>1. \u6dfb\u52a0\u65b0\u8def\u5f84<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>sys.path.append(&#39;\/path\/to\/your\/module&#39;)<\/p>\n<p>import your_module<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f18\u70b9\u4e0e\u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u6613\u7528\u4e14\u4e0d\u9700\u8981\u4fee\u6539\u7cfb\u7edf\u914d\u7f6e\uff0c\u9002\u5408\u4e34\u65f6\u6027\u9700\u6c42\u3002\u4f46\u8fd9\u79cd\u65b9\u5f0f\u53ea\u5728\u5f53\u524d\u4f1a\u8bdd\u4e2d\u6709\u6548\uff0c\u5173\u95ed\u4f1a\u8bdd\u540e\u9700\u8981\u91cd\u65b0\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528importlib\u6a21\u5757<\/h3>\n<\/p>\n<p><p>importlib\u6a21\u5757\u662fPython 3.1\u5f15\u5165\u7684\uff0c\u7528\u4e8e\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u3002\u901a\u8fc7importlib\uff0c\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u63a7\u5236\u6a21\u5757\u5bfc\u5165\u3002<\/p>\n<\/p>\n<p><h4>1. \u52a8\u6001\u5bfc\u5165<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import importlib.util<\/p>\n<p>import sys<\/p>\n<p>import os<\/p>\n<p>module_name = &#39;your_module&#39;<\/p>\n<p>module_path = &#39;\/path\/to\/your\/module.py&#39;<\/p>\n<p>spec = importlib.util.spec_from_file_location(module_name, module_path)<\/p>\n<p>module = importlib.util.module_from_spec(spec)<\/p>\n<p>sys.modules[module_name] = module<\/p>\n<p>spec.loader.exec_module(module)<\/p>\n<h2><strong>\u4f7f\u7528\u6a21\u5757<\/strong><\/h2>\n<p>module.your_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f18\u70b9\u4e0e\u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<p><p>importlib\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u548c\u590d\u6742\u7684\u6a21\u5757\u5bfc\u5165\u65b9\u5f0f\uff0c\u9002\u5408\u9700\u8981\u52a8\u6001\u52a0\u8f7d\u6a21\u5757\u7684\u573a\u666f\u3002\u4f46\u5176\u590d\u6742\u6027\u4e5f\u8f83\u9ad8\uff0c\u9700\u8981\u5bf9\u6a21\u5757\u8def\u5f84\u548c\u540d\u79f0\u6709\u8be6\u7ec6\u4e86\u89e3\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u8bbe\u7f6ePYTHONPATH\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6ePYTHONPATH\u73af\u5883\u53d8\u91cf\uff0c\u53ef\u4ee5\u6c38\u4e45\u6027\u5730\u5c06\u6a21\u5757\u8def\u5f84\u6dfb\u52a0\u5230Python\u89e3\u91ca\u5668\u7684\u641c\u7d22\u8def\u5f84\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/h4>\n<\/p>\n<p><p><strong>Linux\/MacOS:<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u7ec8\u7aef\u4e2d\u6267\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export PYTHONPATH=$PYTHONPATH:\/path\/to\/your\/module<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>Windows:<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u8bbe\u7f6e\u4e2d\u6dfb\u52a0PYTHONPATH\u53d8\u91cf\uff0c\u5e76\u8bbe\u7f6e\u5176\u503c\u4e3a\u6a21\u5757\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f18\u70b9\u4e0e\u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u6c38\u4e45\u6709\u6548\uff0c\u9002\u5408\u9700\u8981\u957f\u671f\u4f7f\u7528\u7684\u6a21\u5757\u8def\u5f84\u3002\u4f46\u4fee\u6539\u73af\u5883\u53d8\u91cf\u9700\u8981\u7ba1\u7406\u5458\u6743\u9650\uff0c\u4e14\u5bf9\u7cfb\u7edf\u5176\u4ed6Python\u9879\u76ee\u53ef\u80fd\u4ea7\u751f\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u521b\u5efa.pth\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728Python\u7684site-packages\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a.pth\u6587\u4ef6\uff0c\u53ef\u4ee5\u5c06\u65b0\u7684\u8def\u5f84\u6c38\u4e45\u6dfb\u52a0\u5230\u89e3\u91ca\u5668\u7684\u641c\u7d22\u8def\u5f84\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa.pth\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728site-packages\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\uff0c\u5982<code>my_module.pth<\/code>\uff0c\u6587\u4ef6\u5185\u5bb9\u4e3a\u6a21\u5757\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-plaintext\">\/path\/to\/your\/module<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f18\u70b9\u4e0e\u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u6c38\u4e45\u6709\u6548\u4e14\u4e0d\u5f71\u54cd\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\uff0c\u9002\u5408\u9700\u8981\u957f\u671f\u4f7f\u7528\u7684\u6a21\u5757\u8def\u5f84\u3002\u4f46\u9700\u8981\u627e\u5230\u6b63\u786e\u7684site-packages\u76ee\u5f55\uff0c\u4e14\u5bf9\u65b0\u624b\u6765\u8bf4\u53ef\u80fd\u4e0d\u592a\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u5bfc\u5165<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5305\u5185\u6a21\u5757\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u5bfc\u5165\uff0c\u9002\u5408\u5927\u578b\u9879\u76ee\u7684\u6a21\u5757\u7ec4\u7ec7\u3002<\/p>\n<\/p>\n<p><h4>1. \u76f8\u5bf9\u8def\u5f84\u5bfc\u5165<\/h4>\n<\/p>\n<p><p>\u5728\u5305\u5185\u6a21\u5757\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u70b9\u53f7\u8868\u793a\u76f8\u5bf9\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from . import sibling_module<\/p>\n<p>from ..parent_package import parent_module<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f18\u70b9\u4e0e\u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<p><p>\u76f8\u5bf9\u8def\u5f84\u5bfc\u5165\u9002\u5408\u5305\u5185\u6a21\u5757\u7684\u7ec4\u7ec7\u548c\u7ba1\u7406\uff0c\u4f46\u4e0d\u80fd\u7528\u4e8e\u5305\u5916\u6a21\u5757\uff0c\u4e14\u9700\u8981\u4e86\u89e3\u5305\u7684\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5c06\u5176\u4ed6\u8def\u5f84\u7684\u6a21\u5757\u5bfc\u5165Python\u4e2d\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002<strong>\u4f7f\u7528sys.path.append()\u65b9\u6cd5\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u4e34\u65f6\u9700\u6c42\uff1bimportlib\u6a21\u5757\u63d0\u4f9b\u52a8\u6001\u5bfc\u5165\u529f\u80fd\uff0c\u9002\u5408\u9700\u8981\u7075\u6d3b\u63a7\u5236\u7684\u573a\u666f\uff1b\u8bbe\u7f6ePYTHONPATH\u73af\u5883\u53d8\u91cf\u548c\u521b\u5efa.pth\u6587\u4ef6\u9002\u5408\u957f\u671f\u4f7f\u7528\u7684\u6a21\u5757\u8def\u5f84\uff1b\u76f8\u5bf9\u8def\u5f84\u5bfc\u5165\u9002\u5408\u5305\u5185\u6a21\u5757\u7684\u7ec4\u7ec7\u548c\u7ba1\u7406\u3002<\/strong> \u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u89e3\u51b3\u6a21\u5757\u5bfc\u5165\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u5bfc\u5165\u975e\u9ed8\u8ba4\u8def\u5f84\u7684\u6a21\u5757\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5bfc\u5165\u975e\u9ed8\u8ba4\u8def\u5f84\u7684\u6a21\u5757\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539<code>sys.path<\/code>\u6765\u5b9e\u73b0\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u9996\u5148\uff0c\u4f7f\u7528<code>import sys<\/code>\u5bfc\u5165sys\u6a21\u5757\uff0c\u7136\u540e\u5c06\u5e0c\u671b\u6dfb\u52a0\u7684\u8def\u5f84\u901a\u8fc7<code>sys.path.append(&#39;\u4f60\u7684\u8def\u5f84&#39;)<\/code>\u6dfb\u52a0\u5230\u7cfb\u7edf\u8def\u5f84\u4e2d\uff0c\u6700\u540e\u5c31\u53ef\u4ee5\u6b63\u5e38\u5bfc\u5165\u8be5\u8def\u5f84\u4e0b\u7684\u6a21\u5757\u4e86\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u6765\u8bbe\u7f6e\u6a21\u5757\u7684\u5bfc\u5165\u8def\u5f84\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<code>PYTHONPATH<\/code>\u6765\u6307\u5b9a\u989d\u5916\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84\u3002\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u8bbe\u7f6e\u8fd9\u4e2a\u73af\u5883\u53d8\u91cf\u540e\uff0cPython\u5728\u542f\u52a8\u65f6\u4f1a\u81ea\u52a8\u5c06\u8be5\u8def\u5f84\u52a0\u5165\u5230\u6a21\u5757\u641c\u7d22\u8def\u5f84\u4e2d\uff0c\u65b9\u4fbf\u60a8\u5728\u4e0d\u540c\u9879\u76ee\u4e2d\u4f7f\u7528\u4e0d\u540c\u7684\u6a21\u5757\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5bfc\u5165\u6a21\u5757\u65f6\u9047\u5230\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5982\u679c\u5728\u5bfc\u5165\u6a21\u5757\u65f6\u9047\u5230\u9519\u8bef\uff0c\u9996\u5148\u5e94\u68c0\u67e5\u8def\u5f84\u662f\u5426\u6b63\u786e\u3002\u786e\u4fdd\u8def\u5f84\u4e2d\u5305\u542b\u7684\u6a21\u5757\u6587\u4ef6\u5b58\u5728\u4e14\u540d\u79f0\u65e0\u8bef\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u6a21\u5757\u4e2d\u6ca1\u6709\u8bed\u6cd5\u9519\u8bef\u3002\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u884c\u8fd0\u884cPython\u5e76\u5c1d\u8bd5\u5bfc\u5165\u6a21\u5757\uff0c\u8fd9\u6837\u53ef\u4ee5\u5feb\u901f\u5b9a\u4f4d\u95ee\u9898\u6240\u5728\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06\u5176\u4ed6\u8def\u5f84\u7684\u6a21\u5757\u5bfc\u5165Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528sys.path\u3001importlib\u6a21\u5757\u3001\u8bbe\u7f6ePYTHONPAT [&hellip;]","protected":false},"author":3,"featured_media":1134007,"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\/1133993"}],"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=1133993"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133993\/revisions"}],"predecessor-version":[{"id":1134011,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133993\/revisions\/1134011"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1134007"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1133993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1133993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1133993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}