{"id":1051196,"date":"2024-12-31T14:14:15","date_gmt":"2024-12-31T06:14:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1051196.html"},"modified":"2024-12-31T14:14:17","modified_gmt":"2024-12-31T06:14:17","slug":"%e4%bb%a3%e7%a0%81%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5%e5%88%b0python%e4%b8%ad%e5%8e%bb%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1051196.html","title":{"rendered":"\u4ee3\u7801\u5982\u4f55\u5bfc\u5165\u5230python\u4e2d\u53bb\u5b9e\u73b0"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/3d379589-4d5e-45e2-ab10-5028f8bc914d.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u4ee3\u7801\u5982\u4f55\u5bfc\u5165\u5230python\u4e2d\u53bb\u5b9e\u73b0\" \/><\/p>\n<p><p> <strong>\u8981\u5c06\u4ee3\u7801\u5bfc\u5165\u5230Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528import\u8bed\u53e5\u3001\u6267\u884c.py\u6587\u4ef6\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570exec\u548ceval<\/strong>\uff0c\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528import\u8bed\u53e5\u6765\u5bfc\u5165\u6a21\u5757\u6216\u5305\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u5bfc\u5165\u4ee3\u7801\uff0c\u5e76\u89e3\u91ca\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e00\u3001\u4f7f\u7528import\u8bed\u53e5\u5bfc\u5165\u6a21\u5757<\/strong><\/h2>\n<p><p>import\u8bed\u53e5\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u5bfc\u5165\u4ee3\u7801\u7684\u65b9\u6cd5\u3002\u5b83\u5141\u8bb8\u4f60\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\u6216\u5305\uff0c\u5e76\u5728\u4f60\u7684\u811a\u672c\u4e2d\u4f7f\u7528\u5176\u4e2d\u7684\u51fd\u6570\u3001\u7c7b\u548c\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h2>1.1 \u5bfc\u5165\u6574\u4e2a\u6a21\u5757<\/h2>\n<\/p>\n<p><p>\u5f53\u4f60\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\u65f6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u6a21\u5757\u540d\u6765\u8bbf\u95ee\u5176\u4e2d\u7684\u51fd\u6570\u3001\u7c7b\u548c\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>print(math.sqrt(16))  # \u8f93\u51fa: 4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5bfc\u5165\u4e86math\u6a21\u5757\uff0c\u5e76\u4f7f\u7528math.sqrt()\u51fd\u6570\u6765\u8ba1\u7b97\u5e73\u65b9\u6839\u3002<\/p>\n<\/p>\n<p><h2>1.2 \u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u5185\u5bb9<\/h2>\n<\/p>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u53ea\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u51fd\u6570\u3001\u7c7b\u6216\u53d8\u91cf\uff0c\u4ece\u800c\u907f\u514d\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from math import sqrt<\/p>\n<p>print(sqrt(16))  # \u8f93\u51fa: 4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ea\u5bfc\u5165\u4e86sqrt\u51fd\u6570\uff0c\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5b83\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528\u6a21\u5757\u540d\u3002<\/p>\n<\/p>\n<p><h2>1.3 \u4f7f\u7528\u522b\u540d<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4e3a\u5bfc\u5165\u7684\u6a21\u5757\u6216\u51fd\u6570\u6307\u5b9a\u522b\u540d\uff0c\u4ee5\u4fbf\u7b80\u5316\u4ee3\u7801\u6216\u907f\u514d\u547d\u540d\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math as m<\/p>\n<p>print(m.sqrt(16))  # \u8f93\u51fa: 4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4e3amath\u6a21\u5757\u6307\u5b9a\u4e86\u522b\u540dm\uff0c\u56e0\u6b64\u53ef\u4ee5\u4f7f\u7528m.sqrt()\u6765\u4ee3\u66ffmath.sqrt()\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e8c\u3001\u6267\u884c.py\u6587\u4ef6<\/strong><\/h2>\n<p><p>\u9664\u4e86\u4f7f\u7528import\u8bed\u53e5\uff0c\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u6267\u884c.py\u6587\u4ef6\u6765\u5bfc\u5165\u4ee3\u7801\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5e0c\u671b\u8fd0\u884c\u6574\u4e2a\u811a\u672c\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h2>2.1 \u4f7f\u7528exec()\u51fd\u6570<\/h2>\n<\/p>\n<p><p>exec()\u51fd\u6570\u53ef\u4ee5\u6267\u884c\u5b58\u50a8\u5728\u5b57\u7b26\u4e32\u6216\u6587\u4ef6\u4e2d\u7684Python\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;script.py&#39;, &#39;r&#39;) as file:<\/p>\n<p>    exec(file.read())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6253\u5f00\u4e00\u4e2a\u540d\u4e3ascript.py\u7684\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528exec()\u51fd\u6570\u6267\u884c\u5176\u4e2d\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h2>2.2 \u4f7f\u7528subprocess\u6a21\u5757<\/h2>\n<\/p>\n<p><p>subprocess\u6a21\u5757\u5141\u8bb8\u4f60\u5728\u5b50\u8fdb\u7a0b\u4e2d\u6267\u884c\u5916\u90e8\u547d\u4ee4\uff0c\u5305\u62ecPython\u811a\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>subprocess.run([&#39;python&#39;, &#39;script.py&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528subprocess.run()\u51fd\u6570\u6267\u884c\u4e86script.py\u811a\u672c\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e09\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570exec\u548ceval<\/strong><\/h2>\n<p><p>\u9664\u4e86\u4e0a\u9762\u7684\u65b9\u6cd5\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528exec\u548ceval\u51fd\u6570\u6765\u52a8\u6001\u6267\u884c\u4ee3\u7801\u3002\u8fd9\u4e9b\u51fd\u6570\u9002\u7528\u4e8e\u9700\u8981\u5728\u8fd0\u884c\u65f6\u751f\u6210\u548c\u6267\u884c\u4ee3\u7801\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h2>3.1 \u4f7f\u7528exec()\u51fd\u6570<\/h2>\n<\/p>\n<p><p>exec()\u51fd\u6570\u53ef\u4ee5\u6267\u884c\u5b58\u50a8\u5728\u5b57\u7b26\u4e32\u4e2d\u7684Python\u4ee3\u7801\uff0c\u5e76\u652f\u6301\u5b9a\u4e49\u5168\u5c40\u548c\u5c40\u90e8\u547d\u540d\u7a7a\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">code = &quot;&quot;&quot;<\/p>\n<p>def greet(name):<\/p>\n<p>    return f&#39;Hello, {name}!&#39;<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>exec(code)<\/p>\n<p>print(greet(&#39;World&#39;))  # \u8f93\u51fa: Hello, World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u4ee3\u7801\u5b58\u50a8\u5728\u5b57\u7b26\u4e32\u4e2d\uff0c\u5e76\u4f7f\u7528exec()\u51fd\u6570\u6267\u884c\u5b83\u3002<\/p>\n<\/p>\n<p><h2>3.2 \u4f7f\u7528eval()\u51fd\u6570<\/h2>\n<\/p>\n<p><p>eval()\u51fd\u6570\u53ef\u4ee5\u6267\u884c\u5b58\u50a8\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u8868\u8fbe\u5f0f\uff0c\u5e76\u8fd4\u56de\u7ed3\u679c\u3002\u5b83\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u8ba1\u7b97\u8868\u8fbe\u5f0f\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">expression = &quot;3 + 5&quot;<\/p>\n<p>result = eval(expression)<\/p>\n<p>print(result)  # \u8f93\u51fa: 8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u8868\u8fbe\u5f0f\u5b58\u50a8\u5728\u5b57\u7b26\u4e32\u4e2d\uff0c\u5e76\u4f7f\u7528eval()\u51fd\u6570\u8ba1\u7b97\u7ed3\u679c\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u56db\u3001\u4f7f\u7528importlib\u6a21\u5757<\/strong><\/h2>\n<p><p>importlib\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u5bfc\u5165\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h2>4.1 \u52a8\u6001\u5bfc\u5165\u6a21\u5757<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528importlib.import_module()\u51fd\u6570\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import importlib<\/p>\n<p>module_name = &#39;math&#39;<\/p>\n<p>math = importlib.import_module(module_name)<\/p>\n<p>print(math.sqrt(16))  # \u8f93\u51fa: 4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528importlib.import_module()\u51fd\u6570\u52a8\u6001\u5bfc\u5165\u4e86math\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h2>4.2 \u91cd\u65b0\u52a0\u8f7d\u6a21\u5757<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528importlib.reload()\u51fd\u6570\u91cd\u65b0\u52a0\u8f7d\u5df2\u7ecf\u5bfc\u5165\u7684\u6a21\u5757\u3002\u8fd9\u5728\u4f60\u4fee\u6539\u4e86\u6a21\u5757\u4ee3\u7801\u540e\uff0c\u60f3\u8981\u91cd\u65b0\u5bfc\u5165\u6a21\u5757\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import importlib<\/p>\n<p>import math<\/p>\n<h2><strong>\u4fee\u6539math\u6a21\u5757\u4ee3\u7801\u540e<\/strong><\/h2>\n<p>importlib.reload(math)<\/p>\n<p>print(math.sqrt(16))  # \u8f93\u51fa: 4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528importlib.reload()\u51fd\u6570\u91cd\u65b0\u52a0\u8f7d\u4e86math\u6a21\u5757\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e94\u3001\u5bfc\u5165\u5305<\/strong><\/h2>\n<p><p>\u5305\u662f\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u76ee\u5f55\uff0c\u901a\u5e38\u5305\u542b\u4e00\u4e2a__init__.py\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528import\u8bed\u53e5\u5bfc\u5165\u5305\u4e2d\u7684\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h2>5.1 \u5bfc\u5165\u5305\u4e2d\u7684\u6a21\u5757<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">import mypackage.mymodule<\/p>\n<p>print(mypackage.mymodule.my_function())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5bfc\u5165\u4e86mypackage\u5305\u4e2d\u7684mymodule\u6a21\u5757\uff0c\u5e76\u8c03\u7528\u4e86\u5176\u4e2d\u7684my_function\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h2>5.2 \u5bfc\u5165\u5305\u4e2d\u7684\u7279\u5b9a\u5185\u5bb9<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">from mypackage.mymodule import my_function<\/p>\n<p>print(my_function())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ea\u5bfc\u5165\u4e86mypackage\u5305\u4e2d\u7684mymodule\u6a21\u5757\u4e2d\u7684my_function\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h2>5.3 \u4f7f\u7528__init__.py\u6587\u4ef6<\/h2>\n<\/p>\n<p><p><strong>init<\/strong>.py\u6587\u4ef6\u53ef\u4ee5\u5305\u542b\u5305\u7684\u521d\u59cb\u5316\u4ee3\u7801\uff0c\u5e76\u63a7\u5236\u4ece\u5305\u4e2d\u5bfc\u5165\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># __init__.py<\/p>\n<p>from .mymodule import my_function<\/p>\n<h2><strong>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/strong><\/h2>\n<p>import mypackage<\/p>\n<p>print(mypackage.my_function())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728__init__.py\u6587\u4ef6\u4e2d\u5bfc\u5165\u4e86mymodule\u6a21\u5757\u4e2d\u7684my_function\u51fd\u6570\uff0c\u5e76\u5728main.py\u4e2d\u4f7f\u7528\u5b83\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u516d\u3001\u4f7f\u7528sys\u6a21\u5757<\/strong><\/h2>\n<p><p>sys\u6a21\u5757\u63d0\u4f9b\u4e86\u4e0ePython\u89e3\u91ca\u5668\u4ea4\u4e92\u7684\u529f\u80fd\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u52a8\u6001\u4fee\u6539\u6a21\u5757\u641c\u7d22\u8def\u5f84\u548c\u5bfc\u5165\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h2>6.1 \u4fee\u6539\u6a21\u5757\u641c\u7d22\u8def\u5f84<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528sys.path\u5217\u8868\u6765\u52a8\u6001\u6dfb\u52a0\u6a21\u5757\u641c\u7d22\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>sys.path.append(&#39;\/path\/to\/mypackage&#39;)<\/p>\n<p>import mymodule<\/p>\n<p>print(mymodule.my_function())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06mypackage\u76ee\u5f55\u6dfb\u52a0\u5230sys.path\u5217\u8868\u4e2d\uff0c\u5e76\u5bfc\u5165\u4e86\u5176\u4e2d\u7684mymodule\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h2>6.2 \u52a8\u6001\u5bfc\u5165\u6a21\u5757<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528__import__()\u51fd\u6570\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">module_name = &#39;math&#39;<\/p>\n<p>math = __import__(module_name)<\/p>\n<p>print(math.sqrt(16))  # \u8f93\u51fa: 4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528__import__()\u51fd\u6570\u52a8\u6001\u5bfc\u5165\u4e86math\u6a21\u5757\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e03\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u5bfc\u5165\u4ee3\u7801\u5230Python\u4e2d\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528import\u8bed\u53e5\u3001\u6267\u884c.py\u6587\u4ef6\u3001\u4f7f\u7528exec\u548ceval\u51fd\u6570\u3001\u4f7f\u7528importlib\u6a21\u5757\u3001\u5bfc\u5165\u5305\u3001\u4f7f\u7528sys\u6a21\u5757\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u5177\u4f53\u9009\u62e9\u53d6\u51b3\u4e8e\u4f60\u7684\u9700\u6c42\u548c\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5b66\u4e60\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u5bfc\u5165\u548c\u4f7f\u7528Python\u4ee3\u7801\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u91cd\u7528\u6027\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165\u73b0\u6709\u7684\u4ee3\u7801\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5c06\u73b0\u6709\u7684\u4ee3\u7801\u6587\u4ef6\u5bfc\u5165\u5230\u5f53\u524d\u811a\u672c\u4e2d\u3002\u5982\u679c\u4f60\u7684\u4ee3\u7801\u6587\u4ef6\u540d\u4e3a<code>my_module.py<\/code>\uff0c\u4f60\u53ea\u9700\u5728\u4f60\u7684Python\u811a\u672c\u4e2d\u6dfb\u52a0<code>import my_module<\/code>\u3002\u786e\u4fdd\u8be5\u6587\u4ef6\u4e0e\u5f53\u524d\u811a\u672c\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\uff0c\u6216\u8005\u5c06\u5176\u8def\u5f84\u6dfb\u52a0\u5230Python\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84\u4e2d\u3002<\/p>\n<p><strong>Python\u652f\u6301\u54ea\u4e9b\u7c7b\u578b\u7684\u4ee3\u7801\u5bfc\u5165\uff1f<\/strong><br \/>Python\u652f\u6301\u591a\u79cd\u7c7b\u578b\u7684\u4ee3\u7801\u5bfc\u5165\uff0c\u5305\u62ec\u6a21\u5757\u3001\u5305\u548c\u7c7b\u3002\u6a21\u5757\u662f\u4e00\u4e2a\u5305\u542bPython\u4ee3\u7801\u7684\u6587\u4ef6\uff0c\u5305\u662f\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u6587\u4ef6\u5939\u3002\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7<code>from module import function<\/code>\u7684\u65b9\u5f0f\u5bfc\u5165\u7279\u5b9a\u7684\u51fd\u6570\u6216\u7c7b\uff0c\u8fd9\u6837\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8fd9\u4e9b\u529f\u80fd\u800c\u4e0d\u5fc5\u52a0\u4e0a\u6a21\u5757\u540d\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5bfc\u5165\u4ee3\u7801\u65f6\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u5bfc\u5165\u4ee3\u7801\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec<code>ModuleNotFoundError<\/code>\u548c<code>ImportError<\/code>\u3002<code>ModuleNotFoundError<\/code>\u901a\u5e38\u662f\u7531\u4e8e\u6a21\u5757\u4e0d\u5b58\u5728\u6216\u8def\u5f84\u9519\u8bef\u5f15\u8d77\u7684\uff0c\u68c0\u67e5\u6587\u4ef6\u540d\u548c\u8def\u5f84\u662f\u5426\u6b63\u786e\u662f\u89e3\u51b3\u6b64\u95ee\u9898\u7684\u5173\u952e\u3002<code>ImportError<\/code>\u5219\u53ef\u80fd\u662f\u7531\u4e8e\u6a21\u5757\u5185\u90e8\u7ed3\u6784\u6539\u53d8\uff0c\u786e\u4fdd\u5bfc\u5165\u7684\u6a21\u5757\u548c\u51fd\u6570\u540d\u79f0\u6b63\u786e\uff0c\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u6a21\u5757\u7684\u6587\u6863\u6765\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06\u4ee3\u7801\u5bfc\u5165\u5230Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528import\u8bed\u53e5\u3001\u6267\u884c.py\u6587\u4ef6\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570exec\u548ceval\uff0c\u6700\u5e38\u89c1 [&hellip;]","protected":false},"author":3,"featured_media":1051202,"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\/1051196"}],"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=1051196"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1051196\/revisions"}],"predecessor-version":[{"id":1051209,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1051196\/revisions\/1051209"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1051202"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1051196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1051196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1051196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}