{"id":922169,"date":"2024-12-26T14:27:03","date_gmt":"2024-12-26T06:27:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/922169.html"},"modified":"2024-12-26T14:27:05","modified_gmt":"2024-12-26T06:27:05","slug":"python-%e5%a6%82%e4%bd%95import","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/922169.html","title":{"rendered":"python \u5982\u4f55import"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210814\/1692e137-40d6-4835-bc38-f8a66166c168.webp\" alt=\"python \u5982\u4f55import\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0cimport\u8bed\u53e5\u7528\u4e8e\u5f15\u5165\u6a21\u5757\u3001\u5e93\u6216\u5305\uff0c\u4ee5\u4fbf\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u5b83\u4eec\u63d0\u4f9b\u7684\u529f\u80fd\u3002\u8981\u4f7f\u7528import\u8bed\u53e5\uff0c\u53ef\u4ee5\u901a\u8fc7import\u6a21\u5757\u540d\u3001from\u6a21\u5757\u540dimport\u51fd\u6570\u6216\u7c7b\u3001import\u6a21\u5757\u540das\u522b\u540d\u4e09\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002\u6700\u5e38\u7528\u7684\u662f\u76f4\u63a5\u4f7f\u7528import\u6a21\u5757\u540d\uff0c\u5b83\u53ef\u4ee5\u5f15\u5165\u6574\u4e2a\u6a21\u5757\u3002<\/strong><\/p>\n<\/p>\n<p><p>Python\u662f\u4e00\u79cd\u529f\u80fd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6a21\u5757\u5e93\u4ee5\u4f9b\u5f00\u53d1\u8005\u4f7f\u7528\u3002\u901a\u8fc7\u4f7f\u7528import\u8bed\u53e5\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u6a21\u5757\u5f15\u5165\u5230\u6211\u4eec\u7684\u7a0b\u5e8f\u4e2d\uff0c\u4ece\u800c\u5927\u5927\u6269\u5c55\u7a0b\u5e8f\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u6570\u5b66\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7import math\u6765\u5f15\u5165Python\u7684\u6570\u5b66\u6a21\u5757math\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528math\u6a21\u5757\u4e2d\u7684\u5404\u79cd\u6570\u5b66\u51fd\u6570\u4e86\u3002import\u8bed\u53e5\u4e0d\u4ec5\u53ef\u4ee5\u5f15\u5165Python\u81ea\u5e26\u7684\u6a21\u5757\uff0c\u8fd8\u53ef\u4ee5\u5f15\u5165\u7b2c\u4e09\u65b9\u5e93\u548c\u81ea\u5b9a\u4e49\u7684\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3Python\u4e2d\u7684import\u8bed\u53e5\uff0c\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u8ba8\u8bbaimport\u7684\u4e0d\u540c\u65b9\u5f0f\u4ee5\u53ca\u5176\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001IMPORT\u6a21\u5757\u540d<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6700\u5e38\u89c1\u7684import\u65b9\u5f0f\u662f\u76f4\u63a5\u4f7f\u7528import\u6a21\u5757\u540d\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5f15\u5165\u6574\u4e2a\u6a21\u5757\uff0c\u8ba9\u6211\u4eec\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u6a21\u5757\u4e2d\u7684\u6240\u6709\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5f15\u5165Python\u6807\u51c6\u5e93\u6a21\u5757<\/strong><\/li>\n<\/ol>\n<p><p>Python\u7684\u6807\u51c6\u5e93\u4e2d\u5305\u542b\u8bb8\u591a\u6709\u7528\u7684\u6a21\u5757\uff0c\u4f8b\u5982math\u3001os\u3001sys\u7b49\u3002\u901a\u8fc7import\u8bed\u53e5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5f15\u5165\u8fd9\u4e9b\u6a21\u5757\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>result = math.sqrt(16)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7import math\u5f15\u5165\u4e86math\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528math.sqrt\u51fd\u6570\u8ba1\u7b97\u5e73\u65b9\u6839\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5f15\u5165\u7b2c\u4e09\u65b9\u5e93<\/strong><\/li>\n<\/ol>\n<p><p>\u9664\u4e86\u6807\u51c6\u5e93\uff0cPython\u8fd8\u62e5\u6709\u4e30\u5bcc\u7684\u7b2c\u4e09\u65b9\u5e93\u3002\u8fd9\u4e9b\u5e93\u901a\u5e38\u9700\u8981\u901a\u8fc7\u5305\u7ba1\u7406\u5de5\u5177\uff08\u5982pip\uff09\u5b89\u88c5\uff0c\u7136\u540e\u901a\u8fc7import\u8bed\u53e5\u5f15\u5165\u3002\u4f8b\u5982\uff0cNumPy\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u6d41\u884c\u5e93\uff0c\u53ef\u4ee5\u8fd9\u6837\u5f15\u5165\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([1, 2, 3])<\/p>\n<p>print(array)  # \u8f93\u51fa\uff1a[1 2 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528import numpy as np\u5f15\u5165\u4e86NumPy\u5e93\uff0c\u5e76\u4e3a\u5176\u6307\u5b9a\u4e86\u522b\u540dnp\uff0c\u4ee5\u4fbf\u4e8e\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5f15\u5165\u81ea\u5b9a\u4e49\u6a21\u5757<\/strong><\/li>\n<\/ol>\n<p><p>\u9664\u4e86\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5f15\u5165\u81ea\u5df1\u7f16\u5199\u7684\u6a21\u5757\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3amy_module.py\u7684\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e9b\u51fd\u6570\u548c\u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5f15\u5165\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import my_module<\/p>\n<p>result = my_module.my_function()<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u901a\u8fc7import my_module\u5f15\u5165\u4e86\u81ea\u5b9a\u4e49\u6a21\u5757my_module\uff0c\u7136\u540e\u8c03\u7528\u5176\u4e2d\u7684my_function\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001FROM\u6a21\u5757\u540dIMPORT\u51fd\u6570\u6216\u7c7b<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u53ea\u9700\u8981\u6a21\u5757\u4e2d\u7684\u67d0\u4e2a\u51fd\u6570\u6216\u7c7b\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528from\u6a21\u5757\u540dimport\u51fd\u6570\u6216\u7c7b\u7684\u5f62\u5f0f\u5f15\u5165\u6240\u9700\u7684\u90e8\u5206\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5f15\u5165\u7279\u5b9a\u7684\u51fd\u6570\u6216\u7c7b<\/strong><\/li>\n<\/ol>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u8ba9\u6211\u4eec\u53ea\u5f15\u5165\u9700\u8981\u7684\u90e8\u5206\uff0c\u51cf\u5c11\u547d\u540d\u7a7a\u95f4\u7684\u6c61\u67d3\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from math import sqrt<\/p>\n<p>result = sqrt(16)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u53ea\u5f15\u5165\u4e86math\u6a21\u5757\u4e2d\u7684sqrt\u51fd\u6570\uff0c\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528sqrt\u8ba1\u7b97\u5e73\u65b9\u6839\uff0c\u800c\u65e0\u9700\u6307\u5b9a\u6a21\u5757\u540d\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5f15\u5165\u591a\u4e2a\u51fd\u6570\u6216\u7c7b<\/strong><\/li>\n<\/ol>\n<p><p>\u5982\u679c\u9700\u8981\u5f15\u5165\u591a\u4e2a\u51fd\u6570\u6216\u7c7b\uff0c\u53ef\u4ee5\u7528\u9017\u53f7\u5206\u9694\u5b83\u4eec\u7684\u540d\u79f0\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from math import sqrt, pow<\/p>\n<p>result1 = sqrt(16)<\/p>\n<p>result2 = pow(2, 3)<\/p>\n<p>print(result1, result2)  # \u8f93\u51fa\uff1a4.0 8.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5f15\u5165\u4e86math\u6a21\u5757\u4e2d\u7684sqrt\u548cpow\u51fd\u6570\uff0c\u5e76\u5206\u522b\u8ba1\u7b97\u5e73\u65b9\u6839\u548c\u5e42\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001IMPORT\u6a21\u5757\u540dAS\u522b\u540d<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6a21\u5757\u540d\u53ef\u80fd\u6bd4\u8f83\u957f\u6216\u4e0e\u4ee3\u7801\u4e2d\u7684\u5176\u4ed6\u90e8\u5206\u51b2\u7a81\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528import\u6a21\u5757\u540das\u522b\u540d\u7684\u65b9\u5f0f\u4e3a\u6a21\u5757\u6307\u5b9a\u4e00\u4e2a\u7b80\u77ed\u7684\u522b\u540d\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4e3a\u6a21\u5757\u6307\u5b9a\u522b\u540d<\/strong><\/li>\n<\/ol>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([1, 2, 3])<\/p>\n<p>print(array)  # \u8f93\u51fa\uff1a[1 2 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4e3aNumPy\u5e93\u6307\u5b9a\u4e86\u522b\u540dnp\uff0c\u8fd9\u6837\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528NumPy\u7684\u529f\u80fd\u65f6\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5f15\u7528\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u907f\u514d\u547d\u540d\u51b2\u7a81<\/strong><\/li>\n<\/ol>\n<p><p>\u5982\u679c\u6211\u4eec\u5f15\u5165\u7684\u6a21\u5757\u540d\u4e0e\u4ee3\u7801\u4e2d\u7684\u53d8\u91cf\u540d\u51b2\u7a81\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u522b\u540d\u6765\u89e3\u51b3\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import my_module as mm<\/p>\n<p>result = mm.my_function()<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4e3a\u81ea\u5b9a\u4e49\u6a21\u5757my_module\u6307\u5b9a\u4e86\u522b\u540dmm\uff0c\u4ee5\u907f\u514d\u4e0e\u5176\u4ed6\u53d8\u91cf\u540d\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001IMPORT\u8bed\u53e5\u7684\u9ad8\u7ea7\u7528\u6cd5<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u57fa\u672c\u7528\u6cd5\uff0cimport\u8bed\u53e5\u8fd8\u6709\u4e00\u4e9b\u9ad8\u7ea7\u7528\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7ec4\u7ec7\u4ee3\u7801\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5f15\u5165\u5305\u4e2d\u7684\u6a21\u5757<\/strong><\/li>\n<\/ol>\n<p><p>Python\u7684\u5305\u662f\u6a21\u5757\u7684\u96c6\u5408\uff0c\u901a\u5e38\u7528\u4e8e\u7ec4\u7ec7\u5927\u578b\u9879\u76ee\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7import\u8bed\u53e5\u5f15\u5165\u5305\u4e2d\u7684\u6a21\u5757\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from my_package import my_module<\/p>\n<p>result = my_module.my_function()<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u901a\u8fc7from my_package import my_module\u5f15\u5165\u4e86\u5305my_package\u4e2d\u7684\u6a21\u5757my_module\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528\u901a\u914d\u7b26\u5f15\u5165\u6240\u6709\u5185\u5bb9<\/strong><\/li>\n<\/ol>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u901a\u914d\u7b26*\u5f15\u5165\u6a21\u5757\u4e2d\u7684\u6240\u6709\u5185\u5bb9\u3002\u7136\u800c\uff0c\u8fd9\u79cd\u65b9\u5f0f\u53ef\u80fd\u5bfc\u81f4\u547d\u540d\u7a7a\u95f4\u6c61\u67d3\uff0c\u4e0d\u63a8\u8350\u5728\u751f\u4ea7\u4ee3\u7801\u4e2d\u4f7f\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from math import *<\/p>\n<p>result = sqrt(16)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136\u53ef\u4ee5\u5f15\u5165\u6240\u6709\u5185\u5bb9\uff0c\u4f46\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u964d\u4f4e\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u52a8\u6001\u5f15\u5165\u6a21\u5757<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u52a8\u6001\u5f15\u5165\u6a21\u5757\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570__import__\u5b9e\u73b0\u52a8\u6001\u5f15\u5165\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">module_name = &#39;math&#39;<\/p>\n<p>math_module = __import__(module_name)<\/p>\n<p>result = math_module.sqrt(16)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u51b3\u5b9a\u5f15\u5165\u54ea\u4e2a\u6a21\u5757\uff0c\u9002\u7528\u4e8e\u9700\u8981\u7075\u6d3b\u6027\u8f83\u9ad8\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001IMPORT\u8bed\u53e5\u7684\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528import\u8bed\u53e5\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u6b63\u786e\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u907f\u514d\u5faa\u73af\u5f15\u5165<\/strong><\/li>\n<\/ol>\n<p><p>\u5faa\u73af\u5f15\u5165\u6307\u7684\u662f\u4e24\u4e2a\u6216\u591a\u4e2a\u6a21\u5757\u76f8\u4e92\u5f15\u5165\uff0c\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u6b63\u5e38\u8fd0\u884c\u3002\u5e94\u5c3d\u91cf\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u7684\u53d1\u751f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module_a.py<\/p>\n<p>import module_b<\/p>\n<h2><strong>module_b.py<\/strong><\/h2>\n<p>import module_a<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cPython\u4f1a\u629b\u51faImportError\u9519\u8bef\uff0c\u63d0\u793a\u5faa\u73af\u5f15\u5165\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5408\u7406\u7ec4\u7ec7\u6a21\u5757<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u5e94\u5408\u7406\u7ec4\u7ec7\u6a21\u5757\u548c\u5305\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u53ef\u4ee5\u6309\u7167\u529f\u80fd\u6216\u903b\u8f91\u5212\u5206\u6a21\u5757\uff0c\u5e76\u4f7f\u7528\u9002\u5f53\u7684\u547d\u540d\u7a7a\u95f4\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u4f7f\u7528\u76f8\u5bf9\u5f15\u5165<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u5305\u4e2d\u5f15\u5165\u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u5f15\u5165\uff0c\u4ee5\u4fbf\u4e8e\u5728\u5305\u5185\u79fb\u52a8\u6a21\u5757\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># my_package\/module_a.py<\/p>\n<p>from . import module_b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u76f8\u5bf9\u5f15\u5165\u4f7f\u7528\u70b9\u53f7\u8868\u793a\u5f53\u524d\u76ee\u5f55\u6216\u7236\u76ee\u5f55\uff0c\u901a\u5e38\u7528\u4e8e\u5305\u5185\u90e8\u7684\u6a21\u5757\u5f15\u5165\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001IMPORT\u8bed\u53e5\u7684\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528import\u8bed\u53e5\u65f6\uff0c\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u8d28\u91cf\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6309\u9700\u5f15\u5165\u6a21\u5757<\/strong><\/li>\n<\/ol>\n<p><p>\u5c3d\u91cf\u53ea\u5f15\u5165\u9700\u8981\u7684\u6a21\u5757\u3001\u51fd\u6570\u6216\u7c7b\uff0c\u4ee5\u51cf\u5c11\u547d\u540d\u7a7a\u95f4\u7684\u6c61\u67d3\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u9075\u5faaPEP 8\u89c4\u8303<\/strong><\/li>\n<\/ol>\n<p><p>PEP 8\u662fPython\u7684\u7f16\u7801\u98ce\u683c\u6307\u5357\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u5173\u4e8eimport\u8bed\u53e5\u7684\u5efa\u8bae\u3002\u4f8b\u5982\uff0c\u5c06import\u8bed\u53e5\u653e\u5728\u6587\u4ef6\u7684\u5f00\u5934\uff0c\u6309\u7167\u6807\u51c6\u5e93\u3001\u7b2c\u4e09\u65b9\u5e93\u3001\u81ea\u5b9a\u4e49\u6a21\u5757\u7684\u987a\u5e8f\u5206\u7ec4\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u4f7f\u7528\u522b\u540d\u63d0\u9ad8\u53ef\u8bfb\u6027<\/strong><\/li>\n<\/ol>\n<p><p>\u5f53\u6a21\u5757\u540d\u8f83\u957f\u6216\u4e0e\u4ee3\u7801\u51b2\u7a81\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u522b\u540d\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7b80\u6d01\u6027\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9bimport\u8bed\u53e5\u7684\u7528\u6cd5\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u7ec4\u7ec7\u548c\u7ba1\u7406Python\u9879\u76ee\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u7ef4\u62a4\u6027\u3002Python\u7684\u6a21\u5757\u7cfb\u7edf\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5f15\u5165\u548c\u4f7f\u7528\u5404\u79cd\u529f\u80fd\uff0c\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u6784\u5efa\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165\u6a21\u5757\uff1f<\/strong><br \/>\u5728Python\u4e2d\u5bfc\u5165\u6a21\u5757\u7684\u5e38\u7528\u65b9\u6cd5\u662f\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u5bfc\u5165\u5185\u7f6e\u7684<code>math<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u4f7f\u7528<code>import math<\/code>\u3002\u8fd9\u4f7f\u4f60\u80fd\u591f\u8bbf\u95ee\u6a21\u5757\u4e2d\u7684\u5404\u79cd\u51fd\u6570\u548c\u5e38\u91cf\uff0c\u4f8b\u5982\u4f7f\u7528<code>math.sqrt(4)<\/code>\u6765\u8ba1\u7b974\u7684\u5e73\u65b9\u6839\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5bfc\u5165\u7279\u5b9a\u7684\u51fd\u6570\u6216\u7c7b\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u53ea\u9700\u8981\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528<code>from ... import ...<\/code>\u8bed\u53e5\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u53ea\u60f3\u5bfc\u5165<code>math<\/code>\u6a21\u5757\u4e2d\u7684<code>sqrt<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5199\u6210<code>from math import sqrt<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>sqrt(4)<\/code>\u800c\u65e0\u9700\u524d\u7f00<code>math.<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5bfc\u5165\u6a21\u5757\u65f6\u7684\u8def\u5f84\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757\u65f6\uff0c\u786e\u4fdd\u6a21\u5757\u6587\u4ef6\u7684\u8def\u5f84\u5728Python\u7684\u641c\u7d22\u8def\u5f84\u4e2d\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528<code>sys.path.append(&#39;\/path\/to\/your\/module&#39;)<\/code>\u6765\u6dfb\u52a0\u81ea\u5b9a\u4e49\u6a21\u5757\u6240\u5728\u7684\u76ee\u5f55\uff0c\u4ece\u800c\u89e3\u51b3\u8def\u5f84\u95ee\u9898\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u901a\u8fc7<code>import your_module<\/code>\u6210\u529f\u5bfc\u5165\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0cimport\u8bed\u53e5\u7528\u4e8e\u5f15\u5165\u6a21\u5757\u3001\u5e93\u6216\u5305\uff0c\u4ee5\u4fbf\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u5b83\u4eec\u63d0\u4f9b\u7684\u529f\u80fd\u3002\u8981\u4f7f\u7528import\u8bed\u53e5 [&hellip;]","protected":false},"author":3,"featured_media":922173,"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\/922169"}],"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=922169"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/922169\/revisions"}],"predecessor-version":[{"id":922174,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/922169\/revisions\/922174"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/922173"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=922169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=922169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=922169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}