{"id":998289,"date":"2024-12-27T09:31:31","date_gmt":"2024-12-27T01:31:31","guid":{"rendered":""},"modified":"2024-12-27T09:31:36","modified_gmt":"2024-12-27T01:31:36","slug":"%e5%a6%82%e4%bd%95%e7%9f%a5%e9%81%93python%e7%b3%bb%e7%bb%9f%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/998289.html","title":{"rendered":"\u5982\u4f55\u77e5\u9053python\u7cfb\u7edf\u8def\u5f84"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073803\/06d5e70b-b1eb-4dd5-bdac-05d8d29f100e.webp\" alt=\"\u5982\u4f55\u77e5\u9053python\u7cfb\u7edf\u8def\u5f84\" \/><\/p>\n<p><h3>\u4e00\u3001\u83b7\u53d6Python\u7cfb\u7edf\u8def\u5f84\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u8981\u77e5\u9053Python\u7684\u7cfb\u7edf\u8def\u5f84\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<strong>\u4f7f\u7528sys\u6a21\u5757\u3001\u4f7f\u7528os\u6a21\u5757\u3001\u68c0\u67e5\u73af\u5883\u53d8\u91cf<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u662f\u4f7f\u7528sys\u6a21\u5757\u4e2d\u7684<code>sys.path<\/code>\u5c5e\u6027\u3002<code>sys.path<\/code>\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u4e86Python\u89e3\u91ca\u5668\u5728\u8fd0\u884c\u65f6\u641c\u7d22\u6a21\u5757\u7684\u8def\u5f84\u3002\u901a\u8fc7\u68c0\u67e5\u8fd9\u4e2a\u5217\u8868\uff0c\u4f60\u53ef\u4ee5\u786e\u5b9aPython\u5728\u5f53\u524d\u73af\u5883\u4e2d\u80fd\u591f\u8bbf\u95ee\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528sys\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>sys<\/code>\u6a21\u5757\u662fPython\u7684\u4e00\u4e2a\u5185\u5efa\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u5bf9\u89e3\u91ca\u5668\u76f8\u5173\u53d8\u91cf\u548c\u529f\u80fd\u7684\u8bbf\u95ee\u3002\u8981\u67e5\u770bPython\u7684\u7cfb\u7edf\u8def\u5f84\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys.path<\/code>\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\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>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u4e86Python\u5f53\u524d\u73af\u5883\u4e2d\u7684\u6240\u6709\u8def\u5f84\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4e9b\u8def\u5f84\u5305\u62ec\u6807\u51c6\u5e93\u8def\u5f84\u3001\u7b2c\u4e09\u65b9\u5e93\u8def\u5f84\u548c\u7528\u6237\u5b9a\u4e49\u7684\u6a21\u5757\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u8bf4\u660e\uff1a<\/strong><\/p>\n<\/p>\n<p><p><code>sys.path<\/code>\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u901a\u5e38\u662f\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\uff0c\u8868\u793a\u5f53\u524d\u76ee\u5f55\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5f53\u4f60\u5728\u5f53\u524d\u76ee\u5f55\u4e2d\u8fd0\u884c\u4e00\u4e2a\u811a\u672c\u65f6\uff0cPython\u4f1a\u9996\u5148\u5728\u8be5\u76ee\u5f55\u4e2d\u5bfb\u627e\u8981\u5bfc\u5165\u7684\u6a21\u5757\u3002\u6b64\u529f\u80fd\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u521b\u5efa\u6a21\u5757\uff0c\u800c\u65e0\u9700\u5c06\u5b83\u4eec\u5b89\u88c5\u5230\u5168\u5c40\u73af\u5883\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528os\u6a21\u5757\u83b7\u53d6Python\u8def\u5f84<\/h3>\n<\/p>\n<p><p><code>os<\/code>\u6a21\u5757\u662f\u53e6\u4e00\u4e2a\u7528\u4e8e\u5904\u7406\u64cd\u4f5c\u7cfb\u7edf\u529f\u80fd\u7684\u6807\u51c6\u5e93\u6a21\u5757\u3002\u867d\u7136\u5b83\u4e0d\u50cf<code>sys<\/code>\u6a21\u5757\u90a3\u6837\u76f4\u63a5\u63d0\u4f9bPython\u8def\u5f84\u4fe1\u606f\uff0c\u4f46\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u83b7\u53d6\u73af\u5883\u53d8\u91cf\uff0c\u8fd9\u4e5f\u662f\u4e00\u4e2a\u83b7\u53d6Python\u8def\u5f84\u7684\u91cd\u8981\u9014\u5f84\u3002<\/p>\n<\/p>\n<p><h4>1. \u83b7\u53d6\u73af\u5883\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7<code>os<\/code>\u6a21\u5757\u7684<code>os.environ<\/code>\u53ef\u4ee5\u8bbf\u95ee\u64cd\u4f5c\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u3002\u5bf9\u4e8ePython\u8def\u5f84\uff0c\u76f8\u5173\u7684\u73af\u5883\u53d8\u91cf\u901a\u5e38\u662f<code>PYTHONPATH<\/code>\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>python_path = os.environ.get(&#39;PYTHONPATH&#39;)<\/p>\n<p>print(python_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u6df1\u5165\u89e3\u6790\uff1a<\/strong><\/p>\n<\/p>\n<p><p><code>PYTHONPATH<\/code>\u662f\u4e00\u4e2a\u7528\u6237\u53ef\u4ee5\u8bbe\u7f6e\u7684\u73af\u5883\u53d8\u91cf\uff0c\u7528\u4e8e\u6dfb\u52a0Python\u6a21\u5757\u7684\u641c\u7d22\u8def\u5f84\u3002\u4e0e<code>sys.path<\/code>\u4e0d\u540c\u7684\u662f\uff0c<code>PYTHONPATH<\/code>\u662f\u7531\u7528\u6237\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u8bbe\u7f6e\u7684\uff0c\u800c<code>sys.path<\/code>\u662f\u7531Python\u89e3\u91ca\u5668\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u751f\u6210\u7684\u3002\u5982\u679c\u4f60\u5728<code>PYTHONPATH<\/code>\u4e2d\u6dfb\u52a0\u4e86\u8def\u5f84\uff0c\u90a3\u4e48\u8fd9\u4e9b\u8def\u5f84\u4f1a\u88ab\u81ea\u52a8\u6dfb\u52a0\u5230<code>sys.path<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u68c0\u67e5\u64cd\u4f5c\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u76f4\u63a5\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u68c0\u67e5\u548c\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u6765\u8c03\u6574Python\u7684\u641c\u7d22\u8def\u5f84\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4e0d\u540c\u7684\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<p><h4>1. \u5728Windows\u4e0a\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5728Windows\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8bbe\u7f6e\u6216\u67e5\u770b<code>PYTHONPATH<\/code>\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u53f3\u952e\u70b9\u51fb\u201c\u6b64\u7535\u8111\u201d\u6216\u201c\u6211\u7684\u7535\u8111\u201d\uff0c\u7136\u540e\u9009\u62e9\u201c\u5c5e\u6027\u201d\u3002<\/li>\n<li>\u70b9\u51fb\u201c\u9ad8\u7ea7\u7cfb\u7edf\u8bbe\u7f6e\u201d\u3002<\/li>\n<li>\u5728\u201c\u7cfb\u7edf\u5c5e\u6027\u201d\u7a97\u53e3\u4e2d\uff0c\u70b9\u51fb\u201c\u73af\u5883\u53d8\u91cf\u201d\u6309\u94ae\u3002<\/li>\n<li>\u5728\u201c\u73af\u5883\u53d8\u91cf\u201d\u7a97\u53e3\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5728\u7528\u6237\u53d8\u91cf\u6216\u7cfb\u7edf\u53d8\u91cf\u4e2d\u67e5\u770b\u548c\u7f16\u8f91<code>PYTHONPATH<\/code>\u3002<\/li>\n<\/ol>\n<p><p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u786e\u4fdd\u8def\u5f84\u4e4b\u95f4\u7528\u5206\u53f7\uff08;\uff09\u5206\u9694\u3002<\/li>\n<li>\u4fee\u6539\u73af\u5883\u53d8\u91cf\u540e\uff0c\u53ef\u80fd\u9700\u8981\u91cd\u65b0\u542f\u52a8\u7ec8\u7aef\u6216\u5e94\u7528\u7a0b\u5e8f\u4ee5\u4f7f\u66f4\u6539\u751f\u6548\u3002<\/li>\n<\/ul>\n<p><h4>2. \u5728Unix\/Linux\u6216macOS\u4e0a\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5728Unix\/Linux\u6216macOS\u7cfb\u7edf\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u7f16\u8f91<code>~\/.bashrc<\/code>\u3001<code>~\/.bash_profile<\/code>\u6216<code>~\/.zshrc<\/code>\u6587\u4ef6\u6765\u8bbe\u7f6e<code>PYTHONPATH<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export PYTHONPATH=\/your\/custom\/path:$PYTHONPATH<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4f7f\u66f4\u6539\u751f\u6548\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">source ~\/.bashrc<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u8def\u5f84\u4e4b\u95f4\u7528\u5192\u53f7\uff08:\uff09\u5206\u9694\u3002<\/li>\n<li>\u4f7f\u7528<code>export<\/code>\u547d\u4ee4\u786e\u4fdd\u53d8\u91cf\u5728\u5b50\u8fdb\u7a0b\u4e2d\u4e5f\u53ef\u7528\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u901a\u8fc7Python\u865a\u62df\u73af\u5883\u83b7\u53d6\u8def\u5f84<\/h3>\n<\/p>\n<p><p>Python\u865a\u62df\u73af\u5883\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u5141\u8bb8\u4f60\u5728\u9879\u76ee\u7ea7\u522b\u7ba1\u7406Python\u5305\u548c\u4f9d\u8d56\u9879\u3002\u6bcf\u4e2a\u865a\u62df\u73af\u5883\u90fd\u6709\u81ea\u5df1\u7684\u72ec\u7acb\u8def\u5f84\u8bbe\u7f6e\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u540e\u68c0\u67e5<code>sys.path<\/code>\u6765\u786e\u8ba4\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u548c\u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>venv<\/code>\u6a21\u5757\u521b\u5efa\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m venv myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p>\u5728Windows\u4e0a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">myenv\\Scripts\\activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u5728Unix\/Linux\u6216macOS\u4e0a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">source myenv\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>2. \u68c0\u67e5\u865a\u62df\u73af\u5883\u7684\u8def\u5f84<\/h4>\n<\/p>\n<p><p>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u540e\uff0c\u8fd0\u884c\u4ee5\u4e0bPython\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><strong>\u6df1\u5165\u89e3\u6790\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u540e\uff0c<code>sys.path<\/code>\u4f1a\u4f18\u5148\u5305\u542b\u865a\u62df\u73af\u5883\u4e2d\u7684\u8def\u5f84\u3002\u8fd9\u610f\u5473\u7740\u4efb\u4f55\u5728\u865a\u62df\u73af\u5883\u4e2d\u5b89\u88c5\u7684\u5305\u90fd\u4f1a\u9996\u5148\u5728\u8be5\u73af\u5883\u4e2d\u88ab\u5bfb\u627e\u548c\u4f7f\u7528\u3002\u8fd9\u79cd\u9694\u79bb\u7279\u6027\u4f7f\u5f97\u5f00\u53d1\u8005\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u9879\u76ee\u4e2d\u4f7f\u7528\u4e0d\u540c\u7248\u672c\u7684\u5305\uff0c\u800c\u4e0d\u5fc5\u62c5\u5fc3\u5168\u5c40Python\u73af\u5883\u7684\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177\u83b7\u53d6\u8def\u5f84\u4fe1\u606f<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u7ba1\u7406\u548c\u67e5\u770bPython\u8def\u5f84\u3002\u8fd9\u4e9b\u5de5\u5177\u901a\u5e38\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u529f\u80fd\uff0c\u6bd4\u5982\u73af\u5883\u7ba1\u7406\u3001\u5305\u7248\u672c\u63a7\u5236\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528pipenv<\/h4>\n<\/p>\n<p><p><code>pipenv<\/code>\u662f\u4e00\u4e2a\u6d41\u884c\u7684Python\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u96c6\u6210\u4e86<code>pip<\/code>\u548c<code>virtualenv<\/code>\u7684\u529f\u80fd\u3002\u4f7f\u7528<code>pipenv<\/code>\u521b\u5efa\u548c\u7ba1\u7406\u9879\u76ee\u73af\u5883\uff0c\u53ef\u4ee5\u81ea\u52a8\u5904\u7406\u4f9d\u8d56\u9879\u548c\u8def\u5f84\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pipenv install<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u5165\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pipenv shell<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u865a\u62df\u73af\u5883\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e0e\u4e0a\u8ff0\u76f8\u540c\u7684\u65b9\u6cd5\u68c0\u67e5\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528conda<\/h4>\n<\/p>\n<p><p><code>conda<\/code>\u662f\u53e6\u4e00\u4e2a\u6d41\u884c\u7684\u5305\u548c\u73af\u5883\u7ba1\u7406\u5de5\u5177\uff0c\u7279\u522b\u662f\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u79d1\u5b66\u9886\u57df\u3002\u4f7f\u7528<code>conda<\/code>\u521b\u5efa\u548c\u7ba1\u7406\u73af\u5883\u975e\u5e38\u7b80\u5355\uff0c\u5e76\u4e14\u5b83\u652f\u6301\u8de8\u8bed\u8a00\u7684\u5305\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684conda\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">conda create --name myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6fc0\u6d3b\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">conda activate myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728conda\u73af\u5883\u4e2d\uff0c<code>sys.path<\/code>\u4f1a\u81ea\u52a8\u914d\u7f6e\u4e3a\u5305\u542b\u5f53\u524d\u73af\u5883\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7ed3\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u83b7\u53d6Python\u7cfb\u7edf\u8def\u5f84\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528<code>sys<\/code>\u548c<code>os<\/code>\u6a21\u5757\u3001\u68c0\u67e5\u64cd\u4f5c\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5de5\u5177\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u9700\u6c42\u548c\u4e2a\u4eba\u4e60\u60ef\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u8fdb\u884c\u7ba1\u7406\u548c\u914d\u7f6e\u3002\u4e86\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u4e0d\u4ec5\u6709\u52a9\u4e8e\u89e3\u51b3\u8def\u5f84\u76f8\u5173\u7684\u95ee\u9898\uff0c\u8fd8\u80fd\u63d0\u9ad8Python\u9879\u76ee\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u67e5\u770bPython\u7684\u7cfb\u7edf\u8def\u5f84\uff1f<\/strong><br \/>\u8981\u67e5\u770bPython\u7684\u7cfb\u7edf\u8def\u5f84\uff0c\u53ef\u4ee5\u5728Python\u7684\u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u6216\u811a\u672c\u4e2d\u4f7f\u7528<code>sys<\/code>\u6a21\u5757\u3002\u5bfc\u5165<code>sys<\/code>\u6a21\u5757\u540e\uff0c\u4f7f\u7528<code>sys.path<\/code>\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u7684\u7cfb\u7edf\u8def\u5f84\u5217\u8868\u3002\u8fd9\u4e9b\u8def\u5f84\u662fPython\u5728\u5bfc\u5165\u6a21\u5757\u65f6\u641c\u7d22\u7684\u76ee\u5f55\u3002<\/p>\n<p><strong>\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u5982\u4f55\u627e\u5230Python\u7684\u5b89\u88c5\u8def\u5f84\uff1f<\/strong><br \/>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u63d0\u793a\u7b26\u8f93\u5165<code>where python<\/code>\u6765\u67e5\u627ePython\u7684\u5b89\u88c5\u8def\u5f84\uff1b\u5728Linux\u6216macOS\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>which python<\/code>\u547d\u4ee4\u3002\u901a\u8fc7\u8fd9\u4e9b\u547d\u4ee4\uff0c\u7528\u6237\u53ef\u4ee5\u5feb\u901f\u5b9a\u4f4dPython\u7684\u5b89\u88c5\u4f4d\u7f6e\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bbe\u7f6e\u6216\u4fee\u6539Python\u7684\u7cfb\u7edf\u8def\u5f84\uff1f<\/strong><br \/>\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539<code>sys.path<\/code>\u5217\u8868\u6765\u4e34\u65f6\u6dfb\u52a0\u6216\u4fee\u6539\u7cfb\u7edf\u8def\u5f84\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<code>PYTHONPATH<\/code>\u6765\u6c38\u4e45\u6027\u5730\u6dfb\u52a0\u8def\u5f84\u3002\u8fd9\u6837\uff0cPython\u5728\u542f\u52a8\u65f6\u4f1a\u81ea\u52a8\u52a0\u8f7d\u8fd9\u4e9b\u8def\u5f84\uff0c\u4ece\u800c\u8ba9\u7528\u6237\u53ef\u4ee5\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e00\u3001\u83b7\u53d6Python\u7cfb\u7edf\u8def\u5f84\u7684\u65b9\u6cd5 \u8981\u77e5\u9053Python\u7684\u7cfb\u7edf\u8def\u5f84\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528sys\u6a21\u5757\u3001\u4f7f\u7528os\u6a21 [&hellip;]","protected":false},"author":3,"featured_media":998293,"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\/998289"}],"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=998289"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998289\/revisions"}],"predecessor-version":[{"id":998296,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998289\/revisions\/998296"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/998293"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=998289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=998289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=998289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}