{"id":1086431,"date":"2025-01-08T13:25:36","date_gmt":"2025-01-08T05:25:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1086431.html"},"modified":"2025-01-08T13:25:39","modified_gmt":"2025-01-08T05:25:39","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%bc%95%e7%94%a8%e8%87%aa%e5%b7%b1%e5%86%99%e7%9a%84-py-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1086431.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5f15\u7528\u81ea\u5df1\u5199\u7684.py"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195844\/c0af1d49-113b-4ad9-b17b-48fc6ee5ee9b.webp\" alt=\"python\u4e2d\u5982\u4f55\u5f15\u7528\u81ea\u5df1\u5199\u7684.py\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5f15\u7528\u81ea\u5df1\u5199\u7684.py\u6587\u4ef6\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528import\u8bed\u53e5\u3001\u5c06.py\u6587\u4ef6\u4f5c\u4e3a\u6a21\u5757\u653e\u5728\u540c\u4e00\u4e2a\u76ee\u5f55\u3001\u4f7f\u7528Python\u8def\u5f84\u914d\u7f6e\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u53ef\u4ee5\u4f7f\u4f60\u5728\u4e00\u4e2aPython\u811a\u672c\u4e2d\u5f15\u7528\u53e6\u4e00\u4e2a.py\u6587\u4ef6\u3002<\/strong> \u5176\u4e2d\uff0c\u5c06.py\u6587\u4ef6\u4f5c\u4e3a\u6a21\u5757\u653e\u5728\u540c\u4e00\u4e2a\u76ee\u5f55\u662f\u4e00\u79cd\u8f83\u4e3a\u5e38\u89c1\u548c\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u4f60\u53ea\u9700\u8981\u5c06\u4f60\u8981\u5f15\u7528\u7684.py\u6587\u4ef6\u653e\u5728\u4f60\u5f53\u524d\u5de5\u4f5c\u7684\u76ee\u5f55\u4e0b\uff0c\u7136\u540e\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u8fdb\u884c\u5f15\u7528\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a<code>my_module.py<\/code>\u7684\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># my_module.py<\/p>\n<p>def hello():<\/p>\n<p>    print(&quot;Hello, world!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u4f60\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\u521b\u5efa\u53e6\u4e00\u4e2aPython\u811a\u672c<code>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/code>\uff0c\u5e76\u5728\u5176\u4e2d\u5f15\u7528<code>my_module.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import my_module<\/p>\n<p>my_module.hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c<code>main.py<\/code>\uff0c\u4f60\u5c06\u770b\u5230\u8f93\u51fa<code>Hello, world!<\/code>\u3002\u8fd9\u5c31\u662f\u6700\u57fa\u672c\u7684\u5f15\u7528\u81ea\u5df1\u5199\u7684.py\u6587\u4ef6\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528import\u8bed\u53e5<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>import<\/code>\u8bed\u53e5\u662f\u6700\u5e38\u89c1\u7684\u7528\u4e8e\u5f15\u7528\u5916\u90e8.py\u6587\u4ef6\u7684\u65b9\u6cd5\u3002\u901a\u8fc7<code>import<\/code>\u8bed\u53e5\uff0c\u4f60\u53ef\u4ee5\u5f15\u7528\u4f60\u81ea\u5df1\u5199\u7684\u4efb\u4f55.py\u6587\u4ef6\uff0c\u5e76\u5728\u4f60\u7684\u4ee3\u7801\u4e2d\u4f7f\u7528\u5176\u4e2d\u7684\u51fd\u6570\u3001\u7c7b\u548c\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># example.py<\/p>\n<p>def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<h2><strong>main.py<\/strong><\/h2>\n<p>import example<\/p>\n<p>print(example.greet(&quot;Alice&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a<code>example.py<\/code>\u7684\u6587\u4ef6\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>greet<\/code>\u3002\u7136\u540e\u5728<code>main.py<\/code>\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>import example<\/code>\u8bed\u53e5\u5f15\u7528\u4e86<code>example.py<\/code>\uff0c\u5e76\u8c03\u7528\u4e86\u5176\u4e2d\u7684<code>greet<\/code>\u51fd\u6570\u3002\u8fd0\u884c<code>main.py<\/code>\uff0c\u4f60\u5c06\u770b\u5230\u8f93\u51fa<code>Hello, Alice!<\/code>\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0import\u8bed\u53e5\u7684\u4f7f\u7528\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p><code>import<\/code>\u8bed\u53e5\u7684\u4f7f\u7528\u975e\u5e38\u7075\u6d3b\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5f15\u7528\u6574\u4e2a\u6a21\u5757\u6216\u8005\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u90e8\u5206\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684<code>import<\/code>\u8bed\u53e5\u7684\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5f15\u7528\u6574\u4e2a\u6a21\u5757<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import module_name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5f15\u7528\u6574\u4e2a\u6a21\u5757\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>module_name.function_name<\/code>\u6216<code>module_name.class_name<\/code>\u7684\u65b9\u5f0f\u8bbf\u95ee\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u548c\u7c7b\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u5f15\u7528\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u90e8\u5206<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from module_name import function_name, class_name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u4f60\u76f4\u63a5\u5f15\u7528\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u51fd\u6570\u6216\u7c7b\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528<code>module_name.<\/code>\u524d\u7f00\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from example import greet<\/p>\n<p>print(greet(&quot;Alice&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u4f7f\u7528\u522b\u540d<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import module_name as alias<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u522b\u540d\u53ef\u4ee5\u7b80\u5316\u6a21\u5757\u7684\u8bbf\u95ee\u65b9\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import example as ex<\/p>\n<p>print(ex.greet(&quot;Alice&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5c06.py\u6587\u4ef6\u4f5c\u4e3a\u6a21\u5757\u653e\u5728\u540c\u4e00\u4e2a\u76ee\u5f55<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u6709\u591a\u4e2a.py\u6587\u4ef6\u9700\u8981\u4e92\u76f8\u5f15\u7528\uff0c\u5c06\u5b83\u4eec\u653e\u5728\u540c\u4e00\u4e2a\u76ee\u5f55\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u3002Python\u4f1a\u81ea\u52a8\u5c06\u5f53\u524d\u76ee\u5f55\u6dfb\u52a0\u5230\u641c\u7d22\u8def\u5f84\u4e2d\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5f15\u7528\u8fd9\u4e9b\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module1.py<\/p>\n<p>def foo():<\/p>\n<p>    return &quot;foo from module1&quot;<\/p>\n<h2><strong>module2.py<\/strong><\/h2>\n<p>def bar():<\/p>\n<p>    return &quot;bar from module2&quot;<\/p>\n<h2><strong>main.py<\/strong><\/h2>\n<p>import module1<\/p>\n<p>import module2<\/p>\n<p>print(module1.foo())<\/p>\n<p>print(module2.bar())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6709\u4e09\u4e2a\u6587\u4ef6<code>module1.py<\/code>\u3001<code>module2.py<\/code>\u548c<code>main.py<\/code>\u3002<code>main.py<\/code>\u5f15\u7528\u4e86<code>module1.py<\/code>\u548c<code>module2.py<\/code>\uff0c\u5e76\u8c03\u7528\u4e86\u5176\u4e2d\u7684\u51fd\u6570\u3002\u8fd0\u884c<code>main.py<\/code>\uff0c\u4f60\u5c06\u770b\u5230\u8f93\u51fa<code>foo from module1<\/code>\u548c<code>bar from module2<\/code>\u3002<\/p>\n<\/p>\n<p><p><strong>\u521b\u5efa\u5305<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u9879\u76ee\u53d8\u5f97\u66f4\u5927\uff0c\u5305\u542b\u591a\u4e2a\u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u6a21\u5757\u7ec4\u7ec7\u5230\u5305\u4e2d\u3002\u5305\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u76ee\u5f55\uff0c\u76ee\u5f55\u4e0b\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u6587\u4ef6<code>__init__.py<\/code>\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5305\u7684\u7ed3\u6784\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code>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>\u4f60\u53ef\u4ee5\u5728<code>__init__.py<\/code>\u4e2d\u5b9a\u4e49\u5305\u7684\u516c\u5171\u63a5\u53e3\uff0c\u7136\u540e\u5728\u5176\u4ed6\u4ee3\u7801\u4e2d\u5f15\u7528\u8fd9\u4e2a\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># my_package\/module1.py<\/p>\n<p>def foo():<\/p>\n<p>    return &quot;foo from module1&quot;<\/p>\n<h2><strong>my_package\/module2.py<\/strong><\/h2>\n<p>def bar():<\/p>\n<p>    return &quot;bar from module2&quot;<\/p>\n<h2><strong>my_package\/__init__.py<\/strong><\/h2>\n<p>from .module1 import foo<\/p>\n<p>from .module2 import bar<\/p>\n<h2><strong>main.py<\/strong><\/h2>\n<p>import my_package<\/p>\n<p>print(my_package.foo())<\/p>\n<p>print(my_package.bar())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>__init__.py<\/code>\u6587\u4ef6\u5b9a\u4e49\u4e86\u5305<code>my_package<\/code>\u7684\u516c\u5171\u63a5\u53e3\uff0c\u5b83\u5f15\u7528\u4e86<code>module1.py<\/code>\u4e2d\u7684<code>foo<\/code>\u51fd\u6570\u548c<code>module2.py<\/code>\u4e2d\u7684<code>bar<\/code>\u51fd\u6570\u3002\u7136\u540e\u5728<code>main.py<\/code>\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5f15\u7528<code>my_package<\/code>\u5e76\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528Python\u8def\u5f84\u914d\u7f6e<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684.py\u6587\u4ef6\u4e0d\u5728\u5f53\u524d\u76ee\u5f55\u6216\u5305\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539Python\u7684\u641c\u7d22\u8def\u5f84\u6765\u5f15\u7528\u5b83\u4eec\u3002\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4fee\u6539<code>sys.path<\/code><\/li>\n<\/ol>\n<p><p><code>sys.path<\/code>\u662fPython\u7684\u641c\u7d22\u8def\u5f84\u5217\u8868\uff0c\u4f60\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u4fee\u6539\u5b83\u6765\u5305\u542b\u4f60\u60f3\u8981\u5f15\u7528\u7684.py\u6587\u4ef6\u7684\u8def\u5f84\u3002\u4f8b\u5982\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>import your_module<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u4f60\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u6dfb\u52a0\u641c\u7d22\u8def\u5f84\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u73af\u5883\u53d8\u91cf<code>PYTHONPATH<\/code><\/li>\n<\/ol>\n<p><p>\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<code>PYTHONPATH<\/code>\u6765\u5305\u542b\u4f60\u60f3\u8981\u5f15\u7528\u7684.py\u6587\u4ef6\u7684\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export PYTHONPATH=\/path\/to\/your\/module<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728\u4f60\u7684Python\u4ee3\u7801\u4e2d\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u5f15\u7528\u8fd9\u4e9b\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import your_module<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u4f7f\u7528.pth\u6587\u4ef6<\/li>\n<\/ol>\n<p><p>\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a.pth\u6587\u4ef6\uff0c\u5c06\u4f60\u60f3\u8981\u5f15\u7528\u7684.py\u6587\u4ef6\u7684\u8def\u5f84\u6dfb\u52a0\u5230\u8fd9\u4e2a\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u5c06.pth\u6587\u4ef6\u653e\u5728Python\u7684<code>site-packages<\/code>\u76ee\u5f55\u4e2d\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a<code>my_paths.pth<\/code>\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>\/path\/to\/your\/module<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c06\u8fd9\u4e2a\u6587\u4ef6\u653e\u5728<code>site-packages<\/code>\u76ee\u5f55\u4e2d\uff0c\u7136\u540e\u5728\u4f60\u7684Python\u4ee3\u7801\u4e2d\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u5f15\u7528\u8fd9\u4e9b\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import your_module<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5f15\u7528\u81ea\u5df1\u5199\u7684.py\u6587\u4ef6\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6700\u5e38\u89c1\u7684\u662f\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u3002\u4f60\u53ef\u4ee5\u5c06.py\u6587\u4ef6\u4f5c\u4e3a\u6a21\u5757\u653e\u5728\u540c\u4e00\u4e2a\u76ee\u5f55\u4e2d\uff0c\u4e5f\u53ef\u4ee5\u521b\u5efa\u5305\u6765\u7ec4\u7ec7\u591a\u4e2a\u6a21\u5757\u3002\u5982\u679c\u4f60\u7684.py\u6587\u4ef6\u4e0d\u5728\u5f53\u524d\u76ee\u5f55\u6216\u5305\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539Python\u7684\u641c\u7d22\u8def\u5f84\u6765\u5f15\u7528\u5b83\u4eec\u3002\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u4e00\u4e2aPython\u811a\u672c\u4e2d\u5f15\u7528\u53e6\u4e00\u4e2a.py\u6587\u4ef6\uff0c\u4f7f\u4f60\u7684\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\u548c\u53ef\u91cd\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165\u81ea\u5df1\u7684\u6a21\u5757\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8981\u5bfc\u5165\u81ea\u5df1\u7f16\u5199\u7684.py\u6587\u4ef6\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u8be5\u6587\u4ef6\u4f4d\u4e8e\u4f60\u7684\u5de5\u4f5c\u76ee\u5f55\u4e2d\u6216\u8005Python\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u6765\u5bfc\u5165\u6a21\u5757\uff0c\u683c\u5f0f\u4e3a<code>import \u6a21\u5757\u540d<\/code>\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u4f7f\u7528\u8be5\u6a21\u5757\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u548c\u7c7b\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u7684.py\u6587\u4ef6\u5728\u5b50\u76ee\u5f55\u4e2d\uff0c\u5e94\u8be5\u5982\u4f55\u5f15\u7528\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u7684.py\u6587\u4ef6\u5b58\u653e\u5728\u5b50\u76ee\u5f55\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u6216\u7edd\u5bf9\u5bfc\u5165\u3002\u7edd\u5bf9\u5bfc\u5165\u662f\u6307\u4f7f\u7528\u5b8c\u6574\u7684\u5305\u8def\u5f84\uff0c\u4f8b\u5982<code>from \u5b50\u76ee\u5f55\u540d import \u6587\u4ef6\u540d<\/code>\u3002\u76f8\u5bf9\u5bfc\u5165\u5219\u4f7f\u7528\u70b9\uff08.\uff09\u8868\u793a\u5f53\u524d\u76ee\u5f55\u6216\u7236\u76ee\u5f55\uff0c\u6bd4\u5982<code>from .\u5b50\u76ee\u5f55\u540d import \u6587\u4ef6\u540d<\/code>\u3002\u786e\u4fdd\u5728\u6267\u884c\u4ee3\u7801\u65f6\uff0cPython\u80fd\u627e\u5230\u8be5\u5b50\u76ee\u5f55\u3002<\/p>\n<p><strong>\u5728\u5bfc\u5165\u65f6\u9047\u5230\u201cModuleNotFoundError\u201d\u8be5\u5982\u4f55\u89e3\u51b3\uff1f<\/strong><br \/>\u201cModuleNotFoundError\u201d\u901a\u5e38\u8868\u793aPython\u627e\u4e0d\u5230\u6307\u5b9a\u7684\u6a21\u5757\u3002\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u68c0\u67e5\u4ee5\u4e0b\u51e0\u70b9\uff1a\u786e\u8ba4.py\u6587\u4ef6\u7684\u540d\u79f0\u662f\u5426\u6b63\u786e\uff0c\u786e\u4fdd\u6587\u4ef6\u8def\u5f84\u5728Python\u7684\u641c\u7d22\u8def\u5f84\u4e2d\uff0c\u6216\u8005\u5728\u8fd0\u884c\u811a\u672c\u65f6\uff0c\u786e\u4fdd\u4f60\u7684\u5de5\u4f5c\u76ee\u5f55\u662f\u6b63\u786e\u7684\u3002\u53ef\u4ee5\u4f7f\u7528<code>sys.path<\/code>\u67e5\u770b\u5f53\u524d\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84\uff0c\u5fc5\u8981\u65f6\u53ef\u4ee5\u901a\u8fc7<code>sys.path.append()<\/code>\u6dfb\u52a0\u65b0\u7684\u8def\u5f84\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5f15\u7528\u81ea\u5df1\u5199\u7684.py\u6587\u4ef6\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528import\u8bed\u53e5\u3001\u5c06.py\u6587\u4ef6\u4f5c\u4e3a\u6a21\u5757\u653e\u5728\u540c\u4e00\u4e2a\u76ee\u5f55 [&hellip;]","protected":false},"author":3,"featured_media":1086440,"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\/1086431"}],"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=1086431"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1086431\/revisions"}],"predecessor-version":[{"id":1086441,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1086431\/revisions\/1086441"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1086440"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1086431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1086431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1086431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}