{"id":1184244,"date":"2025-01-15T19:24:14","date_gmt":"2025-01-15T11:24:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1184244.html"},"modified":"2025-01-15T19:24:17","modified_gmt":"2025-01-15T11:24:17","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a4%e5%ae%9a%e5%af%bc%e5%85%a5-py%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1184244.html","title":{"rendered":"python\u5982\u4f55\u8ba4\u5b9a\u5bfc\u5165.py\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25133742\/d8abe8ba-edbe-4549-afb9-3fbb61775394.webp\" alt=\"python\u5982\u4f55\u8ba4\u5b9a\u5bfc\u5165.py\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python \u8ba4\u5b9a\u5bfc\u5165 .py \u6587\u4ef6\u7684\u65b9\u5f0f<\/strong>\u6d89\u53ca\u51e0\u4e2a\u6838\u5fc3\u8981\u70b9\uff0c\u5305\u62ec\uff1a<strong>\u6a21\u5757\u641c\u7d22\u8def\u5f84\u3001<code>import<\/code> \u8bed\u53e5\u3001<code>__init__.py<\/code> \u6587\u4ef6\u3001\u76f8\u5bf9\u548c\u7edd\u5bf9\u5bfc\u5165<\/strong>\u3002\u5728\u5bfc\u5165 .py \u6587\u4ef6\u65f6\uff0cPython \u4f1a\u6839\u636e\u8fd9\u4e9b\u8981\u70b9\u6765\u67e5\u627e\u548c\u52a0\u8f7d\u6a21\u5757\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u8981\u70b9\uff0c\u5e76\u5bf9\u5176\u4e2d\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><p><strong>\u6a21\u5757\u641c\u7d22\u8def\u5f84<\/strong>\uff1a\u5f53\u4f7f\u7528 <code>import<\/code> \u8bed\u53e5\u5bfc\u5165\u6a21\u5757\u65f6\uff0cPython \u4f1a\u5728\u4e00\u7cfb\u5217\u6307\u5b9a\u7684\u76ee\u5f55\u4e2d\u67e5\u627e\u6a21\u5757\u3002\u8fd9\u4e9b\u76ee\u5f55\u5305\u62ec\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3001\u73af\u5883\u53d8\u91cf <code>PYTHONPATH<\/code> \u6307\u5b9a\u7684\u76ee\u5f55\u3001\u4ee5\u53ca Python \u5b89\u88c5\u76ee\u5f55\u4e0b\u7684\u6807\u51c6\u5e93\u8def\u5f84\u3002\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539 <code>sys.path<\/code> \u6765\u52a8\u6001\u5730\u6539\u53d8\u6a21\u5757\u641c\u7d22\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p><strong>1\u3001\u6a21\u5757\u641c\u7d22\u8def\u5f84<\/strong><\/p>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u6a21\u5757\u641c\u7d22\u8def\u5f84\u51b3\u5b9a\u4e86 Python \u5728\u4f55\u5904\u67e5\u627e\u8981\u5bfc\u5165\u7684\u6a21\u5757\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6a21\u5757\u641c\u7d22\u8def\u5f84\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u4f4d\u7f6e\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong>\uff1a\u8fd9\u662f Python \u89e3\u91ca\u5668\u8fd0\u884c\u7684\u76ee\u5f55\u3002\u5982\u679c\u5728\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e0b\u6709\u8981\u5bfc\u5165\u7684\u6a21\u5757\u6587\u4ef6\uff0cPython \u4f1a\u9996\u5148\u5728\u8fd9\u91cc\u67e5\u627e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u73af\u5883\u53d8\u91cf <code>PYTHONPATH<\/code><\/strong>\uff1a\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e <code>PYTHONPATH<\/code> \u73af\u5883\u53d8\u91cf\u6765\u6307\u5b9a\u989d\u5916\u7684\u76ee\u5f55\uff0c\u8fd9\u4e9b\u76ee\u5f55\u5c06\u88ab\u6dfb\u52a0\u5230\u6a21\u5757\u641c\u7d22\u8def\u5f84\u4e2d\u3002<code>PYTHONPATH<\/code> \u7684\u683c\u5f0f\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u6709\u5173\uff0c\u901a\u5e38\u662f\u4e00\u4e2a\u76ee\u5f55\u5217\u8868\uff0c\u76ee\u5f55\u4e4b\u95f4\u4f7f\u7528\u5192\u53f7\uff08\u5728 Unix \u7cfb\u7edf\u4e0a\uff09\u6216\u5206\u53f7\uff08\u5728 Windows \u7cfb\u7edf\u4e0a\uff09\u5206\u9694\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6807\u51c6\u5e93\u76ee\u5f55<\/strong>\uff1aPython \u5b89\u88c5\u76ee\u5f55\u4e0b\u7684\u6807\u51c6\u5e93\u8def\u5f84\u3002\u6807\u51c6\u5e93\u5305\u542b\u4e86\u8bb8\u591a\u5e38\u7528\u7684\u6a21\u5757\u548c\u5305\uff0cPython \u4f1a\u5728\u8fd9\u4e9b\u76ee\u5f55\u4e2d\u67e5\u627e\u8981\u5bfc\u5165\u7684\u6a21\u5757\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong><code>site-packages<\/code> \u76ee\u5f55<\/strong>\uff1a\u8fd9\u662f Python \u5b89\u88c5\u76ee\u5f55\u4e2d\u7684\u4e00\u4e2a\u7279\u6b8a\u76ee\u5f55\uff0c\u7528\u4e8e\u5b58\u653e\u901a\u8fc7 <code>pip<\/code> \u7b49\u5de5\u5177\u5b89\u88c5\u7684\u7b2c\u4e09\u65b9\u5e93\u548c\u5305\u3002Python \u4f1a\u5728\u8fd9\u91cc\u67e5\u627e\u7b2c\u4e09\u65b9\u6a21\u5757\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>sys.path<\/code> \u67e5\u770b\u548c\u4fee\u6539\u6a21\u5757\u641c\u7d22\u8def\u5f84\u3002<code>sys.path<\/code> \u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u4e86\u6a21\u5757\u641c\u7d22\u8def\u5f84\u4e2d\u7684\u5404\u4e2a\u76ee\u5f55\u3002\u7528\u6237\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u5730\u4fee\u6539 <code>sys.path<\/code>\uff0c\u4ece\u800c\u6539\u53d8\u6a21\u5757\u641c\u7d22\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<h2><strong>\u67e5\u770b\u6a21\u5757\u641c\u7d22\u8def\u5f84<\/strong><\/h2>\n<p>print(sys.path)<\/p>\n<h2><strong>\u6dfb\u52a0\u65b0\u7684\u641c\u7d22\u8def\u5f84<\/strong><\/h2>\n<p>sys.path.append(&#39;\/path\/to\/your\/module&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2\u3001<code>import<\/code> \u8bed\u53e5<\/strong><\/p>\n<\/p>\n<p><p><code>import<\/code> \u8bed\u53e5\u662f\u7528\u4e8e\u5bfc\u5165\u6a21\u5757\u7684\u5173\u952e\u5b57\u3002\u901a\u8fc7 <code>import<\/code> \u8bed\u53e5\uff0c\u7528\u6237\u53ef\u4ee5\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\u6216\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u90e8\u5206\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684 <code>import<\/code> \u8bed\u53e5\u7528\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165\u6574\u4e2a\u6a21\u5757<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import module_name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u5bfc\u5165\u7684\u6a21\u5757\uff0c\u7528\u6237\u9700\u8981\u4f7f\u7528 <code>module_name.function_name<\/code> \u7684\u5f62\u5f0f\u6765\u8c03\u7528\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u6216\u53d8\u91cf\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u90e8\u5206<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from module_name import function_name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u5bfc\u5165\u7684\u51fd\u6570\u6216\u53d8\u91cf\uff0c\u7528\u6237\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 <code>function_name<\/code> \u7684\u5f62\u5f0f\u6765\u8c03\u7528\uff0c\u800c\u4e0d\u9700\u8981\u52a0\u4e0a\u6a21\u5757\u540d\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u6240\u6709\u5185\u5bb9<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from module_name import *<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u5c06\u6a21\u5757\u4e2d\u7684\u6240\u6709\u51fd\u6570\u548c\u53d8\u91cf\u5bfc\u5165\u5f53\u524d\u547d\u540d\u7a7a\u95f4\uff0c\u4e0d\u63a8\u8350\u4f7f\u7528\uff0c\u56e0\u4e3a\u53ef\u80fd\u4f1a\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\u3002<\/p>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u4e3a\u5bfc\u5165\u7684\u6a21\u5757\u8d77\u522b\u540d<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import module_name as alias_name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u7528\u6237\u53ef\u4ee5\u4e3a\u5bfc\u5165\u7684\u6a21\u5757\u8d77\u4e00\u4e2a\u522b\u540d\uff0c\u4f7f\u7528\u522b\u540d\u6765\u8c03\u7528\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u6216\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><p><strong>3\u3001<code>__init__.py<\/code> \u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c<code>__init__.py<\/code> \u6587\u4ef6\u7528\u4e8e\u6807\u8bc6\u4e00\u4e2a\u76ee\u5f55\u4e3a Python \u5305\u3002\u4e00\u4e2a\u5305\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u76ee\u5f55\uff0c<code>__init__.py<\/code> \u6587\u4ef6\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u4e5f\u53ef\u4ee5\u5305\u542b\u5305\u7684\u521d\u59cb\u5316\u4ee3\u7801\u3002\u901a\u8fc7\u5728\u5305\u76ee\u5f55\u4e2d\u6dfb\u52a0 <code>__init__.py<\/code> \u6587\u4ef6\uff0c\u7528\u6237\u53ef\u4ee5\u5c06\u591a\u4e2a\u6a21\u5757\u7ec4\u7ec7\u5728\u4e00\u8d77\uff0c\u5f62\u6210\u4e00\u4e2a\u5305\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-plaintext\">my_package\/<\/p>\n<p>    __init__.py<\/p>\n<p>    module1.py<\/p>\n<p>    module2.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u5bfc\u5165\u5305\u65f6\uff0c<code>__init__.py<\/code> \u6587\u4ef6\u4f1a\u9996\u5148\u88ab\u6267\u884c\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u8be5\u6587\u4ef6\u4e2d\u7f16\u5199\u5305\u7684\u521d\u59cb\u5316\u4ee3\u7801\u3002\u5bfc\u5165\u5305\u4e2d\u7684\u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u70b9\u53f7\u5206\u9694\u5305\u540d\u548c\u6a21\u5757\u540d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import my_package.module1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>4\u3001\u76f8\u5bf9\u548c\u7edd\u5bf9\u5bfc\u5165<\/strong><\/p>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u5bfc\u5165\u6a21\u5757\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u548c\u7edd\u5bf9\u5bfc\u5165\u3002\u76f8\u5bf9\u5bfc\u5165\u662f\u76f8\u5bf9\u4e8e\u5f53\u524d\u6a21\u5757\u6240\u5728\u7684\u5305\u7ed3\u6784\u8fdb\u884c\u5bfc\u5165\uff0c\u800c\u7edd\u5bf9\u5bfc\u5165\u662f\u4ece\u9879\u76ee\u7684\u6839\u76ee\u5f55\u5f00\u59cb\u8fdb\u884c\u5bfc\u5165\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u7edd\u5bf9\u5bfc\u5165<\/strong>\uff1a<\/li>\n<\/ol>\n<p><p>\u7edd\u5bf9\u5bfc\u5165\u662f\u4ece\u9879\u76ee\u7684\u6839\u76ee\u5f55\u5f00\u59cb\u5bfc\u5165\u6a21\u5757\uff0c\u4f7f\u7528\u6a21\u5757\u7684\u5b8c\u6574\u8def\u5f84\u3002\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u7edd\u5bf9\u5bfc\u5165\u901a\u5e38\u66f4\u4e3a\u6e05\u6670\u548c\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import my_package.module1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u76f8\u5bf9\u5bfc\u5165<\/strong>\uff1a<\/li>\n<\/ol>\n<p><p>\u76f8\u5bf9\u5bfc\u5165\u662f\u76f8\u5bf9\u4e8e\u5f53\u524d\u6a21\u5757\u6240\u5728\u7684\u5305\u7ed3\u6784\u8fdb\u884c\u5bfc\u5165\uff0c\u4f7f\u7528\u70b9\u53f7\u8868\u793a\u76f8\u5bf9\u4f4d\u7f6e\u3002\u4e00\u4e2a\u70b9\u53f7\u8868\u793a\u5f53\u524d\u76ee\u5f55\uff0c\u4e24\u4e2a\u70b9\u53f7\u8868\u793a\u4e0a\u4e00\u7ea7\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from . import module1  # \u5bfc\u5165\u540c\u4e00\u76ee\u5f55\u4e0b\u7684\u6a21\u5757<\/p>\n<p>from ..subpackage import module2  # \u5bfc\u5165\u4e0a\u4e00\u7ea7\u76ee\u5f55\u7684\u5b50\u5305\u4e2d\u7684\u6a21\u5757<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u76f8\u5bf9\u5bfc\u5165\u901a\u5e38\u7528\u4e8e\u5305\u5185\u6a21\u5757\u4e4b\u95f4\u7684\u76f8\u4e92\u5bfc\u5165\uff0c\u6709\u52a9\u4e8e\u4fdd\u6301\u5305\u7684\u72ec\u7acb\u6027\u548c\u6a21\u5757\u5316\u3002<\/p>\n<\/p>\n<p><h3>\u6a21\u5757\u641c\u7d22\u8def\u5f84\u7684\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p>Python \u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u76ee\u5f55\u7684\u5217\u8868\uff0c\u5f53\u4f7f\u7528 <code>import<\/code> \u8bed\u53e5\u5bfc\u5165\u6a21\u5757\u65f6\uff0cPython \u4f1a\u6309\u7167\u987a\u5e8f\u4f9d\u6b21\u5728\u8fd9\u4e9b\u76ee\u5f55\u4e2d\u67e5\u627e\u6a21\u5757\u6587\u4ef6\u3002\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u6a21\u5757\u641c\u7d22\u8def\u5f84\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u4e2a\u76ee\u5f55\u7684\u4f5c\u7528\u548c\u914d\u7f6e\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>1\u3001\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/p>\n<\/p>\n<p><p>\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u662f\u6307 Python \u89e3\u91ca\u5668\u8fd0\u884c\u65f6\u6240\u5728\u7684\u76ee\u5f55\u3002\u5f53\u7528\u6237\u5728\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e0b\u7f16\u5199\u548c\u8fd0\u884c Python \u811a\u672c\u65f6\uff0cPython \u4f1a\u9996\u5148\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e2d\u67e5\u627e\u8981\u5bfc\u5165\u7684\u6a21\u5757\u6587\u4ef6\u3002\u5982\u679c\u6a21\u5757\u6587\u4ef6\u4f4d\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e2d\uff0cPython \u5c06\u76f4\u63a5\u5bfc\u5165\u8be5\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>cd<\/code> \u547d\u4ee4\u5207\u6362\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">cd \/path\/to\/your\/script<\/p>\n<p>python script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u811a\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>os<\/code> \u6a21\u5757\u67e5\u770b\u548c\u4fee\u6539\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u67e5\u770b\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>print(os.getcwd())<\/p>\n<h2><strong>\u4fee\u6539\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>os.chdir(&#39;\/path\/to\/new\/directory&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2\u3001\u73af\u5883\u53d8\u91cf <code>PYTHONPATH<\/code><\/strong><\/p>\n<\/p>\n<p><p><code>PYTHONPATH<\/code> \u662f\u4e00\u4e2a\u73af\u5883\u53d8\u91cf\uff0c\u7528\u4e8e\u6307\u5b9a\u989d\u5916\u7684\u76ee\u5f55\uff0c\u8fd9\u4e9b\u76ee\u5f55\u5c06\u88ab\u6dfb\u52a0\u5230\u6a21\u5757\u641c\u7d22\u8def\u5f84\u4e2d\u3002\u5728 Unix \u7cfb\u7edf\u4e0a\uff0c<code>PYTHONPATH<\/code> \u7684\u683c\u5f0f\u662f\u76ee\u5f55\u5217\u8868\uff0c\u76ee\u5f55\u4e4b\u95f4\u4f7f\u7528\u5192\u53f7\u5206\u9694\uff1b\u5728 Windows \u7cfb\u7edf\u4e0a\uff0c\u76ee\u5f55\u4e4b\u95f4\u4f7f\u7528\u5206\u53f7\u5206\u9694\u3002<\/p>\n<\/p>\n<p><p>\u8bbe\u7f6e <code>PYTHONPATH<\/code> \u73af\u5883\u53d8\u91cf\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ul>\n<li>Unix \u7cfb\u7edf\uff08\u4f8b\u5982 Linux \u548c macOS\uff09\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-bash\">export PYTHONPATH=\/path\/to\/dir1:\/path\/to\/dir2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>Windows \u7cfb\u7edf\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-cmd\">set PYTHONPATH=C:\\path\\to\\dir1;C:\\path\\to\\dir2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u811a\u672c\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>sys.path<\/code> \u52a8\u6001\u4fee\u6539\u6a21\u5757\u641c\u7d22\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<h2><strong>\u6dfb\u52a0\u65b0\u7684\u641c\u7d22\u8def\u5f84<\/strong><\/h2>\n<p>sys.path.append(&#39;\/path\/to\/your\/module&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3\u3001\u6807\u51c6\u5e93\u76ee\u5f55<\/strong><\/p>\n<\/p>\n<p><p>\u6807\u51c6\u5e93\u76ee\u5f55\u662f Python \u5b89\u88c5\u76ee\u5f55\u4e0b\u7684\u4e00\u90e8\u5206\uff0c\u5305\u542b\u4e86\u8bb8\u591a\u5e38\u7528\u7684\u6a21\u5757\u548c\u5305\u3002Python \u4f1a\u5728\u8fd9\u4e9b\u76ee\u5f55\u4e2d\u67e5\u627e\u8981\u5bfc\u5165\u7684\u6a21\u5757\u3002\u6807\u51c6\u5e93\u76ee\u5f55\u662f\u5185\u7f6e\u7684\uff0c\u7528\u6237\u901a\u5e38\u4e0d\u9700\u8981\u4fee\u6539\u8fd9\u4e9b\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u67e5\u770b Python \u5b89\u88c5\u76ee\u5f55\u548c\u6807\u51c6\u5e93\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<h2><strong>\u67e5\u770b Python \u5b89\u88c5\u76ee\u5f55<\/strong><\/h2>\n<p>print(sys.executable)<\/p>\n<h2><strong>\u67e5\u770b\u6807\u51c6\u5e93\u8def\u5f84<\/strong><\/h2>\n<p>print(sys.prefix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>4\u3001<code>site-packages<\/code> \u76ee\u5f55<\/strong><\/p>\n<\/p>\n<p><p><code>site-packages<\/code> \u76ee\u5f55\u662f Python \u5b89\u88c5\u76ee\u5f55\u4e2d\u7684\u4e00\u4e2a\u7279\u6b8a\u76ee\u5f55\uff0c\u7528\u4e8e\u5b58\u653e\u901a\u8fc7 <code>pip<\/code> \u7b49\u5de5\u5177\u5b89\u88c5\u7684\u7b2c\u4e09\u65b9\u5e93\u548c\u5305\u3002Python \u4f1a\u5728\u8fd9\u91cc\u67e5\u627e\u7b2c\u4e09\u65b9\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p>\u5728 Unix \u7cfb\u7edf\u4e0a\uff0c<code>site-packages<\/code> \u76ee\u5f55\u901a\u5e38\u4f4d\u4e8e\u4ee5\u4e0b\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-plaintext\">\/usr\/local\/lib\/pythonX.Y\/site-packages<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728 Windows \u7cfb\u7edf\u4e0a\uff0c<code>site-packages<\/code> \u76ee\u5f55\u901a\u5e38\u4f4d\u4e8e\u4ee5\u4e0b\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-plaintext\">C:\\PythonXY\\Lib\\site-packages<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u67e5\u770b <code>site-packages<\/code> \u76ee\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import site<\/p>\n<p>print(site.getsitepackages())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e86\u89e3\u548c\u914d\u7f6e\u6a21\u5757\u641c\u7d22\u8def\u5f84\uff0c\u7528\u6237\u53ef\u4ee5\u7075\u6d3b\u5730\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757\u548c\u7b2c\u4e09\u65b9\u5e93\u3002\u6a21\u5757\u641c\u7d22\u8def\u5f84\u5305\u62ec\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3001\u73af\u5883\u53d8\u91cf <code>PYTHONPATH<\/code>\u3001\u6807\u51c6\u5e93\u76ee\u5f55\u548c <code>site-packages<\/code> \u76ee\u5f55\u3002\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539 <code>sys.path<\/code> \u52a8\u6001\u5730\u6539\u53d8\u6a21\u5757\u641c\u7d22\u8def\u5f84\uff0c\u4ece\u800c\u6ee1\u8db3\u4e0d\u540c\u9879\u76ee\u7684\u9700\u6c42\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5408\u7406\u914d\u7f6e\u6a21\u5757\u641c\u7d22\u8def\u5f84\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u79fb\u690d\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6b63\u786e\u5bfc\u5165\u4e00\u4e2a.py\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5bfc\u5165\u4e00\u4e2a.py\u6587\u4ef6\u901a\u5e38\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u3002\u786e\u4fdd\u8be5.py\u6587\u4ef6\u4f4d\u4e8ePython\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84\u4e2d\uff0c\u6216\u8005\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7<code>import module_name<\/code>\u6765\u5bfc\u5165\u5176\u4e2d\u7684\u5185\u5bb9\uff0c<code>module_name<\/code>\u662f\u4f60\u8981\u5bfc\u5165\u7684\u6587\u4ef6\u540d\uff08\u4e0d\u5305\u62ec.py\u540e\u7f00\uff09\u3002\u5982\u679c\u9700\u8981\u7279\u5b9a\u7684\u51fd\u6570\u6216\u7c7b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>from module_name import function_name<\/code>\u7684\u5f62\u5f0f\u3002<\/p>\n<p><strong>\u5bfc\u5165\u7684.py\u6587\u4ef6\u9700\u8981\u6ee1\u8db3\u4ec0\u4e48\u6761\u4ef6\uff1f<\/strong><br \/>\u8981\u6210\u529f\u5bfc\u5165\u4e00\u4e2a.py\u6587\u4ef6\uff0c\u8be5\u6587\u4ef6\u5fc5\u987b\u662f\u6709\u6548\u7684Python\u4ee3\u7801\uff0c\u5e76\u4e14\u80fd\u591f\u5728\u5f53\u524d\u73af\u5883\u4e2d\u88ab\u8bc6\u522b\u3002\u786e\u4fdd\u6587\u4ef6\u540d\u9075\u5faaPython\u547d\u540d\u89c4\u5219\uff0c\u4e0d\u5305\u542b\u7279\u6b8a\u5b57\u7b26\u6216\u7a7a\u683c\u3002\u6b64\u5916\uff0c\u5efa\u8bae\u5728\u6587\u4ef6\u4e2d\u5305\u542b<code>__init__.py<\/code>\u6587\u4ef6\uff0c\u4ee5\u4fbf\u4f7f\u5176\u88ab\u89c6\u4f5c\u4e00\u4e2a\u5305\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u591a\u4e2a\u6a21\u5757\u65f6\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u5728\u5bfc\u5165.py\u6587\u4ef6\u65f6\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u5bfc\u5165.py\u6587\u4ef6\u662f\u53ef\u884c\u7684\u3002\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u6587\u4ef6\u540d\u8fdb\u884c\u5bfc\u5165\uff1b\u5982\u679c\u5728\u5b50\u76ee\u5f55\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>.<\/code>\u548c<code>..<\/code>\u6765\u8868\u793a\u5f53\u524d\u76ee\u5f55\u548c\u4e0a\u7ea7\u76ee\u5f55\u3002\u4f8b\u5982\uff0c<code>from ..subfolder import module_name<\/code>\u53ef\u4ee5\u7528\u4e8e\u4ece\u4e0a\u7ea7\u76ee\u5f55\u7684\u5b50\u6587\u4ef6\u5939\u4e2d\u5bfc\u5165\u6a21\u5757\u3002\u786e\u4fdd\u5728\u8fd0\u884c\u811a\u672c\u65f6\uff0c\u5de5\u4f5c\u76ee\u5f55\u8bbe\u7f6e\u6b63\u786e\uff0c\u4ee5\u4fbfPython\u80fd\u591f\u627e\u5230\u76f8\u5bf9\u8def\u5f84\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u8ba4\u5b9a\u5bfc\u5165 .py \u6587\u4ef6\u7684\u65b9\u5f0f\u6d89\u53ca\u51e0\u4e2a\u6838\u5fc3\u8981\u70b9\uff0c\u5305\u62ec\uff1a\u6a21\u5757\u641c\u7d22\u8def\u5f84\u3001import \u8bed\u53e5\u3001__ini [&hellip;]","protected":false},"author":3,"featured_media":1184250,"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\/1184244"}],"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=1184244"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1184244\/revisions"}],"predecessor-version":[{"id":1184252,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1184244\/revisions\/1184252"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1184250"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1184244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1184244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1184244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}