{"id":996097,"date":"2024-12-27T09:10:26","date_gmt":"2024-12-27T01:10:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/996097.html"},"modified":"2024-12-27T09:10:28","modified_gmt":"2024-12-27T01:10:28","slug":"%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%aepython%e6%90%9c%e7%b4%a2%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/996097.html","title":{"rendered":"\u5982\u4f55\u8bbe\u7f6epython\u641c\u7d22\u8def\u5f84"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072451\/d51db2b5-bd61-427e-a1bc-459536ae9f16.webp\" alt=\"\u5982\u4f55\u8bbe\u7f6epython\u641c\u7d22\u8def\u5f84\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u8bbe\u7f6e\u641c\u7d22\u8def\u5f84\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u4efb\u52a1\uff0c\u56e0\u4e3a\u5b83\u51b3\u5b9a\u4e86Python\u89e3\u91ca\u5668\u5728\u8fd0\u884c\u65f6\u5982\u4f55\u627e\u5230\u6a21\u5757\u548c\u5305\u3002<strong>\u8bbe\u7f6ePython\u641c\u7d22\u8def\u5f84\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u73af\u5883\u53d8\u91cfPYTHONPATH\u3001\u4fee\u6539sys.path\u5217\u8868\u3001\u5229\u7528.pth\u6587\u4ef6<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5b83\u4eec\u7684\u4f7f\u7528\u573a\u666f\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u73af\u5883\u53d8\u91cfPYTHONPATH<\/p>\n<\/p>\n<p><p>Python\u89e3\u91ca\u5668\u5728\u542f\u52a8\u65f6\uff0c\u4f1a\u81ea\u52a8\u52a0\u8f7d\u73af\u5883\u53d8\u91cfPYTHONPATH\u4e2d\u6307\u5b9a\u7684\u8def\u5f84\u3002\u8fd9\u4e2a\u53d8\u91cf\u7684\u4f5c\u7528\u7c7b\u4f3c\u4e8e\u7cfb\u7edf\u7684PATH\u53d8\u91cf\uff0c\u7528\u4e8e\u6307\u5b9aPython\u6a21\u5757\u7684\u641c\u7d22\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>1.1 \u8bbe\u7f6ePYTHONPATH<\/p>\n<\/p>\n<p><p>\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u7684\u65b9\u6cd5\u6709\u6240\u4e0d\u540c\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5728Windows\u4e2d<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u201c\u7cfb\u7edf\u5c5e\u6027\u201d\u4e2d\u7684\u201c\u73af\u5883\u53d8\u91cf\u201d\u9009\u9879\u6765\u8bbe\u7f6ePYTHONPATH\u3002\u5728\u201c\u7cfb\u7edf\u53d8\u91cf\u201d\u6216\u201c\u7528\u6237\u53d8\u91cf\u201d\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\uff0c\u53d8\u91cf\u540d\u4e3aPYTHONPATH\uff0c\u53d8\u91cf\u503c\u4e3a\u4f60\u7684\u6a21\u5757\u8def\u5f84\uff0c\u8def\u5f84\u4e4b\u95f4\u7528\u5206\u53f7\u5206\u9694\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5728Linux\u548cmacOS\u4e2d<\/strong>\uff1a\u53ef\u4ee5\u5728\u7ec8\u7aef\u4e2d\u4f7f\u7528export\u547d\u4ee4\u8bbe\u7f6ePYTHONPATH\u3002\u4f8b\u5982\uff0c\u5728bash\u4e2d\uff0c\u53ef\u4ee5\u5c06\u4ee5\u4e0b\u547d\u4ee4\u6dfb\u52a0\u5230<del>\/.bashrc\u6216<\/del>\/.bash_profile\u6587\u4ef6\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export PYTHONPATH=&quot;\/path\/to\/your\/module:\/another\/path&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>1.2 \u4f7f\u7528\u73af\u5883\u53d8\u91cf\u7684\u4f18\u7f3a\u70b9<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u8bbe\u7f6e\u641c\u7d22\u8def\u5f84\u7684\u4f18\u70b9\u662f\u7b80\u5355\u76f4\u63a5\uff0c\u9002\u7528\u4e8e\u4e34\u65f6\u6027\u7684\u5f00\u53d1\u73af\u5883\u8bbe\u7f6e\u3002\u4f46\u5176\u7f3a\u70b9\u4e5f\u5f88\u660e\u663e\uff1a\u73af\u5883\u53d8\u91cf\u7684\u8bbe\u7f6e\u5bf9\u7cfb\u7edf\u5168\u5c40\u751f\u6548\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u5176\u4ed6Python\u9879\u76ee\u7684\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4fee\u6539sys.path\u5217\u8868<\/p>\n<\/p>\n<p><p>sys.path\u662fPython\u4e2d\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u5217\u8868\uff0c\u5b83\u5305\u542b\u4e86\u5f53\u524dPython\u89e3\u91ca\u5668\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84\u3002\u901a\u8fc7\u4fee\u6539sys.path\uff0c\u53ef\u4ee5\u52a8\u6001\u5730\u6dfb\u52a0\u6216\u5220\u9664\u641c\u7d22\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>2.1 \u5728\u811a\u672c\u4e2d\u4fee\u6539sys.path<\/p>\n<\/p>\n<p><p>\u5728Python\u811a\u672c\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u4fee\u6539sys.path\u6765\u6dfb\u52a0\u65b0\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84\uff1a<\/p>\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><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u8c03\u6574\u6a21\u5757\u8def\u5f84\u7684\u60c5\u51b5\uff0c\u7279\u522b\u662f\u5728\u811a\u672c\u6267\u884c\u8fc7\u7a0b\u4e2d\u9700\u8981\u5f15\u5165\u4e0d\u540c\u8def\u5f84\u4e0b\u7684\u6a21\u5757\u65f6\u3002<\/p>\n<\/p>\n<p><p>2.2 \u4f7f\u7528sys.path\u7684\u4f18\u7f3a\u70b9<\/p>\n<\/p>\n<p><p>\u4f7f\u7528sys.path\u7684\u4f18\u70b9\u662f\u7075\u6d3b\u3001\u53ef\u63a7\uff0c\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u8c03\u6574\u641c\u7d22\u8def\u5f84\u3002\u4f46\u5176\u7f3a\u70b9\u662f\u9700\u8981\u5728\u6bcf\u4e2a\u9700\u8981\u8c03\u6574\u8def\u5f84\u7684\u811a\u672c\u4e2d\u8fdb\u884c\u4fee\u6539\uff0c\u4e0d\u9002\u5408\u5168\u5c40\u6027\u7684\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5229\u7528.pth\u6587\u4ef6<\/p>\n<\/p>\n<p><p>.pth\u6587\u4ef6\u662f\u4e00\u79cd\u7528\u4e8e\u6269\u5c55Python\u8def\u5f84\u7684\u6587\u4ef6\uff0c\u901a\u5e38\u4f4d\u4e8esite-packages\u76ee\u5f55\u4e2d\u3002\u6bcf\u4e2a.pth\u6587\u4ef6\u4e2d\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u8def\u5f84\uff0c\u8fd9\u4e9b\u8def\u5f84\u4f1a\u5728Python\u542f\u52a8\u65f6\u88ab\u81ea\u52a8\u6dfb\u52a0\u5230sys.path\u4e2d\u3002<\/p>\n<\/p>\n<p><p>3.1 \u521b\u5efa\u548c\u4f7f\u7528.pth\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u8981\u521b\u5efa\u4e00\u4e2a.pth\u6587\u4ef6\uff0c\u53ea\u9700\u5728site-packages\u76ee\u5f55\u4e2d\u65b0\u5efa\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u4ee5.pth\u4e3a\u540e\u7f00\u3002\u6587\u4ef6\u5185\u5bb9\u4e3a\u4f60\u5e0c\u671b\u6dfb\u52a0\u7684\u8def\u5f84\uff0c\u6bcf\u884c\u4e00\u4e2a\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code>\/path\/to\/your\/module<\/p>\n<p>\/another\/path<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3.2 \u4f7f\u7528.pth\u6587\u4ef6\u7684\u4f18\u7f3a\u70b9<\/p>\n<\/p>\n<p><p>.pth\u6587\u4ef6\u7684\u4f18\u70b9\u662f\u4fbf\u4e8e\u7ba1\u7406\uff0c\u53ef\u4ee5\u6279\u91cf\u6dfb\u52a0\u591a\u4e2a\u8def\u5f84\uff0c\u4e14\u5bf9\u6240\u6709Python\u811a\u672c\u751f\u6548\u3002\u4f46\u5176\u7f3a\u70b9\u662f\u9700\u8981\u624b\u52a8\u7ba1\u7406\u8fd9\u4e9b\u6587\u4ef6\uff0c\u4e14\u5bf9\u65b0\u624b\u7528\u6237\u800c\u8a00\u53ef\u80fd\u4e0d\u591f\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u6700\u4f73\u5b9e\u8df5\u4e0e\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u8bbe\u7f6ePython\u641c\u7d22\u8def\u5f84\u65f6\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u9879\u76ee\u9700\u6c42\u548c\u73af\u5883\u6765\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u548c\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<\/p>\n<p><p>4.1 \u907f\u514d\u4f7f\u7528\u5168\u5c40\u73af\u5883\u53d8\u91cf<\/p>\n<\/p>\n<p><p>\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u5168\u5c40\u7684PYTHONPATH\u73af\u5883\u53d8\u91cf\uff0c\u56e0\u4e3a\u5b83\u4f1a\u5f71\u54cd\u5230\u6240\u6709\u4f7f\u7528\u8be5Python\u89e3\u91ca\u5668\u7684\u9879\u76ee\uff0c\u53ef\u80fd\u5bfc\u81f4\u8def\u5f84\u51b2\u7a81\u6216\u6a21\u5757\u7248\u672c\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>4.2 \u4f18\u5148\u4f7f\u7528\u865a\u62df\u73af\u5883<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u865a\u62df\u73af\u5883\uff08\u5982venv\u6216virtualenv\uff09\u53ef\u4ee5\u6709\u6548\u9694\u79bb\u9879\u76ee\u73af\u5883\uff0c\u907f\u514d\u8def\u5f84\u51b2\u7a81\u3002\u5728\u865a\u62df\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5\u4f9d\u8d56\u6a21\u5757\uff0c\u800c\u65e0\u9700\u624b\u52a8\u8bbe\u7f6e\u641c\u7d22\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>4.3 \u7ec4\u7ec7\u597d\u9879\u76ee\u7ed3\u6784<\/p>\n<\/p>\n<p><p>\u826f\u597d\u7684\u9879\u76ee\u7ed3\u6784\u53ef\u4ee5\u51cf\u5c11\u5bf9\u641c\u7d22\u8def\u5f84\u7684\u4f9d\u8d56\u3002\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2asrc\u76ee\u5f55\uff0c\u5c06\u6240\u6709\u6a21\u5757\u548c\u5305\u653e\u5728\u5176\u4e2d\uff0c\u5e76\u5728\u9879\u76ee\u7684\u5165\u53e3\u811a\u672c\u4e2d\u5c06src\u76ee\u5f55\u6dfb\u52a0\u5230sys.path\u4e2d\u3002<\/p>\n<\/p>\n<p><p>4.4 \u6ce8\u610f\u8def\u5f84\u7684\u4f18\u5148\u7ea7<\/p>\n<\/p>\n<p><p>\u5728sys.path\u4e2d\uff0c\u8def\u5f84\u7684\u4f18\u5148\u7ea7\u662f\u81ea\u4e0a\u800c\u4e0b\u7684\u3002\u5982\u679c\u6709\u591a\u4e2a\u8def\u5f84\u4e2d\u5b58\u5728\u540c\u540d\u6a21\u5757\uff0cPython\u4f1a\u4f18\u5148\u52a0\u8f7dsys.path\u4e2d\u9760\u524d\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>4.5 \u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165<\/p>\n<\/p>\n<p><p>\u5728\u5305\u5185\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\uff08\u5982from .module import function\uff09\u53ef\u4ee5\u51cf\u5c11\u5bf9\u641c\u7d22\u8def\u5f84\u7684\u4f9d\u8d56\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u79fb\u690d\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6848\u4f8b\u5206\u6790<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u8bbe\u7f6ePython\u641c\u7d22\u8def\u5f84\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b9e\u9645\u6848\u4f8b\u5206\u6790\u3002<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5927\u578bPython\u9879\u76ee\uff0c\u8be5\u9879\u76ee\u5305\u542b\u591a\u4e2a\u6a21\u5757\u548c\u5305\uff0c\u800c\u5176\u4e2d\u67d0\u4e9b\u6a21\u5757\u4f4d\u4e8e\u4e0d\u540c\u7684\u76ee\u5f55\u4e0b\u3002\u4e3a\u4e86\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u65b9\u4fbf\u5730\u5bfc\u5165\u8fd9\u4e9b\u6a21\u5757\uff0c\u6211\u4eec\u9700\u8981\u5408\u7406\u5730\u8bbe\u7f6ePython\u641c\u7d22\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>5.1 \u9879\u76ee\u7ed3\u6784<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>\u2502<\/p>\n<p>\u251c\u2500\u2500 src\/<\/p>\n<p>\u2502   \u251c\u2500\u2500 m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/p>\n<p>\u2502   \u251c\u2500\u2500 module1\/<\/p>\n<p>\u2502   \u2502   \u2514\u2500\u2500 __init__.py<\/p>\n<p>\u2502   \u251c\u2500\u2500 module2\/<\/p>\n<p>\u2502   \u2502   \u2514\u2500\u2500 __init__.py<\/p>\n<p>\u2502<\/p>\n<p>\u2514\u2500\u2500 external_modules\/<\/p>\n<p>    \u251c\u2500\u2500 ext_module1\/<\/p>\n<p>    \u2514\u2500\u2500 ext_module2\/<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u9879\u76ee\u4e2d\uff0csrc\u76ee\u5f55\u4e0b\u662f\u4e3b\u8981\u7684\u9879\u76ee\u4ee3\u7801\uff0c\u800cexternal_modules\u76ee\u5f55\u4e0b\u662f\u4e00\u4e9b\u5916\u90e8\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p>5.2 \u8bbe\u7f6e\u641c\u7d22\u8def\u5f84<\/p>\n<\/p>\n<p><p>\u5728main.py\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539sys.path\u6765\u52a8\u6001\u8bbe\u7f6e\u641c\u7d22\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>import os<\/p>\n<h2><strong>\u83b7\u53d6\u9879\u76ee\u6839\u76ee\u5f55<\/strong><\/h2>\n<p>project_root = os.path.dirname(os.path.abspath(__file__))<\/p>\n<h2><strong>\u6dfb\u52a0src\u76ee\u5f55\u5230sys.path<\/strong><\/h2>\n<p>sys.path.append(os.path.join(project_root, &#39;src&#39;))<\/p>\n<h2><strong>\u6dfb\u52a0external_modules\u76ee\u5f55\u5230sys.path<\/strong><\/h2>\n<p>sys.path.append(os.path.join(project_root, &#39;external_modules&#39;))<\/p>\n<h2><strong>\u73b0\u5728\u53ef\u4ee5\u5bfc\u5165\u6a21\u5757<\/strong><\/h2>\n<p>import module1<\/p>\n<p>import ext_module1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>5.3 \u89e3\u91ca<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u9879\u76ee\u7684\u5165\u53e3\u811a\u672c\u4e2d\u52a8\u6001\u8bbe\u7f6e\u641c\u7d22\u8def\u5f84\uff0c\u800c\u4e0d\u9700\u8981\u4f9d\u8d56\u5168\u5c40\u7684\u73af\u5883\u53d8\u91cf\u3002\u8fd9\u4f7f\u5f97\u9879\u76ee\u7684\u73af\u5883\u914d\u7f6e\u66f4\u52a0\u7075\u6d3b\uff0c\u4e5f\u4fbf\u4e8e\u5728\u4e0d\u540c\u7684\u5f00\u53d1\u673a\u5668\u4e0a\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u7ed3\u8bba<\/p>\n<\/p>\n<p><p>\u8bbe\u7f6ePython\u641c\u7d22\u8def\u5f84\u662fPython\u5f00\u53d1\u4e2d\u7684\u4e00\u4e2a\u57fa\u7840\u4efb\u52a1\uff0c\u901a\u8fc7\u5408\u7406\u8bbe\u7f6e\u641c\u7d22\u8def\u5f84\uff0c\u53ef\u4ee5\u63d0\u9ad8\u9879\u76ee\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5e94\u8be5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u6700\u4f73\u5b9e\u8df5\uff0c\u4ee5\u786e\u4fdd\u9879\u76ee\u7684\u6a21\u5757\u7ba1\u7406\u6e05\u6670\u3001\u6709\u5e8f\u3002\u65e0\u8bba\u662f\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u3001sys.path\u8fd8\u662f.pth\u6587\u4ef6\uff0c\u90fd\u9700\u8981\u6ce8\u610f\u8def\u5f84\u7684\u4f18\u5148\u7ea7\u548c\u6f5c\u5728\u7684\u51b2\u7a81\u95ee\u9898\u3002\u901a\u8fc7\u5bf9\u9879\u76ee\u7ed3\u6784\u7684\u5408\u7406\u89c4\u5212\u548c\u5bf9\u865a\u62df\u73af\u5883\u7684\u6709\u6548\u4f7f\u7528\uff0c\u53ef\u4ee5\u6700\u5927\u7a0b\u5ea6\u5730\u907f\u514d\u8def\u5f84\u76f8\u5173\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6dfb\u52a0\u81ea\u5b9a\u4e49\u6a21\u5757\u641c\u7d22\u8def\u5f84\uff1f<\/strong><br \/>\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539<code>sys.path<\/code>\u6765\u6dfb\u52a0\u81ea\u5b9a\u4e49\u6a21\u5757\u641c\u7d22\u8def\u5f84\u3002\u5728Python\u811a\u672c\u4e2d\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import sys\nsys.path.append(&#39;\/your\/custom\/path&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0cPython\u5c31\u80fd\u5728\u8be5\u8def\u5f84\u4e2d\u67e5\u627e\u6a21\u5757\u548c\u5305\u3002\u786e\u4fdd\u66ff\u6362<code>\/your\/custom\/path<\/code>\u4e3a\u4f60\u5b9e\u9645\u7684\u76ee\u5f55\u3002<\/p>\n<p><strong>\u5728\u865a\u62df\u73af\u5883\u4e2d\u5982\u4f55\u8bbe\u7f6e\u641c\u7d22\u8def\u5f84\uff1f<\/strong><br \/>\u5728\u865a\u62df\u73af\u5883\u4e2d\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u6216\u4fee\u6539<code>PYTHONPATH<\/code>\u73af\u5883\u53d8\u91cf\u6765\u8bbe\u7f6e\u641c\u7d22\u8def\u5f84\u3002\u5728Linux\u6216MacOS\u4e2d\uff0c\u53ef\u4ee5\u5728\u7ec8\u7aef\u4e2d\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a  <\/p>\n<pre><code class=\"language-bash\">export PYTHONPATH=&quot;\/your\/custom\/path:$PYTHONPATH&quot;\n<\/code><\/pre>\n<p>\u5728Windows\u4e0a\uff0c\u53ef\u4ee5\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a  <\/p>\n<pre><code class=\"language-cmd\">set PYTHONPATH=C:\\your\\custom\\path;%PYTHONPATH%\n<\/code><\/pre>\n<p>\u8fd9\u6837\u8bbe\u7f6e\u540e\uff0c\u865a\u62df\u73af\u5883\u4e2d\u7684Python\u5c06\u4f1a\u4f18\u5148\u5728\u6307\u5b9a\u8def\u5f84\u4e2d\u67e5\u627e\u6a21\u5757\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728IDE\u4e2d\u914d\u7f6ePython\u641c\u7d22\u8def\u5f84\uff1f<\/strong><br \/>\u5728\u5e38\u89c1\u7684IDE\u5982PyCharm\u4e2d\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u9879\u76ee\u8bbe\u7f6e\u6765\u914d\u7f6e\u641c\u7d22\u8def\u5f84\u3002\u6253\u5f00\u9879\u76ee\u8bbe\u7f6e\uff0c\u5bfc\u822a\u5230\u201cProject: [\u4f60\u7684\u9879\u76ee\u540d]\u201d &gt; \u201cPython Interpreter\u201d\uff0c\u9009\u62e9\u201cShow paths for the selected interpreter\u201d\u9009\u9879\uff0c\u70b9\u51fb\u201c+\u201d\u6309\u94ae\u6dfb\u52a0\u81ea\u5b9a\u4e49\u8def\u5f84\u3002\u8fd9\u6837\uff0cIDE\u4f1a\u81ea\u52a8\u5c06\u8fd9\u4e9b\u8def\u5f84\u6dfb\u52a0\u5230Python\u7684\u641c\u7d22\u8def\u5f84\u4e2d\uff0c\u4ece\u800c\u65b9\u4fbf\u5f00\u53d1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8bbe\u7f6e\u641c\u7d22\u8def\u5f84\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u4efb\u52a1\uff0c\u56e0\u4e3a\u5b83\u51b3\u5b9a\u4e86Python\u89e3\u91ca\u5668\u5728\u8fd0\u884c\u65f6\u5982\u4f55\u627e\u5230\u6a21\u5757\u548c\u5305\u3002\u8bbe\u7f6ePy [&hellip;]","protected":false},"author":3,"featured_media":996105,"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\/996097"}],"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=996097"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/996097\/revisions"}],"predecessor-version":[{"id":996107,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/996097\/revisions\/996107"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/996105"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=996097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=996097"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=996097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}