{"id":1104762,"date":"2025-01-08T16:24:02","date_gmt":"2025-01-08T08:24:02","guid":{"rendered":""},"modified":"2025-01-08T16:24:05","modified_gmt":"2025-01-08T08:24:05","slug":"%e5%90%8c%e7%9b%ae%e5%bd%95python%e5%a6%82%e4%bd%95%e5%bc%95%e7%94%a8%e5%85%b6%e5%ae%83%e9%a1%b5%e9%9d%a2%e7%9a%84%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1104762.html","title":{"rendered":"\u540c\u76ee\u5f55python\u5982\u4f55\u5f15\u7528\u5176\u5b83\u9875\u9762\u7684\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065952\/4793ab7d-3770-4cbd-89d2-5602d8e8067b.webp\" alt=\"\u540c\u76ee\u5f55python\u5982\u4f55\u5f15\u7528\u5176\u5b83\u9875\u9762\u7684\u51fd\u6570\" \/><\/p>\n<p><p> \u5728\u540c\u4e00\u76ee\u5f55\u4e2d\u5f15\u7528\u5176\u4ed6\u9875\u9762\u7684\u51fd\u6570\u975e\u5e38\u7b80\u5355\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u7684import\u8bed\u53e5\u6765\u5bfc\u5165\u4f60\u9700\u8981\u7684\u51fd\u6570\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u786e\u4fdd\u6587\u4ef6\u5728\u540c\u4e00\u76ee\u5f55\u3001\u4f7f\u7528\u6b63\u786e\u7684\u5bfc\u5165\u8bed\u6cd5\u3001\u907f\u514d\u547d\u540d\u51b2\u7a81\u3001\u7406\u89e3\u76f8\u5bf9\u5bfc\u5165\u3002<strong>\u786e\u4fdd\u6587\u4ef6\u5728\u540c\u4e00\u76ee\u5f55\u3001\u4f7f\u7528\u6b63\u786e\u7684\u5bfc\u5165\u8bed\u6cd5\u3001\u907f\u514d\u547d\u540d\u51b2\u7a81\u3001\u7406\u89e3\u76f8\u5bf9\u5bfc\u5165<\/strong>\u3002\u4e0b\u9762\u8be6\u7ec6\u8bb2\u89e3\u5176\u4e2d\u7684\u4e00\u4e2a\u8981\u70b9\uff1a<strong>\u786e\u4fdd\u6587\u4ef6\u5728\u540c\u4e00\u76ee\u5f55<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u786e\u4fdd\u6587\u4ef6\u5728\u540c\u4e00\u76ee\u5f55\u662f\u6700\u57fa\u7840\u7684\u4e00\u6b65\u3002\u4f60\u9700\u8981\u786e\u4fdd\u8981\u5f15\u7528\u7684\u51fd\u6570\u6240\u5728\u7684Python\u6587\u4ef6\u548c\u4f60\u5f53\u524d\u7684Python\u6587\u4ef6\u5728\u540c\u4e00\u4e2a\u76ee\u5f55\u4e0b\u3002\u8fd9\u610f\u5473\u7740\u5b83\u4eec\u90fd\u5e94\u8be5\u4f4d\u4e8e\u540c\u4e00\u4e2a\u6587\u4ef6\u5939\u4e2d\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u76ee\u5f55\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>    \u251c\u2500\u2500 m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/p>\n<p>    \u251c\u2500\u2500 utils.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c\u6211\u4eec\u5e0c\u671b\u5728 <code>main.py<\/code> \u4e2d\u5f15\u7528 <code>utils.py<\/code> \u4e2d\u7684\u51fd\u6570\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5982\u4f55\u8fdb\u884c\u8fd9\u4e00\u6b65\u64cd\u4f5c\u4ee5\u53ca\u76f8\u5173\u7684\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u786e\u4fdd\u6587\u4ef6\u5728\u540c\u4e00\u76ee\u5f55<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u6587\u4ef6\u7ed3\u6784\u662f\u6b63\u786e\u7684\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u6587\u4ef6\uff0c<code>main.py<\/code> \u548c <code>utils.py<\/code>\uff0c\u5b83\u4eec\u90fd\u5728\u540c\u4e00\u4e2a\u76ee\u5f55\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u5728 <code>utils.py<\/code> \u4e2d\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># utils.py<\/p>\n<p>def greeting(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728 <code>main.py<\/code> \u4e2d\u5f15\u7528\u5e76\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from utils import greeting<\/p>\n<p>def main():<\/p>\n<p>    name = &quot;Alice&quot;<\/p>\n<p>    print(greeting(name))<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u6b63\u786e\u7684\u5bfc\u5165\u8bed\u6cd5<\/h3>\n<\/p>\n<p><p>\u5bfc\u5165\u5176\u4ed6\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u5bfc\u5165\u8bed\u6cd5\u3002\u901a\u8fc7 <code>import<\/code> \u548c <code>from ... import ...<\/code> \u8bed\u53e5\u6765\u5bfc\u5165\u3002<\/p>\n<\/p>\n<p><h4>\u5bfc\u5165\u6574\u4e2a\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\uff0c\u7136\u540e\u901a\u8fc7\u6a21\u5757\u540d\u6765\u8bbf\u95ee\u5176\u4e2d\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import utils<\/p>\n<p>def main():<\/p>\n<p>    name = &quot;Alice&quot;<\/p>\n<p>    print(utils.greeting(name))<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5bfc\u5165\u7279\u5b9a\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u6216\u8005\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u5bfc\u5165\u7279\u5b9a\u7684\u51fd\u6570\uff0c\u8fd9\u6837\u4f60\u5c31\u4e0d\u9700\u8981\u4f7f\u7528\u6a21\u5757\u540d\u4f5c\u4e3a\u524d\u7f00\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from utils import greeting<\/p>\n<p>def main():<\/p>\n<p>    name = &quot;Alice&quot;<\/p>\n<p>    print(greeting(name))<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u907f\u514d\u547d\u540d\u51b2\u7a81<\/h3>\n<\/p>\n<p><p>\u907f\u514d\u547d\u540d\u51b2\u7a81\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u6b65\u9aa4\u3002\u5982\u679c\u4e24\u4e2a\u6a21\u5757\u4e2d\u6709\u540c\u540d\u7684\u51fd\u6570\u6216\u53d8\u91cf\uff0c\u53ef\u80fd\u4f1a\u5f15\u8d77\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u522b\u540d<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u522b\u540d\u6765\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from utils import greeting as utils_greeting<\/p>\n<p>def main():<\/p>\n<p>    name = &quot;Alice&quot;<\/p>\n<p>    print(utils_greeting(name))<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c\u5373\u4f7f\u5728\u53e6\u4e00\u4e2a\u6a21\u5757\u4e2d\u6709\u4e00\u4e2a\u540c\u540d\u7684\u51fd\u6570\uff0c\u4e5f\u4e0d\u4f1a\u53d1\u751f\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7406\u89e3\u76f8\u5bf9\u5bfc\u5165<\/h3>\n<\/p>\n<p><p>\u76f8\u5bf9\u5bfc\u5165\u5bf9\u4e8e\u5927\u578b\u9879\u76ee\u5c24\u5176\u91cd\u8981\u3002\u5728\u590d\u6742\u7684\u9879\u76ee\u4e2d\uff0c\u76ee\u5f55\u7ed3\u6784\u53ef\u80fd\u975e\u5e38\u590d\u6742\uff0c\u4e86\u89e3\u5982\u4f55\u8fdb\u884c\u76f8\u5bf9\u5bfc\u5165\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u76ee\u5f55\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>    \u251c\u2500\u2500 main.py<\/p>\n<p>    \u251c\u2500\u2500 utils\/<\/p>\n<p>    \u2502   \u251c\u2500\u2500 __init__.py<\/p>\n<p>    \u2502   \u251c\u2500\u2500 helpers.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728 <code>helpers.py<\/code> \u4e2d\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># utils\/helpers.py<\/p>\n<p>def greeting(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728 <code>main.py<\/code> \u4e2d\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from utils.helpers import greeting<\/p>\n<p>def main():<\/p>\n<p>    name = &quot;Alice&quot;<\/p>\n<p>    print(greeting(name))<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u5bfc\u5165\u9519\u8bef<\/h3>\n<\/p>\n<p><p>\u5373\u4f7f\u6309\u7167\u4e0a\u8ff0\u6b65\u9aa4\u64cd\u4f5c\uff0c\u6709\u65f6\u5019\u4e5f\u53ef\u80fd\u9047\u5230\u5bfc\u5165\u9519\u8bef\u3002\u8fd9\u4e9b\u9519\u8bef\u901a\u5e38\u662f\u7531\u6a21\u5757\u8def\u5f84\u9519\u8bef\u6216\u547d\u540d\u51b2\u7a81\u5f15\u8d77\u7684\u3002<\/p>\n<\/p>\n<p><h4>\u5e38\u89c1\u9519\u8bef\u53ca\u89e3\u51b3\u65b9\u6848<\/h4>\n<\/p>\n<ol>\n<li><strong>ModuleNotFoundError<\/strong>: \u786e\u4fdd\u6587\u4ef6\u8def\u5f84\u548c\u540d\u79f0\u6b63\u786e\u3002\u68c0\u67e5\u62fc\u5199\u9519\u8bef\uff0c\u5e76\u786e\u4fdd\u6587\u4ef6\u5728\u540c\u4e00\u76ee\u5f55\u4e2d\u3002<\/li>\n<li><strong>ImportError<\/strong>: \u68c0\u67e5\u662f\u5426\u6b63\u786e\u4f7f\u7528\u4e86\u76f8\u5bf9\u5bfc\u5165\u548c\u7edd\u5bf9\u5bfc\u5165\u3002\u5c1d\u8bd5\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u6216\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u8fd0\u884c\u811a\u672c\u3002<\/li>\n<li><strong>NameError<\/strong>: \u786e\u4fdd\u51fd\u6570\u6216\u53d8\u91cf\u540d\u6b63\u786e\u3002\u68c0\u67e5\u662f\u5426\u6709\u547d\u540d\u51b2\u7a81\u3002<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u4f7f\u7528\u865a\u62df\u73af\u5883\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7ba1\u7406\u4f9d\u8d56\u9879\uff0c\u5e76\u907f\u514d\u547d\u540d\u51b2\u7a81\u548c\u7248\u672c\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efa\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m venv venv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<ul>\n<li>\u5728Windows\u4e0a:<\/li>\n<\/ul>\n<p><pre><code class=\"language-bash\">venv\\Scripts\\activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u5728macOS\u548cLinux\u4e0a:<\/li>\n<\/ul>\n<p><pre><code class=\"language-bash\">source venv\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5b89\u88c5\u4f9d\u8d56\u9879<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install -r requirements.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u547d\u540d\u7a7a\u95f4<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u9879\u76ee\uff0c\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7ec4\u7ec7\u4ee3\u7801\uff0c\u5e76\u907f\u514d\u547d\u540d\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efa\u547d\u540d\u7a7a\u95f4<\/h4>\n<\/p>\n<p><p>\u5728\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a <code>__init__.py<\/code> \u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>    \u251c\u2500\u2500 main.py<\/p>\n<p>    \u251c\u2500\u2500 utils\/<\/p>\n<p>    \u2502   \u251c\u2500\u2500 __init__.py<\/p>\n<p>    \u2502   \u251c\u2500\u2500 helpers.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728 <code>helpers.py<\/code> \u4e2d\u5b9a\u4e49\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># utils\/helpers.py<\/p>\n<p>def greeting(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728 <code>main.py<\/code> \u4e2d\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from utils.helpers import greeting<\/p>\n<p>def main():<\/p>\n<p>    name = &quot;Alice&quot;<\/p>\n<p>    print(greeting(name))<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u5916\u90e8\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5f15\u7528\u5916\u90e8\u5e93\u4e2d\u7684\u51fd\u6570\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>pip<\/code> \u6765\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff0c\u5e76\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5\u5916\u90e8\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u5916\u90e8\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import requests<\/p>\n<p>def fetch_data(url):<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    return response.text<\/p>\n<p>def main():<\/p>\n<p>    url = &quot;https:\/\/example.com&quot;<\/p>\n<p>    print(fetch_data(url))<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u8c03\u8bd5\u5bfc\u5165\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u8c03\u8bd5\u5bfc\u5165\u95ee\u9898\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5de5\u5177\u548c\u65b9\u6cd5\u6765\u5e2e\u52a9\u4f60\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u8c03\u8bd5\u5668<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Python\u81ea\u5e26\u7684\u8c03\u8bd5\u5668 <code>pdb<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import pdb<\/p>\n<p>from utils.helpers import greeting<\/p>\n<p>def main():<\/p>\n<p>    pdb.set_trace()<\/p>\n<p>    name = &quot;Alice&quot;<\/p>\n<p>    print(greeting(name))<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528IDE<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u73b0\u4ee3\u7684IDE\uff08\u5982PyCharm\u3001VSCode\uff09\u53ef\u4ee5\u63d0\u4f9b\u66f4\u5f3a\u5927\u7684\u8c03\u8bd5\u548c\u81ea\u52a8\u8865\u5168\u529f\u80fd\uff0c\u5e2e\u52a9\u4f60\u66f4\u5feb\u5730\u89e3\u51b3\u5bfc\u5165\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u540c\u4e00\u76ee\u5f55\u4e2d\u5f15\u7528\u5176\u4ed6\u9875\u9762\u7684\u51fd\u6570\u662f\u4e00\u4e2a\u5e38\u89c1\u4e14\u91cd\u8981\u7684\u4efb\u52a1\u3002\u901a\u8fc7\u786e\u4fdd\u6587\u4ef6\u5728\u540c\u4e00\u76ee\u5f55\u3001\u4f7f\u7528\u6b63\u786e\u7684\u5bfc\u5165\u8bed\u6cd5\u3001\u907f\u514d\u547d\u540d\u51b2\u7a81\u3001\u7406\u89e3\u76f8\u5bf9\u5bfc\u5165\u3001\u5904\u7406\u5bfc\u5165\u9519\u8bef\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883\u3001\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u3001\u4f7f\u7528\u5916\u90e8\u5e93\u4ee5\u53ca\u8c03\u8bd5\u5bfc\u5165\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u7ba1\u7406\u548c\u7ec4\u7ec7\u4f60\u7684\u4ee3\u7801\uff0c\u4ece\u800c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\u7684\u4e0d\u540cPython\u6587\u4ef6\u4e2d\u5f15\u7528\u51fd\u6570\uff1f<\/strong><br \/>\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u6765\u5f15\u7528\u5176\u4ed6Python\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u8981\u5f15\u7528\u7684\u51fd\u6570\u6240\u5728\u7684\u6587\u4ef6\u6709\u4e00\u4e2a\u6709\u6548\u7684Python\u6a21\u5757\u540d\uff08\u5373\u6587\u4ef6\u540d\u4e0d\u80fd\u4ee5\u6570\u5b57\u5f00\u5934\uff0c\u4e14\u4e0d\u80fd\u5305\u542b\u7a7a\u683c\u7b49\u7279\u6b8a\u5b57\u7b26\uff09\u3002\u7136\u540e\u5728\u4f60\u7684\u4e3b\u6587\u4ef6\u4e2d\uff0c\u4f7f\u7528<code>from filename import function_name<\/code>\u7684\u65b9\u5f0f\u6765\u5bfc\u5165\u6240\u9700\u7684\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u7684\u6587\u4ef6\u540d\u662f<code>utils.py<\/code>\uff0c\u5e76\u4e14\u4f60\u60f3\u5bfc\u5165\u540d\u4e3a<code>my_function<\/code>\u7684\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<code>from utils import my_function<\/code>\u3002<\/p>\n<p><strong>\u5f15\u7528\u51fd\u6570\u65f6\u662f\u5426\u9700\u8981\u6ce8\u610f\u6a21\u5757\u7684\u6267\u884c\u987a\u5e8f\uff1f<\/strong><br \/>\u786e\u5b9e\uff0c\u6a21\u5757\u7684\u6267\u884c\u987a\u5e8f\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4f1a\u5f71\u54cd\u51fd\u6570\u7684\u5f15\u7528\u3002\u5982\u679c\u4f60\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\u5bfc\u5165\u4e86\u53e6\u4e00\u4e2a\u6a21\u5757\uff0c\u800c\u540e\u8005\u53c8\u5bfc\u5165\u4e86\u524d\u8005\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5faa\u73af\u5bfc\u5165\u7684\u95ee\u9898\u3002\u5728\u8bbe\u8ba1\u4ee3\u7801\u7ed3\u6784\u65f6\uff0c\u5efa\u8bae\u5c3d\u91cf\u907f\u514d\u8fd9\u6837\u7684\u5faa\u73af\u4f9d\u8d56\uff0c\u786e\u4fdd\u5404\u4e2a\u6a21\u5757\u4e4b\u95f4\u7684\u5173\u7cfb\u6e05\u6670\u3002<\/p>\n<p><strong>\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\u5f15\u7528\u51fd\u6570\u662f\u5426\u4f1a\u5f71\u54cd\u6027\u80fd\uff1f<\/strong><br \/>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u4ece\u540c\u4e00\u76ee\u5f55\u4e0b\u5f15\u7528\u51fd\u6570\u4e0d\u4f1a\u663e\u8457\u5f71\u54cd\u6027\u80fd\u3002Python\u5728\u5bfc\u5165\u6a21\u5757\u65f6\u4f1a\u5c06\u5176\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\uff0c\u540e\u7eed\u7684\u5f15\u7528\u5c06\u76f4\u63a5\u4f7f\u7528\u5df2\u52a0\u8f7d\u7684\u6a21\u5757\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u521d\u6b21\u5bfc\u5165\u53ef\u80fd\u9700\u8981\u4e00\u4e9b\u65f6\u95f4\uff0c\u4f46\u540e\u7eed\u7684\u8c03\u7528\u901f\u5ea6\u662f\u76f8\u5bf9\u8f83\u5feb\u7684\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u6d89\u53ca\u5230\u5927\u91cf\u7684\u6a21\u5757\u6216\u590d\u6742\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u52a0\u8f7d\u65f6\u95f4\uff0c\u56e0\u6b64\u5408\u7406\u7ec4\u7ec7\u4ee3\u7801\u7ed3\u6784\u662f\u4f18\u5316\u6027\u80fd\u7684\u91cd\u8981\u4e00\u6b65\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728\u540c\u4e00\u76ee\u5f55\u4e2d\u5f15\u7528\u5176\u4ed6\u9875\u9762\u7684\u51fd\u6570\u975e\u5e38\u7b80\u5355\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u7684import\u8bed\u53e5\u6765\u5bfc\u5165\u4f60\u9700\u8981\u7684\u51fd\u6570\u3002\u5177\u4f53\u6b65\u9aa4\u5305 [&hellip;]","protected":false},"author":3,"featured_media":1104775,"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\/1104762"}],"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=1104762"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104762\/revisions"}],"predecessor-version":[{"id":1104781,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104762\/revisions\/1104781"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1104775"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1104762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1104762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1104762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}