{"id":1108511,"date":"2025-01-08T17:01:00","date_gmt":"2025-01-08T09:01:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1108511.html"},"modified":"2025-01-08T17:01:03","modified_gmt":"2025-01-08T09:01:03","slug":"python%e5%a6%82%e4%bd%95%e5%8a%a8%e6%80%81%e5%af%bc%e5%85%a5%e4%b8%80%e4%b8%aa%e5%87%bd%e6%95%b0%e6%89%a7%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1108511.html","title":{"rendered":"python\u5982\u4f55\u52a8\u6001\u5bfc\u5165\u4e00\u4e2a\u51fd\u6570\u6267\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072127\/8f52e2d5-afa0-4467-9ba2-fbc9b0a1365d.webp\" alt=\"python\u5982\u4f55\u52a8\u6001\u5bfc\u5165\u4e00\u4e2a\u51fd\u6570\u6267\u884c\" \/><\/p>\n<p><p> <strong>Python \u52a8\u6001\u5bfc\u5165\u4e00\u4e2a\u51fd\u6570\u6267\u884c\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 <code>importlib<\/code> \u6a21\u5757\u3001\u4f7f\u7528 <code>__import__<\/code> \u51fd\u6570\u3001\u4ee5\u53ca\u901a\u8fc7 <code>exec<\/code> \u51fd\u6570\u5b9e\u73b0\u3002<\/strong> \u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c<code>importlib<\/code> \u6a21\u5757\u662f\u8f83\u4e3a\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u4e3a\u7075\u6d3b\u548c\u5b89\u5168\u7684\u5bfc\u5165\u673a\u5236\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 <code>importlib<\/code> \u6a21\u5757\u52a8\u6001\u5bfc\u5165\u5e76\u6267\u884c\u4e00\u4e2a\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>importlib<\/code> \u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>importlib<\/code> \u6a21\u5757\u662f Python 3.1 \u5f15\u5165\u7684\u4e00\u4e2a\u6807\u51c6\u5e93\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u4e0e\u5185\u7f6e <code>import<\/code> \u8bed\u53e5\u76f8\u540c\u7684\u5bfc\u5165\u673a\u5236\u3002\u4f7f\u7528 <code>importlib<\/code> \u53ef\u4ee5\u52a8\u6001\u5730\u5bfc\u5165\u6a21\u5757\u548c\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u5165\u6a21\u5757\u548c\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import importlib<\/p>\n<h2><strong>\u52a8\u6001\u5bfc\u5165\u6a21\u5757<\/strong><\/h2>\n<p>module_name = &#39;my_module&#39;<\/p>\n<p>module = importlib.import_module(module_name)<\/p>\n<h2><strong>\u52a8\u6001\u5bfc\u5165\u51fd\u6570<\/strong><\/h2>\n<p>function_name = &#39;my_function&#39;<\/p>\n<p>my_function = getattr(module, function_name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6267\u884c\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u51fd\u6570\u53c2\u6570<\/p>\n<p>args = (arg1, arg2)<\/p>\n<p>kwargs = {&#39;key&#39;: value}<\/p>\n<h2><strong>\u52a8\u6001\u6267\u884c\u51fd\u6570<\/strong><\/h2>\n<p>result = my_function(*args, kwargs)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>__import__<\/code> \u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>__import__<\/code> \u51fd\u6570\u662f Python \u5185\u7f6e\u7684\u5bfc\u5165\u51fd\u6570\uff0c\u901a\u5e38\u7528\u4e8e\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u3002\u867d\u7136\u4e0d\u5982 <code>importlib<\/code> \u90a3\u4e48\u7075\u6d3b\uff0c\u4f46\u5728\u4e00\u4e9b\u7b80\u5355\u7684\u573a\u666f\u4e2d\u4e5f\u80fd\u6ee1\u8db3\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u5165\u6a21\u5757\u548c\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u52a8\u6001\u5bfc\u5165\u6a21\u5757<\/p>\n<p>module_name = &#39;my_module&#39;<\/p>\n<p>module = __import__(module_name)<\/p>\n<h2><strong>\u52a8\u6001\u5bfc\u5165\u51fd\u6570<\/strong><\/h2>\n<p>function_name = &#39;my_function&#39;<\/p>\n<p>my_function = getattr(module, function_name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6267\u884c\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u51fd\u6570\u53c2\u6570<\/p>\n<p>args = (arg1, arg2)<\/p>\n<p>kwargs = {&#39;key&#39;: value}<\/p>\n<h2><strong>\u52a8\u6001\u6267\u884c\u51fd\u6570<\/strong><\/h2>\n<p>result = my_function(*args, kwargs)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 <code>exec<\/code> \u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>exec<\/code> \u51fd\u6570\u53ef\u4ee5\u6267\u884c\u52a8\u6001\u751f\u6210\u7684 Python \u4ee3\u7801\u3002\u867d\u7136\u4e0d\u5982\u524d\u4e24\u79cd\u65b9\u6cd5\u5b89\u5168\u548c\u7075\u6d3b\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u5b9a\u573a\u666f\u4e0b\u4e5f\u80fd\u6d3e\u4e0a\u7528\u573a\u3002<\/p>\n<\/p>\n<p><h4>1. \u52a8\u6001\u5bfc\u5165\u548c\u6267\u884c\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">module_name = &#39;my_module&#39;<\/p>\n<p>function_name = &#39;my_function&#39;<\/p>\n<p>exec(f&#39;from {module_name} import {function_name} as my_function&#39;)<\/p>\n<h2><strong>\u51fd\u6570\u53c2\u6570<\/strong><\/h2>\n<p>args = (arg1, arg2)<\/p>\n<p>kwargs = {&#39;key&#39;: value}<\/p>\n<h2><strong>\u52a8\u6001\u6267\u884c\u51fd\u6570<\/strong><\/h2>\n<p>result = my_function(*args, kwargs)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0 <code>importlib<\/code> \u6a21\u5757\u7684\u4f18\u70b9<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>importlib<\/code> \u6a21\u5757\u7684\u4e3b\u8981\u4f18\u70b9\u5728\u4e8e\u5176\u7075\u6d3b\u6027\u548c\u5b89\u5168\u6027\u3002\u9996\u5148\uff0c<code>importlib<\/code> \u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u51fd\u6570\u548c\u7c7b\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u64cd\u63a7\u6a21\u5757\u7684\u5bfc\u5165\u548c\u91cd\u65b0\u52a0\u8f7d\u3002\u5176\u6b21\uff0c<code>importlib<\/code> \u7684\u5bfc\u5165\u673a\u5236\u4e0e\u5185\u7f6e <code>import<\/code> \u8bed\u53e5\u76f8\u540c\uff0c\u4fdd\u8bc1\u4e86\u5bfc\u5165\u6a21\u5757\u7684\u5b89\u5168\u6027\u3002\u6b64\u5916\uff0c<code>importlib<\/code> \u7684\u4f7f\u7528\u65b9\u6cd5\u8f83\u4e3a\u76f4\u89c2\uff0c\u4ee3\u7801\u53ef\u8bfb\u6027\u8f83\u9ad8\uff0c\u4fbf\u4e8e\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u52a8\u6001\u5bfc\u5165\u548c\u6267\u884c\u51fd\u6570\u5728\u4ee5\u4e0b\u573a\u666f\u4e2d\u5c24\u4e3a\u5e38\u89c1\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u63d2\u4ef6\u7cfb\u7edf<\/strong>\uff1a\u5728\u63d2\u4ef6\u7cfb\u7edf\u4e2d\uff0c\u4e0d\u540c\u7684\u63d2\u4ef6\u53ef\u80fd\u5305\u542b\u4e0d\u540c\u7684\u529f\u80fd\u6a21\u5757\uff0c\u901a\u8fc7\u52a8\u6001\u5bfc\u5165\uff0c\u53ef\u4ee5\u5b9e\u73b0\u63d2\u4ef6\u7684\u70ed\u63d2\u62d4\u3002<\/li>\n<li><strong>\u914d\u7f6e\u9a71\u52a8\u7684\u5e94\u7528<\/strong>\uff1a\u5728\u4e00\u4e9b\u914d\u7f6e\u9a71\u52a8\u7684\u5e94\u7528\u4e2d\uff0c\u914d\u7f6e\u6587\u4ef6\u4e2d\u53ef\u80fd\u6307\u5b9a\u4e86\u9700\u8981\u5bfc\u5165\u548c\u6267\u884c\u7684\u6a21\u5757\u548c\u51fd\u6570\uff0c\u901a\u8fc7\u52a8\u6001\u5bfc\u5165\uff0c\u53ef\u4ee5\u6839\u636e\u914d\u7f6e\u7075\u6d3b\u5730\u52a0\u8f7d\u548c\u6267\u884c\u76f8\u5e94\u7684\u529f\u80fd\u3002<\/li>\n<li><strong>\u52a8\u6001\u811a\u672c\u6267\u884c<\/strong>\uff1a\u5728\u4e00\u4e9b\u9700\u8981\u52a8\u6001\u6267\u884c\u811a\u672c\u7684\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u52a8\u6001\u5bfc\u5165\u548c\u6267\u884c\u51fd\u6570\uff0c\u7075\u6d3b\u5730\u52a0\u8f7d\u548c\u8fd0\u884c\u4e0d\u540c\u7684\u811a\u672c\u3002<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u5b9e\u73b0\u52a8\u6001\u5bfc\u5165\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5b89\u5168\u6027<\/strong>\uff1a\u52a8\u6001\u5bfc\u5165\u548c\u6267\u884c\u4ee3\u7801\u5b58\u5728\u4e00\u5b9a\u7684\u5b89\u5168\u98ce\u9669\uff0c\u5c24\u5176\u662f\u5f53\u5bfc\u5165\u7684\u6a21\u5757\u548c\u51fd\u6570\u6765\u6e90\u4e0d\u660e\u65f6\uff0c\u53ef\u80fd\u4f1a\u5e26\u6765\u6076\u610f\u4ee3\u7801\u6ce8\u5165\u7684\u98ce\u9669\u3002\u56e0\u6b64\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e94\u5c3d\u91cf\u907f\u514d\u4ece\u4e0d\u53ef\u4fe1\u6765\u6e90\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u548c\u51fd\u6570\u3002<\/li>\n<li><strong>\u9519\u8bef\u5904\u7406<\/strong>\uff1a\u52a8\u6001\u5bfc\u5165\u548c\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6a21\u5757\u4e0d\u5b58\u5728\u3001\u51fd\u6570\u4e0d\u5b58\u5728\u3001\u53c2\u6570\u9519\u8bef\u7b49\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u56e0\u6b64\u9700\u8981\u505a\u597d\u9519\u8bef\u5904\u7406\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u3002<\/li>\n<li><strong>\u6027\u80fd\u5f71\u54cd<\/strong>\uff1a\u52a8\u6001\u5bfc\u5165\u548c\u6267\u884c\u51fd\u6570\u53ef\u80fd\u4f1a\u5e26\u6765\u4e00\u5b9a\u7684\u6027\u80fd\u5f00\u9500\uff0c\u5c24\u5176\u662f\u5728\u9891\u7e41\u5bfc\u5165\u548c\u6267\u884c\u7684\u573a\u666f\u4e2d\uff0c\u56e0\u6b64\u9700\u8981\u5408\u7406\u8bc4\u4f30\u548c\u4f18\u5316\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython \u63d0\u4f9b\u4e86\u591a\u79cd\u52a8\u6001\u5bfc\u5165\u548c\u6267\u884c\u51fd\u6570\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528 <code>importlib<\/code> \u6a21\u5757\u3001\u4f7f\u7528 <code>__import__<\/code> \u51fd\u6570\u4ee5\u53ca\u901a\u8fc7 <code>exec<\/code> \u51fd\u6570\u5b9e\u73b0\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u63a8\u8350\u4f7f\u7528 <code>importlib<\/code> \u6a21\u5757\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u4e3a\u7075\u6d3b\u548c\u5b89\u5168\u7684\u5bfc\u5165\u673a\u5236\u3002\u52a8\u6001\u5bfc\u5165\u548c\u6267\u884c\u51fd\u6570\u5728\u63d2\u4ef6\u7cfb\u7edf\u3001\u914d\u7f6e\u9a71\u52a8\u7684\u5e94\u7528\u4ee5\u53ca\u52a8\u6001\u811a\u672c\u6267\u884c\u7b49\u573a\u666f\u4e2d\u5c24\u4e3a\u5e38\u89c1\u3002\u5728\u5b9e\u73b0\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u5b89\u5168\u6027\u3001\u9519\u8bef\u5904\u7406\u548c\u6027\u80fd\u5f71\u54cd\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u9ad8\u6548\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u5e76\u8c03\u7528\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>importlib<\/code>\u6a21\u5757\u6765\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u4f7f\u7528<code>importlib.import_module()<\/code>\u51fd\u6570\u5bfc\u5165\u6a21\u5757\uff0c\u7136\u540e\u901a\u8fc7\u6a21\u5757\u7684\u5c5e\u6027\u8bbf\u95ee\u6240\u9700\u7684\u51fd\u6570\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import importlib\n\nmodule_name = &#39;your_module_name&#39;\nfunction_name = &#39;your_function_name&#39;\n\nmodule = importlib.import_module(module_name)\nfunction = getattr(module, function_name)\nresult = function(*args, **kwargs)  # \u4f20\u9012\u5fc5\u8981\u7684\u53c2\u6570\n<\/code><\/pre>\n<p>\u786e\u4fdd\u66ff\u6362<code>your_module_name<\/code>\u548c<code>your_function_name<\/code>\u4e3a\u5b9e\u9645\u7684\u6a21\u5757\u548c\u51fd\u6570\u540d\u79f0\u3002<\/p>\n<p><strong>\u52a8\u6001\u5bfc\u5165\u51fd\u6570\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u52a8\u6001\u5bfc\u5165\u51fd\u6570\u65f6\uff0c\u786e\u4fdd\u8981\u5bfc\u5165\u7684\u6a21\u5757\u5df2\u7ecf\u5728Python\u8def\u5f84\u4e2d\u53ef\u8bbf\u95ee\u3002\u6b64\u5916\uff0c\u9700\u8981\u786e\u8ba4\u51fd\u6570\u7684\u540d\u79f0\u662f\u5426\u6b63\u786e\uff0c\u4ee5\u907f\u514d\u8fd0\u884c\u65f6\u9519\u8bef\u3002\u4f7f\u7528<code>getattr()<\/code>\u65f6\uff0c\u5982\u679c\u51fd\u6570\u4e0d\u5b58\u5728\uff0c\u5c06\u5f15\u53d1<code>AttributeError<\/code>\uff0c\u56e0\u6b64\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>hasattr()<\/code>\u5148\u68c0\u67e5\u51fd\u6570\u662f\u5426\u5b58\u5728\u3002<\/p>\n<p><strong>\u5728\u4f55\u79cd\u60c5\u51b5\u4e0b\u9700\u8981\u52a8\u6001\u5bfc\u5165\u51fd\u6570\uff1f<\/strong><br \/>\u52a8\u6001\u5bfc\u5165\u51fd\u6570\u7684\u60c5\u51b5\u5305\u62ec\uff1a\u9700\u8981\u6839\u636e\u7528\u6237\u8f93\u5165\u6216\u914d\u7f6e\u6587\u4ef6\u52a8\u6001\u9009\u62e9\u6267\u884c\u7684\u529f\u80fd\uff0c\u6216\u5728\u8fd0\u884c\u65f6\u51b3\u5b9a\u52a0\u8f7d\u7684\u6a21\u5757\u3002\u8fd9\u79cd\u6280\u672f\u5728\u63d2\u4ef6\u7cfb\u7edf\u3001\u811a\u672c\u5f15\u64ce\u548c\u5927\u578b\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5c24\u4e3a\u6709\u7528\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u52a8\u6001\u5bfc\u5165\u4e00\u4e2a\u51fd\u6570\u6267\u884c\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 importlib \u6a21\u5757\u3001\u4f7f\u7528 __import__ \u51fd\u6570 [&hellip;]","protected":false},"author":3,"featured_media":1108520,"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\/1108511"}],"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=1108511"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108511\/revisions"}],"predecessor-version":[{"id":1108524,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108511\/revisions\/1108524"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1108520"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1108511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1108511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1108511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}