{"id":1102887,"date":"2025-01-08T16:04:37","date_gmt":"2025-01-08T08:04:37","guid":{"rendered":""},"modified":"2025-01-08T16:04:43","modified_gmt":"2025-01-08T08:04:43","slug":"python%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5%e6%a0%b9%e7%9b%ae%e5%bd%95%e6%96%87%e4%bb%b6%e5%a4%b9%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1102887.html","title":{"rendered":"python\u5982\u4f55\u5bfc\u5165\u6839\u76ee\u5f55\u6587\u4ef6\u5939\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064957\/ab5d7465-d052-4ed2-8d2c-0f198bf1d331.webp\" alt=\"python\u5982\u4f55\u5bfc\u5165\u6839\u76ee\u5f55\u6587\u4ef6\u5939\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u5bfc\u5165\u6839\u76ee\u5f55\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u3001\u4fee\u6539sys.path\u3001\u4f7f\u7528importlib\u7b49\u65b9\u6cd5<\/strong>\uff0c\u5176\u4e2d\u63a8\u8350\u7684\u65b9\u6cd5\u662f\u4fee\u6539sys.path\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u5bfc\u5165\u6839\u76ee\u5f55\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165<\/h3>\n<\/p>\n<p><p>\u76f8\u5bf9\u5bfc\u5165\u9002\u7528\u4e8e\u5728\u5305\u5185\u5bfc\u5165\u6a21\u5757\u6216\u6587\u4ef6\u3002\u5f53\u6587\u4ef6\u7ed3\u6784\u5982\u4e0b\u65f6\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>    \u251c\u2500\u2500 m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/p>\n<p>    \u2514\u2500\u2500 subdir\/<\/p>\n<p>        \u2514\u2500\u2500 mymodule.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>main.py<\/code>\u4e2d\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u6765\u5bfc\u5165<code>mymodule.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from subdir import mymodule<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f46\u9700\u8981\u6ce8\u610f\uff0c\u76f8\u5bf9\u5bfc\u5165\u9002\u7528\u4e8e\u5305\u5185\u5bfc\u5165\uff0c\u4e14\u5305\u5185\u9700\u8981\u6709<code>__init__.py<\/code>\u6587\u4ef6\u3002\u5982\u679c\u4f60\u5bfc\u5165\u7684\u662f\u6839\u76ee\u5f55\u7684\u6587\u4ef6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u53ef\u80fd\u4e0d\u9002\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4fee\u6539sys.path<\/h3>\n<\/p>\n<p><p>\u76f4\u63a5\u4fee\u6539<code>sys.path<\/code>\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u4f60\u52a8\u6001\u5730\u6dfb\u52a0\u641c\u7d22\u8def\u5f84\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u5728\u4efb\u610f\u4f4d\u7f6e\u5bfc\u5165\u6587\u4ef6\u3002\u5047\u8bbe\u6587\u4ef6\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>    \u251c\u2500\u2500 main.py<\/p>\n<p>    \u2514\u2500\u2500 mymodule.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>main.py<\/code>\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5bfc\u5165<code>mymodule<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>import os<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u7684\u76ee\u5f55<\/strong><\/h2>\n<p>current_directory = os.path.dirname(os.path.abspath(__file__))<\/p>\n<h2><strong>\u5c06\u6839\u76ee\u5f55\u6dfb\u52a0\u5230sys.path<\/strong><\/h2>\n<p>sys.path.append(current_directory)<\/p>\n<p>import mymodule<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u901a\u8fc7\u4fee\u6539<code>sys.path<\/code>\uff0c\u4f60\u53ef\u4ee5\u786e\u4fddPython\u89e3\u91ca\u5668\u80fd\u591f\u627e\u5230\u4f60\u9700\u8981\u7684\u6a21\u5757\u6216\u6587\u4ef6\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528importlib<\/h3>\n<\/p>\n<p><p><code>importlib<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u5bfc\u5165\u673a\u5236\uff0c\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u7684\u60c5\u51b5\u3002\u5047\u8bbe\u6587\u4ef6\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>    \u251c\u2500\u2500 main.py<\/p>\n<p>    \u2514\u2500\u2500 mymodule.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>main.py<\/code>\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5bfc\u5165<code>mymodule<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import importlib.util<\/p>\n<p>import sys<\/p>\n<h2><strong>\u6784\u9020\u6a21\u5757\u7684\u8def\u5f84<\/strong><\/h2>\n<p>module_name = &quot;mymodule&quot;<\/p>\n<p>module_path = &quot;.\/mymodule.py&quot;<\/p>\n<h2><strong>\u4f7f\u7528importlib\u52a0\u8f7d\u6a21\u5757<\/strong><\/h2>\n<p>spec = importlib.util.spec_from_file_location(module_name, module_path)<\/p>\n<p>mymodule = importlib.util.module_from_spec(spec)<\/p>\n<p>spec.loader.exec_module(mymodule)<\/p>\n<h2><strong>\u4f7f\u7528\u5bfc\u5165\u7684\u6a21\u5757<\/strong><\/h2>\n<p>mymodule.some_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>importlib<\/code>\u53ef\u4ee5\u907f\u514d\u76f4\u63a5\u4fee\u6539<code>sys.path<\/code>\uff0c\u63d0\u4f9b\u66f4\u7075\u6d3b\u7684\u5bfc\u5165\u673a\u5236\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5305\u88c5\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5bfc\u5165\u7684\u6587\u4ef6\u662f\u811a\u672c\u6587\u4ef6\uff08\u5982.py\u6587\u4ef6\uff09\uff0c\u4f60\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u5305\u88c5\u811a\u672c\u6765\u7b80\u5316\u5bfc\u5165\u3002\u5047\u8bbe\u6587\u4ef6\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>    \u251c\u2500\u2500 main.py<\/p>\n<p>    \u2514\u2500\u2500 scripts\/<\/p>\n<p>        \u2514\u2500\u2500 myscript.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>scripts\/myscript.py<\/code>\u4e2d\uff0c\u4f60\u53ef\u4ee5\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def main():<\/p>\n<p>    print(&quot;Hello from myscript&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728<code>main.py<\/code>\u4e2d\u5bfc\u5165\u5e76\u8c03\u7528<code>myscript<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>import os<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u7684\u76ee\u5f55<\/strong><\/h2>\n<p>current_directory = os.path.dirname(os.path.abspath(__file__))<\/p>\n<h2><strong>\u5c06scripts\u76ee\u5f55\u6dfb\u52a0\u5230sys.path<\/strong><\/h2>\n<p>sys.path.append(os.path.join(current_directory, &#39;scripts&#39;))<\/p>\n<p>import myscript<\/p>\n<p>myscript.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u578b\u9879\u76ee\uff0c\u5efa\u8bae\u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177\uff08\u5982<code>pip<\/code>\u6216<code>setuptools<\/code>\uff09\u6765\u7ba1\u7406\u6a21\u5757\u548c\u4f9d\u8d56\u9879\u3002\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u53ef\u5b89\u88c5\u7684\u5305\u6765\u7b80\u5316\u5bfc\u5165\u548c\u7ba1\u7406\u4f9d\u8d56\u9879\u3002\u5047\u8bbe\u6587\u4ef6\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>    \u251c\u2500\u2500 main.py<\/p>\n<p>    \u251c\u2500\u2500 mypackage\/<\/p>\n<p>    \u2502   \u251c\u2500\u2500 __init__.py<\/p>\n<p>    \u2502   \u2514\u2500\u2500 mymodule.py<\/p>\n<p>    \u2514\u2500\u2500 setup.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>setup.py<\/code>\u4e2d\u5b9a\u4e49\u5305\u7684\u8bbe\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from setuptools import setup, find_packages<\/p>\n<p>setup(<\/p>\n<p>    name=&quot;mypackage&quot;,<\/p>\n<p>    version=&quot;0.1&quot;,<\/p>\n<p>    packages=find_packages(),<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install -e .<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>main.py<\/code>\u4e2d\u5bfc\u5165<code>mypackage<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from mypackage import mymodule<\/p>\n<p>mymodule.some_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177\u53ef\u4ee5\u786e\u4fdd\u6a21\u5757\u548c\u4f9d\u8d56\u9879\u7684\u53ef\u91cd\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u5bfc\u5165\u6839\u76ee\u5f55\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u5728Python\u4e2d\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6700\u5e38\u89c1\u548c\u6700\u63a8\u8350\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u4fee\u6539<code>sys.path<\/code>\u6765\u5b9e\u73b0\u3002\u5176\u4ed6\u65b9\u6cd5\u5982\u76f8\u5bf9\u5bfc\u5165\u3001\u4f7f\u7528<code>importlib<\/code>\u3001\u7f16\u5199\u5305\u88c5\u811a\u672c\u548c\u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177\u4e5f\u5404\u6709\u5176\u9002\u7528\u573a\u666f\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u9879\u76ee\u7684\u89c4\u6a21\u3001\u590d\u6742\u5ea6\u548c\u5177\u4f53\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165\u6839\u76ee\u5f55\u4e0b\u7684\u7279\u5b9a\u6a21\u5757\u6216\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\u5bfc\u5165\u6839\u76ee\u5f55\u4e0b\u7684\u7279\u5b9a\u6a21\u5757\u6216\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u6216\u76f8\u5bf9\u8def\u5f84\u3002\u786e\u4fdd\u5728\u811a\u672c\u4e2d\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\uff0c\u5e76\u6839\u636e\u6587\u4ef6\u7684\u76f8\u5bf9\u4f4d\u7f6e\u6216\u7edd\u5bf9\u8def\u5f84\u8fdb\u884c\u5bfc\u5165\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6587\u4ef6\u4f4d\u4e8e\u6839\u76ee\u5f55\u4e0b\uff0c\u4f7f\u7528<code>from your_module import your_function<\/code>\u7684\u683c\u5f0f\u6765\u5bfc\u5165\u76f8\u5e94\u7684\u529f\u80fd\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8bbe\u7f6e\u6839\u76ee\u5f55\u4ee5\u4fbf\u4e8e\u5bfc\u5165\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u8bbe\u7f6e\u6839\u76ee\u5f55\u4ee5\u4fbf\u4e8e\u5bfc\u5165\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys.path<\/code>\u6765\u6dfb\u52a0\u6839\u76ee\u5f55\u5230Python\u7684\u641c\u7d22\u8def\u5f84\u3002\u5728\u4f60\u7684\u811a\u672c\u5f00\u5934\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import sys\nimport os\nsys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), &#39;..&#39;)))\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u786e\u4fddPython\u89e3\u91ca\u5668\u53ef\u4ee5\u627e\u5230\u6839\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u3002<\/p>\n<p><strong>\u9047\u5230\u5bfc\u5165\u9519\u8bef\u65f6\uff0c\u6211\u5e94\u8be5\u5982\u4f55\u6392\u67e5\u95ee\u9898\uff1f<\/strong><br \/>\u5982\u679c\u5728\u5bfc\u5165\u6839\u76ee\u5f55\u6587\u4ef6\u5939\u7684\u6587\u4ef6\u65f6\u9047\u5230\u9519\u8bef\uff0c\u9996\u5148\u68c0\u67e5\u6587\u4ef6\u8def\u5f84\u662f\u5426\u6b63\u786e\u3002\u786e\u8ba4\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u6587\u4ef6\u540d\u548c\u6a21\u5757\u540d\u662f\u5426\u62fc\u5199\u6b63\u786e\u3002\u8fd8\u8981\u786e\u4fdd\u6a21\u5757\u4e2d\u6ca1\u6709\u5faa\u73af\u5bfc\u5165\u7684\u60c5\u51b5\u3002\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u8c03\u8bd5\u4fe1\u606f\uff0c\u4f8b\u5982\u6253\u5370<code>sys.path<\/code>\u7684\u5185\u5bb9\uff0c\u6765\u5e2e\u52a9\u8bc6\u522b\u95ee\u9898\u6240\u5728\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u5bfc\u5165\u6839\u76ee\u5f55\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u3001\u4fee\u6539sys.path\u3001\u4f7f\u7528importlib\u7b49\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1102899,"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\/1102887"}],"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=1102887"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1102887\/revisions"}],"predecessor-version":[{"id":1102903,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1102887\/revisions\/1102903"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1102899"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1102887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1102887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1102887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}