{"id":1028775,"date":"2024-12-31T11:02:30","date_gmt":"2024-12-31T03:02:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1028775.html"},"modified":"2024-12-31T11:02:32","modified_gmt":"2024-12-31T03:02:32","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e4%b8%ad%e5%af%bc%e5%85%a5%e5%ba%93%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1028775.html","title":{"rendered":"\u5982\u4f55\u5728python\u4e2d\u5bfc\u5165\u5e93\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/e0f386c4-95d8-4bc5-bea1-a7dfe1b8d752.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u5728python\u4e2d\u5bfc\u5165\u5e93\u51fd\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5bfc\u5165\u5e93\u51fd\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ecimport\u8bed\u53e5\u3001from&#8230;import\u8bed\u53e5\u3001as\u5173\u952e\u5b57\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u8bb2\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5bfc\u5165\u5e93\u51fd\u6570\u975e\u5e38\u7b80\u5355\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a<strong>\u4f7f\u7528import\u8bed\u53e5\u3001\u4f7f\u7528from&#8230;import\u8bed\u53e5\u3001\u4f7f\u7528as\u5173\u952e\u5b57\u7ed9\u5bfc\u5165\u7684\u5e93\u6216\u51fd\u6570\u8d77\u522b\u540d<\/strong>\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u2014\u2014import\u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><p><strong>import\u8bed\u53e5<\/strong><\/p>\n<\/p>\n<p><p>import\u8bed\u53e5\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u5c06\u6574\u4e2a\u6a21\u5757\u5bfc\u5165\u5230\u5f53\u524d\u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6a21\u5757\u540d.\u51fd\u6570\u540d\u7684\u65b9\u5f0f\u6765\u8c03\u7528\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u548c\u53d8\u91cf\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<h2><strong>\u4f7f\u7528math\u6a21\u5757\u4e2d\u7684sqrt\u51fd\u6570<\/strong><\/h2>\n<p>result = math.sqrt(16)<\/p>\n<p>print(result)  # \u8f93\u51fa: 4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528import\u8bed\u53e5\u5c06math\u6a21\u5757\u5bfc\u5165\u5230\u5f53\u524d\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u5e76\u4f7f\u7528math.sqrt()\u51fd\u6570\u8ba1\u7b97\u4e8616\u7684\u5e73\u65b9\u6839\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\u6a21\u5757\u4e2d\u7684\u6240\u6709\u51fd\u6570\u548c\u53d8\u91cf\u90fd\u53ef\u4ee5\u901a\u8fc7\u6a21\u5757\u540d\u8fdb\u884c\u8bbf\u95ee\uff0c\u907f\u514d\u4e86\u547d\u540d\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528from&#8230;import\u8bed\u53e5<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ea\u9700\u8981\u6a21\u5757\u4e2d\u7684\u67d0\u4e2a\u7279\u5b9a\u51fd\u6570\u6216\u53d8\u91cf\uff0c\u800c\u4e0d\u9700\u8981\u6574\u4e2a\u6a21\u5757\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528from&#8230;import\u8bed\u53e5\u5c06\u6307\u5b9a\u7684\u51fd\u6570\u6216\u53d8\u91cf\u5bfc\u5165\u5230\u5f53\u524d\u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u51fd\u6570\u540d\u6216\u53d8\u91cf\u540d\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528\u6a21\u5757\u540d\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from math import sqrt<\/p>\n<h2><strong>\u76f4\u63a5\u4f7f\u7528sqrt\u51fd\u6570<\/strong><\/h2>\n<p>result = sqrt(16)<\/p>\n<p>print(result)  # \u8f93\u51fa: 4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528from math import sqrt\u8bed\u53e5\u5c06math\u6a21\u5757\u4e2d\u7684sqrt\u51fd\u6570\u5bfc\u5165\u5230\u5f53\u524d\u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528sqrt()\u51fd\u6570\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528math.sqrt()\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528as\u5173\u952e\u5b57<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6a21\u5757\u540d\u53ef\u80fd\u6bd4\u8f83\u957f\u6216\u8005\u4e0e\u5f53\u524d\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u67d0\u4e2a\u53d8\u91cf\u540d\u51b2\u7a81\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528as\u5173\u952e\u5b57\u7ed9\u5bfc\u5165\u7684\u6a21\u5757\u6216\u51fd\u6570\u8d77\u4e00\u4e2a\u522b\u540d\u3002\u8fd9\u6837\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528np\u522b\u540d\u8c03\u7528numpy\u6a21\u5757\u7684\u51fd\u6570<\/strong><\/h2>\n<p>array = np.array([1, 2, 3, 4])<\/p>\n<p>print(array)  # \u8f93\u51fa: [1 2 3 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528import numpy as np\u8bed\u53e5\u5c06numpy\u6a21\u5757\u5bfc\u5165\u5230\u5f53\u524d\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u5e76\u7ed9\u5b83\u8d77\u4e86\u4e00\u4e2a\u522b\u540dnp\u3002\u8fd9\u6837\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528np.array()\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528numpy.array()\u3002<\/p>\n<\/p>\n<p><p><strong>\u591a\u6a21\u5757\u5bfc\u5165<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u540c\u65f6\u5bfc\u5165\u591a\u4e2a\u6a21\u5757\u6216\u591a\u4e2a\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u548c\u53d8\u91cf\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u9017\u53f7\u5206\u9694\u7684\u65b9\u5f0f\u4e00\u6b21\u6027\u5bfc\u5165\u591a\u4e2a\u6a21\u5757\u6216\u51fd\u6570\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os, sys<\/p>\n<h2><strong>\u4f7f\u7528os\u6a21\u5757\u548csys\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u548c\u53d8\u91cf<\/strong><\/h2>\n<p>print(os.getcwd())  # \u8f93\u51fa\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/p>\n<p>print(sys.version)  # \u8f93\u51faPython\u7248\u672c\u4fe1\u606f<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528import os, sys\u8bed\u53e5\u4e00\u6b21\u6027\u5bfc\u5165\u4e86os\u6a21\u5757\u548csys\u6a21\u5757\u3002\u8fd9\u6837\u6211\u4eec\u53ef\u4ee5\u540c\u65f6\u4f7f\u7528os\u6a21\u5757\u548csys\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u548c\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><p><strong>\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u9664\u4e86\u5bfc\u5165Python\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93\u4e2d\u7684\u6a21\u5757\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757\u3002\u81ea\u5b9a\u4e49\u6a21\u5757\u662f\u7531\u6211\u4eec\u81ea\u5df1\u7f16\u5199\u7684Python\u6587\u4ef6\uff0c\u5b83\u4eec\u901a\u5e38\u5305\u542b\u4e00\u4e9b\u51fd\u6570\u548c\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u5176\u4ed6Python\u6587\u4ef6\u4e2d\u5bfc\u5165\u548c\u4f7f\u7528\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6a21\u5757my_module.py\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># my_module.py<\/p>\n<p>def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5728\u53e6\u4e00\u4e2aPython\u6587\u4ef6\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u8fd9\u4e2a\u81ea\u5b9a\u4e49\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/p>\n<p>import my_module<\/p>\n<h2><strong>\u4f7f\u7528my_module\u6a21\u5757\u4e2d\u7684greet\u51fd\u6570<\/strong><\/h2>\n<p>message = my_module.greet(&quot;Alice&quot;)<\/p>\n<p>print(message)  # \u8f93\u51fa: Hello, Alice!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6a21\u5757my_module.py\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2agreet\u51fd\u6570\u3002\u7136\u540e\uff0c\u6211\u4eec\u5728main.py\u6587\u4ef6\u4e2d\u4f7f\u7528import my_module\u8bed\u53e5\u5c06\u81ea\u5b9a\u4e49\u6a21\u5757\u5bfc\u5165\u5230\u5f53\u524d\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u5e76\u4f7f\u7528my_module.greet()\u51fd\u6570\u751f\u6210\u4e86\u4e00\u6761\u95ee\u5019\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><p><strong>\u5bfc\u5165\u5305\u4e2d\u7684\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5305\u662f\u4e00\u79cd\u7ec4\u7ec7\u6a21\u5757\u7684\u65b9\u5f0f\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u5c06\u591a\u4e2a\u6a21\u5757\u653e\u5728\u4e00\u4e2a\u76ee\u5f55\u4e2d\uff0c\u5e76\u901a\u8fc7\u76ee\u5f55\u7ed3\u6784\u6765\u7ec4\u7ec7\u5b83\u4eec\u3002\u5305\u901a\u5e38\u5305\u542b\u4e00\u4e2a__init__.py\u6587\u4ef6\uff0c\u8fd9\u4e2a\u6587\u4ef6\u53ef\u4ee5\u662f\u7a7a\u7684\uff0c\u4f46\u5b83\u7684\u5b58\u5728\u544a\u8bc9Python\u8fd9\u4e2a\u76ee\u5f55\u5e94\u8be5\u88ab\u89c6\u4e3a\u4e00\u4e2a\u5305\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u5305my_package\uff0c\u5176\u4e2d\u5305\u542b\u4e24\u4e2a\u6a21\u5757module1.py\u548cmodule2.py\uff1a<\/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>\u5728module1.py\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module1.py<\/p>\n<p>def func1():<\/p>\n<p>    return &quot;This is function 1 from module 1&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728module2.py\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u53e6\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module2.py<\/p>\n<p>def func2():<\/p>\n<p>    return &quot;This is function 2 from module 2&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5728\u53e6\u4e00\u4e2aPython\u6587\u4ef6\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u5305\u4e2d\u7684\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from my_package import module1, module2<\/p>\n<h2><strong>\u4f7f\u7528module1\u548cmodule2\u4e2d\u7684\u51fd\u6570<\/strong><\/h2>\n<p>print(module1.func1())  # \u8f93\u51fa: This is function 1 from module 1<\/p>\n<p>print(module2.func2())  # \u8f93\u51fa: This is function 2 from module 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305my_package\uff0c\u5176\u4e2d\u5305\u542b\u4e24\u4e2a\u6a21\u5757module1.py\u548cmodule2.py\u3002\u7136\u540e\uff0c\u6211\u4eec\u5728main.py\u6587\u4ef6\u4e2d\u4f7f\u7528from my_package import module1, module2\u8bed\u53e5\u5c06\u5305\u4e2d\u7684\u6a21\u5757\u5bfc\u5165\u5230\u5f53\u524d\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u5e76\u4f7f\u7528module1.func1()\u548cmodule2.func2()\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u6a21\u5757\u7684\u76f8\u5bf9\u5bfc\u5165<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5305\u7684\u5185\u90e8\uff0c\u6709\u65f6\u6211\u4eec\u9700\u8981\u5728\u4e0d\u540c\u6a21\u5757\u4e4b\u95f4\u8fdb\u884c\u76f8\u5bf9\u5bfc\u5165\u3002\u76f8\u5bf9\u5bfc\u5165\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u6765\u5bfc\u5165\u540c\u4e00\u5305\u4e2d\u7684\u5176\u4ed6\u6a21\u5757\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u5305\u7ed3\u6784\uff1a<\/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>\u5728module1.py\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module1.py<\/p>\n<p>def func1():<\/p>\n<p>    return &quot;This is function 1 from module 1&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728module2.py\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u5bfc\u5165module1\u4e2d\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module2.py<\/p>\n<p>from .module1 import func1<\/p>\n<p>def func2():<\/p>\n<p>    return f&quot;Function 2 calls: {func1()}&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5728\u53e6\u4e00\u4e2aPython\u6587\u4ef6\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528module2\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from my_package import module2<\/p>\n<h2><strong>\u4f7f\u7528module2\u4e2d\u7684\u51fd\u6570<\/strong><\/h2>\n<p>print(module2.func2())  # \u8f93\u51fa: Function 2 calls: This is function 1 from module 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528from .module1 import func1\u8bed\u53e5\u5728module2.py\u4e2d\u5bfc\u5165\u4e86module1\u4e2d\u7684func1\u51fd\u6570\u3002\u8fd9\u662f\u4e00\u4e2a\u76f8\u5bf9\u5bfc\u5165\uff0c\u56e0\u4e3a\u5b83\u4f7f\u7528\u4e86\u76f8\u5bf9\u8def\u5f84\uff08\u5373\u5f53\u524d\u5305\u4e2d\u7684module1\u6a21\u5757\uff09\u3002\u7136\u540e\uff0c\u6211\u4eec\u5728func2\u51fd\u6570\u4e2d\u8c03\u7528\u4e86func1\u51fd\u6570\uff0c\u5e76\u5728main.py\u6587\u4ef6\u4e2d\u4f7f\u7528module2.func2()\u51fd\u6570\u751f\u6210\u4e86\u4e00\u6761\u5305\u542bfunc1\u51fd\u6570\u8f93\u51fa\u7684\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5bfc\u5165\u5e93\u51fd\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ecimport\u8bed\u53e5\u3001from&#8230;import\u8bed\u53e5\u3001as\u5173\u952e\u5b57\u3001\u591a\u6a21\u5757\u5bfc\u5165\u3001\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757\u3001\u5bfc\u5165\u5305\u4e2d\u7684\u6a21\u5757\u4ee5\u53ca\u6a21\u5757\u7684\u76f8\u5bf9\u5bfc\u5165\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u7279\u5b9a\u7684\u7528\u9014\u548c\u4f18\u70b9\uff0c\u9009\u62e9\u9002\u5f53\u7684\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u5bfc\u5165\u65b9\u5f0f\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><p><strong>\u8865\u5145\u8bf4\u660e<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u6700\u4f73\u5b9e\u8df5\u6765\u5bfc\u5165\u5e93\u51fd\u6570\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165\u6807\u51c6\u5e93\u6a21\u5757<\/strong>\uff1a\u9996\u5148\u5bfc\u5165Python\u6807\u51c6\u5e93\u4e2d\u7684\u6a21\u5757\u3002<\/li>\n<li><strong>\u5bfc\u5165\u7b2c\u4e09\u65b9\u5e93\u6a21\u5757<\/strong>\uff1a\u63a5\u4e0b\u6765\u5bfc\u5165\u7b2c\u4e09\u65b9\u5e93\u4e2d\u7684\u6a21\u5757\u3002<\/li>\n<li><strong>\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757<\/strong>\uff1a\u6700\u540e\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757\u6216\u5305\u4e2d\u7684\u6a21\u5757\u3002<\/li>\n<\/ol>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5bfc\u5165\u6807\u51c6\u5e93\u6a21\u5757<\/p>\n<p>import os<\/p>\n<p>import sys<\/p>\n<h2><strong>\u5bfc\u5165\u7b2c\u4e09\u65b9\u5e93\u6a21\u5757<\/strong><\/h2>\n<p>import numpy as np<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757<\/strong><\/h2>\n<p>import my_module<\/p>\n<p>from my_package import module1, module2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u6211\u4eec\u53ef\u4ee5\u4fdd\u6301\u5bfc\u5165\u8bed\u53e5\u7684\u6e05\u6670\u548c\u6709\u5e8f\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u6613\u8bfb\u548c\u53ef\u7ef4\u62a4\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1\u5728Python\u4e2d\u5bfc\u5165\u5e93\u51fd\u6570\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165\u7279\u5b9a\u7684\u5e93\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>from<\/code>\u5173\u952e\u5b57\u5bfc\u5165\u7279\u5b9a\u7684\u5e93\u51fd\u6570\u3002\u8bed\u6cd5\u4e3a<code>from module_name import function_name<\/code>\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u5bfc\u5165<code>math<\/code>\u5e93\u4e2d\u7684<code>sqrt<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5199\u6210<code>from math import sqrt<\/code>\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>sqrt<\/code>\u51fd\u6570\uff0c\u800c\u65e0\u9700\u5728\u8c03\u7528\u65f6\u52a0\u4e0a\u6a21\u5757\u540d\u3002<\/p>\n<p><strong>\u5bfc\u5165\u5e93\u65f6\u662f\u5426\u6709\u5fc5\u8981\u5b89\u88c5\u5e93\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u82e5\u8981\u5bfc\u5165\u7684\u5e93\u4e0d\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u5219\u9700\u8981\u5148\u5b89\u88c5\u8be5\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177\u5982<code>pip<\/code>\u6765\u5b89\u88c5\u3002\u4f8b\u5982\uff0c\u82e5\u8981\u5b89\u88c5<code>numpy<\/code>\u5e93\uff0c\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>pip install numpy<\/code>\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5c31\u53ef\u4ee5\u5728Python\u4ee3\u7801\u4e2d\u4f7f\u7528<code>import numpy<\/code>\u6765\u5bfc\u5165\u8be5\u5e93\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5\u5df2\u5b89\u88c5\u7684\u5e93\u548c\u5176\u7248\u672c\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>pip list<\/code>\u6765\u67e5\u770b\u5f53\u524d\u73af\u5883\u4e2d\u5df2\u5b89\u88c5\u7684\u6240\u6709\u5e93\u53ca\u5176\u7248\u672c\u4fe1\u606f\u3002\u6b64\u5916\uff0c\u5982\u679c\u60f3\u8981\u68c0\u67e5\u67d0\u4e2a\u7279\u5b9a\u5e93\u7684\u7248\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip show library_name<\/code>\u547d\u4ee4\u3002\u4f8b\u5982\uff0c\u8f93\u5165<code>pip show numpy<\/code>\u5c06\u663e\u793a<code>numpy<\/code>\u5e93\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5305\u62ec\u5176\u7248\u672c\u53f7\u548c\u5b89\u88c5\u8def\u5f84\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5bfc\u5165\u5e93\u51fd\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ecimport\u8bed\u53e5\u3001from&#8230;import\u8bed\u53e5\u3001as [&hellip;]","protected":false},"author":3,"featured_media":1028778,"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\/1028775"}],"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=1028775"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1028775\/revisions"}],"predecessor-version":[{"id":1028779,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1028775\/revisions\/1028779"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1028778"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1028775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1028775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1028775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}