{"id":1181986,"date":"2025-01-15T18:58:08","date_gmt":"2025-01-15T10:58:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1181986.html"},"modified":"2025-01-15T18:58:11","modified_gmt":"2025-01-15T10:58:11","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8%e5%85%b6%e4%bb%96py%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1181986.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528\u5176\u4ed6py\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25130141\/483bb6e8-e009-43f1-84c0-d767fee096c8.webp\" alt=\"python\u5982\u4f55\u8c03\u7528\u5176\u4ed6py\u7a0b\u5e8f\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u8c03\u7528\u5176\u4ed6.py\u7a0b\u5e8f\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528<code>os.system()<\/code>\u3001<code>subprocess<\/code>\u6a21\u5757\u3001<code>import<\/code>\u8bed\u53e5\u7b49\u3002<strong>\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>os.system()<\/code>\u3001\u4f7f\u7528<code>import<\/code>\u8bed\u53e5<\/strong>\u662f\u5e38\u89c1\u7684\u65b9\u5f0f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757<\/strong>\u662f\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u548c\u66f4\u597d\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>subprocess.run()<\/code>\u6765\u6267\u884c\u5176\u4ed6.py\u7a0b\u5e8f\u5e76\u6355\u83b7\u5176\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>result = subprocess.run([&#39;python&#39;, &#39;other_script.py&#39;], capture_output=True, text=True)<\/p>\n<p>print(result.stdout)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>subprocess<\/code>\u6a21\u5757\u662fPython 3\u5f15\u5165\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u7528\u4e8e\u751f\u6210\u65b0\u7684\u8fdb\u7a0b\u3001\u8fde\u63a5\u5b83\u4eec\u7684\u8f93\u5165\/\u8f93\u51fa\/\u9519\u8bef\u7ba1\u9053\uff0c\u5e76\u83b7\u5f97\u8fd4\u56de\u7801\u3002\u5b83\u63d0\u4f9b\u4e86\u6bd4<code>os.system()<\/code>\u66f4\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5e76\u4e14\u63a8\u8350\u5728\u65b0\u7684\u4ee3\u7801\u4e2d\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u6765\u66ff\u4ee3<code>os.system()<\/code>\u3002<\/p>\n<\/p>\n<p><h3>1.1 <code>subprocess.run()<\/code><\/h3>\n<\/p>\n<p><p><code>subprocess.run()<\/code>\u662f\u4e00\u4e2a\u7b80\u5355\u800c\u5f3a\u5927\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u8fd0\u884c\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u5e76\u7b49\u5f85\u5b83\u5b8c\u6210\u3002\u5b83\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a<code>CompletedProcess<\/code>\u5b9e\u4f8b\uff0c\u5305\u542b\u4e86\u6267\u884c\u7ed3\u679c\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>result = subprocess.run([&#39;python&#39;, &#39;other_script.py&#39;], capture_output=True, text=True)<\/p>\n<p>print(result.stdout)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>capture_output=True<\/code>\u8868\u793a\u6355\u83b7\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\uff0c<code>text=True<\/code>\u8868\u793a\u4ee5\u6587\u672c\u6a21\u5f0f\u5904\u7406\u8f93\u51fa\u3002\u5982\u679c\u53ea\u5173\u5fc3\u8fd4\u56de\u7801\uff0c\u53ef\u4ee5\u7701\u7565\u8fd9\u4e9b\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h3>1.2 <code>subprocess.Popen()<\/code><\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u66f4\u590d\u6742\u7684\u4ea4\u4e92\uff0c\u53ef\u4ee5\u4f7f\u7528<code>subprocess.Popen()<\/code>\u3002\u5b83\u5141\u8bb8\u4f60\u66f4\u7075\u6d3b\u5730\u63a7\u5236\u5b50\u8fdb\u7a0b\u7684\u8f93\u5165\/\u8f93\u51fa\/\u9519\u8bef\u7ba1\u9053\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>process = subprocess.Popen([&#39;python&#39;, &#39;other_script.py&#39;], stdout=subprocess.PIPE, stderr=subprocess.PIPE)<\/p>\n<p>stdout, stderr = process.communicate()<\/p>\n<p>print(stdout.decode())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Popen<\/code>\u5bf9\u8c61\u7684<code>communicate()<\/code>\u65b9\u6cd5\u8bfb\u53d6\u5b50\u8fdb\u7a0b\u7684\u8f93\u51fa\u548c\u9519\u8bef\u6d41\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u7684\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>os.system()<\/code><\/p>\n<\/p>\n<p><p><code>os.system()<\/code>\u662f\u4e00\u4e2a\u66f4\u7b80\u5355\u4f46\u529f\u80fd\u8f83\u5c11\u7684\u65b9\u6cd5\u3002\u5b83\u5728\u5b50\u8fdb\u7a0b\u4e2d\u6267\u884c\u547d\u4ee4\uff0c\u5e76\u5c06\u6267\u884c\u7ed3\u679c\u8fd4\u56de\u7ed9\u7236\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>os.system(&#39;python other_script.py&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136<code>os.system()<\/code>\u5f88\u7b80\u5355\uff0c\u4f46\u5b83\u4e0d\u63a8\u8350\u7528\u4e8e\u65b0\u7684\u4ee3\u7801\uff0c\u56e0\u4e3a\u5b83\u4e0d\u5982<code>subprocess<\/code>\u6a21\u5757\u7075\u6d3b\u548c\u5f3a\u5927\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>import<\/code>\u8bed\u53e5<\/p>\n<\/p>\n<p><p>\u5982\u679c\u53e6\u4e00\u4e2a.py\u6587\u4ef6\u5305\u542b\u7684\u51fd\u6570\u6216\u7c7b\u662f\u4f60\u5728\u5f53\u524d\u7a0b\u5e8f\u4e2d\u9700\u8981\u8c03\u7528\u7684\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u5b83\u4eec\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u6a21\u5757\u5316\u7684\u4ee3\u7801\u8bbe\u8ba1\u3002<\/p>\n<\/p>\n<p><h3>3.1 \u5bfc\u5165\u6574\u4e2a\u6a21\u5757<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import other_script<\/p>\n<p>other_script.some_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.2 \u5bfc\u5165\u7279\u5b9a\u7684\u51fd\u6570\u6216\u7c7b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from other_script import some_function<\/p>\n<p>some_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u66f4\u6e05\u6670\uff0c\u6a21\u5757\u5316\u66f4\u597d\uff0c\u4f46\u7f3a\u70b9\u662f\u4e0d\u80fd\u7528\u4e8e\u6267\u884c\u5305\u542b\u9876\u5c42\u811a\u672c\u903b\u8f91\u7684.py\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528<code>exec()<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u52a8\u6001\u5730\u6267\u884c\u4e00\u4e2a.py\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528<code>exec()<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;other_script.py&#39;) as f:<\/p>\n<p>    code = f.read()<\/p>\n<p>    exec(code)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5feb\u901f\u539f\u578b\u5f00\u53d1\u6216\u9700\u8981\u52a8\u6001\u6267\u884c\u4ee3\u7801\u7684\u573a\u666f\uff0c\u4f46\u4e0d\u63a8\u8350\u5728\u751f\u4ea7\u4ee3\u7801\u4e2d\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u5f15\u5165\u5b89\u5168\u95ee\u9898\u548c\u4ee3\u7801\u7ef4\u62a4\u56f0\u96be\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528<code>importlib<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>importlib<\/code>\u6a21\u5757\u5141\u8bb8\u4f60\u52a8\u6001\u5730\u5bfc\u5165\u6a21\u5757\uff0c\u5e76\u5728\u8fd0\u884c\u65f6\u52a0\u8f7d\u548c\u6267\u884c\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import importlib<\/p>\n<p>module_name = &#39;other_script&#39;<\/p>\n<p>module = importlib.import_module(module_name)<\/p>\n<p>module.some_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>importlib<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u4f18\u96c5\u548c\u5b89\u5168\u7684\u65b9\u5f0f\u6765\u52a8\u6001\u5bfc\u5165\u6a21\u5757\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5728\u8fd0\u884c\u65f6\u786e\u5b9a\u5bfc\u5165\u54ea\u4e2a\u6a21\u5757\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8c03\u7528\u5176\u4ed6.py\u7a0b\u5e8f\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u3002<strong>\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757<\/strong>\u662f\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u548c\u826f\u597d\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u3002<strong>\u4f7f\u7528<code>os.system()<\/code><\/strong>\u867d\u7136\u7b80\u5355\uff0c\u4f46\u529f\u80fd\u6709\u9650\uff0c\u4e0d\u63a8\u8350\u5728\u65b0\u7684\u4ee3\u7801\u4e2d\u4f7f\u7528\u3002<strong>\u4f7f\u7528<code>import<\/code>\u8bed\u53e5<\/strong>\u9002\u7528\u4e8e\u6a21\u5757\u5316\u7684\u4ee3\u7801\u8bbe\u8ba1\uff0c\u800c<strong><code>exec()<\/code>\u51fd\u6570<\/strong>\u548c<strong><code>importlib<\/code>\u6a21\u5757<\/strong>\u5219\u9002\u7528\u4e8e\u52a8\u6001\u6267\u884c\u4ee3\u7801\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u573a\u666f\uff0c\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u8fd9\u4e9b\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\u5176\u4ed6.py\u6587\u4ef6\u7684\u51fd\u6570\u548c\u7c7b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u6765\u5bfc\u5165\u5176\u4ed6.py\u6587\u4ef6\u7684\u51fd\u6570\u548c\u7c7b\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a<code>module.py<\/code>\u7684\u6587\u4ef6\uff0c\u91cc\u9762\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>my_function<\/code>\uff0c\u4f60\u53ef\u4ee5\u5728\u53e6\u4e00\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528<code>from module import my_function<\/code>\u6765\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u3002\u5982\u679c\u9700\u8981\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\uff0c\u53ef\u4ee5\u4f7f\u7528<code>import module<\/code>\uff0c\u7136\u540e\u901a\u8fc7<code>module.my_function()<\/code>\u6765\u8c03\u7528\u3002<\/p>\n<p><strong>\u8c03\u7528\u5176\u4ed6Python\u7a0b\u5e8f\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u8c03\u7528\u5176\u4ed6Python\u7a0b\u5e8f\u65f6\uff0c\u786e\u4fdd\u88ab\u8c03\u7528\u7684\u6587\u4ef6\u5728Python\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84\u4e2d\uff0c\u6216\u8005\u4e0e\u8c03\u7528\u6587\u4ef6\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\u3002\u53e6\u5916\uff0c\u6ce8\u610f\u907f\u514d\u5faa\u73af\u5bfc\u5165\u7684\u95ee\u9898\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\u4e3a\u4e86\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u5efa\u8bae\u4f7f\u7528\u5408\u9002\u7684\u547d\u540d\u548c\u7ed3\u6784\uff0c\u907f\u514d\u8fc7\u4e8e\u590d\u6742\u7684\u4f9d\u8d56\u5173\u7cfb\u3002<\/p>\n<p><strong>\u5982\u4f55\u901a\u8fc7\u547d\u4ee4\u884c\u8fd0\u884c\u5176\u4ed6\u7684.py\u6587\u4ef6\uff1f<\/strong><br \/>\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u901a\u8fc7\u8f93\u5165<code>python filename.py<\/code>\u6765\u76f4\u63a5\u8fd0\u884c\u5176\u4ed6\u7684.py\u6587\u4ef6\u3002\u786e\u4fdd\u5728\u547d\u4ee4\u884c\u4e2d\u5207\u6362\u5230\u8be5\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\uff0c\u6216\u8005\u63d0\u4f9b\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u7528\u4e8e\u6d4b\u8bd5\u811a\u672c\u6216\u8005\u8fd0\u884c\u72ec\u7acb\u7684Python\u7a0b\u5e8f\u3002\u5982\u679c\u9700\u8981\u4f20\u9012\u53c2\u6570\uff0c\u53ef\u4ee5\u5728\u547d\u4ee4\u540e\u52a0\u4e0a\u53c2\u6570\uff0c\u4f8b\u5982<code>python filename.py arg1 arg2<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8c03\u7528\u5176\u4ed6.py\u7a0b\u5e8f\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528os.system()\u3001subprocess\u6a21\u5757\u3001im [&hellip;]","protected":false},"author":3,"featured_media":1181995,"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\/1181986"}],"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=1181986"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1181986\/revisions"}],"predecessor-version":[{"id":1181996,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1181986\/revisions\/1181996"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1181995"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1181986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1181986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1181986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}