{"id":988919,"date":"2024-12-27T08:06:49","date_gmt":"2024-12-27T00:06:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/988919.html"},"modified":"2024-12-27T08:06:51","modified_gmt":"2024-12-27T00:06:51","slug":"python%e5%a6%82%e4%bd%95%e6%94%b9%e5%8f%98%e4%bb%a3%e7%a0%81%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/988919.html","title":{"rendered":"python\u5982\u4f55\u6539\u53d8\u4ee3\u7801\u8def\u5f84"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064350\/dbaf2385-cf75-4bd6-9fc0-8ac121c12546.webp\" alt=\"python\u5982\u4f55\u6539\u53d8\u4ee3\u7801\u8def\u5f84\" \/><\/p>\n<blockquote>\n<p><p>search(&quot;python\u5982\u4f55\u6539\u53d8\u4ee3\u7801\u8def\u5f84&quot;)<\/p>\n<\/p>\n<\/blockquote>\n<p><p><strong>\u5728Python\u4e2d\uff0c\u6539\u53d8\u4ee3\u7801\u8def\u5f84\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u7684<code>chdir()<\/code>\u51fd\u6570\u4fee\u6539\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3001\u4f7f\u7528<code>sys<\/code>\u6a21\u5757\u7684<code>path<\/code>\u5217\u8868\u8c03\u6574\u6a21\u5757\u641c\u7d22\u8def\u5f84\u3001\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u7684<code>chdir()<\/code>\u51fd\u6570\u4fee\u6539\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong> \u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u7684<code>chdir()<\/code>\u51fd\u6570\u4fee\u6539\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>current_directory = os.getcwd()<\/p>\n<p>print(f&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55: {current_directory}&quot;)<\/p>\n<h2><strong>\u4fee\u6539\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>new_directory = &#39;\/path\/to\/new\/directory&#39;<\/p>\n<p>os.chdir(new_directory)<\/p>\n<p>print(f&quot;\u4fee\u6539\u540e\u7684\u5de5\u4f5c\u76ee\u5f55: {os.getcwd()}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u901a\u8fc7<code>os.getcwd()<\/code>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u7136\u540e\u4f7f\u7528<code>os.chdir(new_directory)<\/code>\u5c06\u5de5\u4f5c\u76ee\u5f55\u4fee\u6539\u4e3a\u6307\u5b9a\u7684\u8def\u5f84\u3002\u4fee\u6539\u540e\uff0c\u518d\u6b21\u8c03\u7528<code>os.getcwd()<\/code>\u9a8c\u8bc1\u5de5\u4f5c\u76ee\u5f55\u662f\u5426\u5df2\u6210\u529f\u66f4\u6539\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>sys<\/code>\u6a21\u5757\u7684<code>path<\/code>\u5217\u8868\u8c03\u6574\u6a21\u5757\u641c\u7d22\u8def\u5f84<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<h2><strong>\u67e5\u770b\u5f53\u524d\u6a21\u5757\u641c\u7d22\u8def\u5f84<\/strong><\/h2>\n<p>print(&quot;\u5f53\u524d\u6a21\u5757\u641c\u7d22\u8def\u5f84:&quot;)<\/p>\n<p>for path in sys.path:<\/p>\n<p>    print(path)<\/p>\n<h2><strong>\u6dfb\u52a0\u65b0\u7684\u641c\u7d22\u8def\u5f84<\/strong><\/h2>\n<p>new_path = &#39;\/path\/to\/module\/directory&#39;<\/p>\n<p>if new_path not in sys.path:<\/p>\n<p>    sys.path.append(new_path)<\/p>\n<p>    print(f&quot;\u5df2\u6dfb\u52a0\u65b0\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84: {new_path}&quot;)<\/p>\n<h2><strong>\u9a8c\u8bc1\u6dfb\u52a0\u7684\u8def\u5f84<\/strong><\/h2>\n<p>print(&quot;\u66f4\u65b0\u540e\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84:&quot;)<\/p>\n<p>for path in sys.path:<\/p>\n<p>    print(path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4fee\u6539<code>sys.path<\/code>\uff0c\u53ef\u4ee5\u52a8\u6001\u6dfb\u52a0\u65b0\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84\uff0c\u4f7fPython\u80fd\u591f\u627e\u5230\u4f4d\u4e8e\u4e0d\u540c\u76ee\u5f55\u4e0b\u7684\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c<\/strong>\uff1a<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u79fb\u690d\u6027\u3002\u76f8\u5bf9\u8def\u5f84\u662f\u76f8\u5bf9\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\uff0c\u56e0\u6b64\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e2d\u65e0\u9700\u4fee\u6539\u4ee3\u7801\u5373\u53ef\u6b63\u786e\u8bbf\u95ee\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5047\u8bbe\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u5305\u542b\u4e00\u4e2a\u540d\u4e3a&#39;subdir&#39;\u7684\u5b50\u76ee\u5f55\u548c\u4e00\u4e2a\u540d\u4e3a&#39;file.txt&#39;\u7684\u6587\u4ef6<\/strong><\/h2>\n<h2><strong>\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u6253\u5f00\u6587\u4ef6<\/strong><\/h2>\n<p>file_path = os.path.join(&#39;subdir&#39;, &#39;file.txt&#39;)<\/p>\n<p>with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>os.path.join()<\/code>\u7528\u4e8e\u6784\u5efa\u76f8\u5bf9\u8def\u5f84\uff0c\u7136\u540e\u4f7f\u7528\u8be5\u8def\u5f84\u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u8bfb\u53d6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u8def\u5f84\u7684\u6b63\u786e\u6027<\/strong>\uff1a\u786e\u4fdd\u6307\u5b9a\u7684\u8def\u5f84\u5b58\u5728\u4e14\u5177\u6709\u9002\u5f53\u7684\u6743\u9650\uff0c\u5426\u5219\u53ef\u80fd\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8de8\u5e73\u53f0\u517c\u5bb9\u6027<\/strong>\uff1a\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\u8868\u793a\u65b9\u5f0f\u53ef\u80fd\u4e0d\u540c\u3002\u4e3a\u63d0\u9ad8\u4ee3\u7801\u7684\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\uff0c\u5efa\u8bae\u4f7f\u7528<code>os.path<\/code>\u6216<code>pathlib<\/code>\u6a21\u5757\u6765\u5904\u7406\u8def\u5f84\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u76f8\u5bf9\u8def\u5f84\u7684\u57fa\u51c6<\/strong>\uff1a\u76f8\u5bf9\u8def\u5f84\u662f\u57fa\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\uff0c\u56e0\u6b64\u5728\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u65f6\uff0c\u786e\u4fdd\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u8bbe\u7f6e\u6b63\u786e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u7075\u6d3b\u5730\u6539\u53d8\u4ee3\u7801\u8def\u5f84\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u5f00\u53d1\u9700\u6c42\u3002 <\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6e\u6216\u66f4\u6539\u6a21\u5757\u7684\u641c\u7d22\u8def\u5f84\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys<\/code>\u6a21\u5757\u7684<code>sys.path<\/code>\u5217\u8868\u6765\u6539\u53d8\u6a21\u5757\u7684\u641c\u7d22\u8def\u5f84\u3002\u901a\u8fc7\u5c06\u65b0\u7684\u76ee\u5f55\u6dfb\u52a0\u5230\u8fd9\u4e2a\u5217\u8868\u4e2d\uff0c\u4f60\u53ef\u4ee5\u8ba9Python\u5728\u8fd9\u4e9b\u76ee\u5f55\u4e2d\u5bfb\u627e\u6a21\u5757\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import sys\nsys.path.append(&#39;\/path\/to\/your\/directory&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5c06\u6307\u5b9a\u7684\u76ee\u5f55\u6dfb\u52a0\u5230\u6a21\u5757\u641c\u7d22\u8def\u5f84\u4e2d\uff0c\u4ece\u800c\u4f7f\u5f97\u4f60\u53ef\u4ee5\u5bfc\u5165\u8be5\u76ee\u5f55\u4e0b\u7684\u6a21\u5757\u3002<\/p>\n<p><strong>\u5728Python\u9879\u76ee\u4e2d\u5982\u4f55\u5904\u7406\u76f8\u5bf9\u8def\u5f84\u548c\u7edd\u5bf9\u8def\u5f84\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5904\u7406\u8def\u5f84\u65f6\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u63d0\u4f9b\u7684\u529f\u80fd\u3002<code>os.path<\/code>\u5b50\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\uff0c\u5982<code>os.path.join()<\/code>\u548c<code>os.path.abspath()<\/code>\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u76f8\u5bf9\u8def\u5f84\u548c\u7edd\u5bf9\u8def\u5f84\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\ncurrent_path = os.path.abspath(os.path.dirname(__file__))\nnew_path = os.path.join(current_path, &#39;subdirectory&#39;, &#39;file.py&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u8def\u5f84\uff0c\u786e\u4fdd\u65e0\u8bba\u7a0b\u5e8f\u5728\u54ea\u91cc\u8fd0\u884c\uff0c\u8def\u5f84\u90fd\u662f\u6b63\u786e\u7684\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u6765\u8bbe\u7f6ePython\u4ee3\u7801\u7684\u8fd0\u884c\u8def\u5f84\uff1f<\/strong><br \/>\u901a\u8fc7\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u53ef\u4ee5\u5f71\u54cdPython\u7a0b\u5e8f\u7684\u8fd0\u884c\u8def\u5f84\u3002\u53ef\u4ee5\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u8bbe\u7f6e<code>PYTHONPATH<\/code>\u73af\u5883\u53d8\u91cf\uff0c\u6307\u5411\u4f60\u5e0c\u671bPython\u641c\u7d22\u6a21\u5757\u7684\u76ee\u5f55\u3002\u65b9\u6cd5\u56e0\u64cd\u4f5c\u7cfb\u7edf\u800c\u5f02\uff0c\u5728Linux\u548cMacOS\u4e2d\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u884c\u8bbe\u7f6e\uff0c\u5728Windows\u4e2d\u53ef\u4ee5\u5728\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4e2d\u8fdb\u884c\u914d\u7f6e\u3002\u8fd9\u5c06\u5141\u8bb8\u4f60\u5728\u8fd0\u884cPython\u7a0b\u5e8f\u65f6\u81ea\u52a8\u52a0\u8f7d\u6307\u5b9a\u8def\u5f84\u4e0b\u7684\u6a21\u5757\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"search(&quot;python\u5982\u4f55\u6539\u53d8\u4ee3\u7801\u8def\u5f84&quot;) \u5728Python\u4e2d\uff0c\u6539\u53d8\u4ee3\u7801\u8def\u5f84\u7684\u65b9\u6cd5\u5305\u62ec [&hellip;]","protected":false},"author":3,"featured_media":988932,"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\/988919"}],"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=988919"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/988919\/revisions"}],"predecessor-version":[{"id":988937,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/988919\/revisions\/988937"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/988932"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=988919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=988919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=988919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}