{"id":1001828,"date":"2024-12-27T10:03:38","date_gmt":"2024-12-27T02:03:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1001828.html"},"modified":"2024-12-27T10:03:41","modified_gmt":"2024-12-27T02:03:41","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%b0%83%e7%94%a8exe","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1001828.html","title":{"rendered":"\u5982\u4f55\u7528python\u8c03\u7528exe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075833\/36edc447-333c-44a9-9b1a-ccb07c7ed4d0.webp\" alt=\"\u5982\u4f55\u7528python\u8c03\u7528exe\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u8c03\u7528exe\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u3001<code>os.system<\/code>\u51fd\u6570\u548c<code>Popen<\/code>\u5bf9\u8c61\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f<\/strong>\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u91cd\u70b9\u8ba8\u8bba<code>subprocess<\/code>\u6a21\u5757\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\u548c\u529f\u80fd\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001SUBPROCESS\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>subprocess<\/code>\u6a21\u5757\u662fPython\u4e2d\u7528\u4e8e\u7ba1\u7406\u5b50\u8fdb\u7a0b\u7684\u6807\u51c6\u6a21\u5757\u3002\u5b83\u63d0\u4f9b\u4e86\u591a\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u65b9\u6cd5\u6765\u521b\u5efa\u548c\u7ba1\u7406\u5b50\u8fdb\u7a0b\uff0c\u5141\u8bb8\u4f60\u5728Python\u811a\u672c\u4e2d\u6267\u884c\u5916\u90e8\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>1.1\u3001\u4f7f\u7528subprocess.run<\/p>\n<\/p>\n<p><p><code>subprocess.run<\/code>\u662f<code>subprocess<\/code>\u6a21\u5757\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u5b83\u5141\u8bb8\u4f60\u6267\u884c\u4e00\u4e2a\u547d\u4ee4\uff0c\u5e76\u7b49\u5f85\u5b83\u5b8c\u6210\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u7b80\u5355\u6267\u884c\u547d\u4ee4\u5e76\u83b7\u53d6\u8fd4\u56de\u7801\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u8c03\u7528exe\u6587\u4ef6<\/strong><\/h2>\n<p>result = subprocess.run([&#39;path\/to\/your\/executable.exe&#39;, &#39;arg1&#39;, &#39;arg2&#39;], capture_output=True, text=True)<\/p>\n<h2><strong>\u68c0\u67e5\u8fd4\u56de\u7801<\/strong><\/h2>\n<p>if result.returncode == 0:<\/p>\n<p>    print(&quot;Execution successful&quot;)<\/p>\n<p>    print(result.stdout)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Execution f<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led&quot;)<\/p>\n<p>    print(result.stderr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>subprocess.run<\/code>\u88ab\u7528\u4e8e\u6267\u884c\u4e00\u4e2aexe\u6587\u4ef6\u3002<code>capture_output=True<\/code>\u4f1a\u6355\u83b7\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\uff0c\u5e76\u5b58\u50a8\u5728<code>result.stdout<\/code>\u548c<code>result.stderr<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><p>1.2\u3001\u4f7f\u7528subprocess.Popen<\/p>\n<\/p>\n<p><p><code>subprocess.Popen<\/code>\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u63a5\u53e3\uff0c\u53ef\u4ee5\u8ba9\u4f60\u5728\u6267\u884c\u547d\u4ee4\u65f6\u4e0d\u963b\u585ePython\u7a0b\u5e8f\u7684\u6267\u884c\u3002\u56e0\u6b64\uff0c\u5b83\u9002\u7528\u4e8e\u9700\u8981\u540c\u65f6\u5904\u7406\u591a\u4e2a\u8fdb\u7a0b\u6216\u9700\u8981\u5b9e\u65f6\u5904\u7406\u8f93\u51fa\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u521b\u5efaPopen\u5bf9\u8c61<\/strong><\/h2>\n<p>process = subprocess.Popen([&#39;path\/to\/your\/executable.exe&#39;, &#39;arg1&#39;, &#39;arg2&#39;], stdout=subprocess.PIPE, stderr=subprocess.PIPE)<\/p>\n<h2><strong>\u5b9e\u65f6\u8bfb\u53d6\u8f93\u51fa<\/strong><\/h2>\n<p>stdout, stderr = process.communicate()<\/p>\n<h2><strong>\u68c0\u67e5\u8fd4\u56de\u7801<\/strong><\/h2>\n<p>if process.returncode == 0:<\/p>\n<p>    print(&quot;Execution successful&quot;)<\/p>\n<p>    print(stdout.decode())<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Execution failed&quot;)<\/p>\n<p>    print(stderr.decode())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>subprocess.Popen<\/code>\u88ab\u7528\u4e8e\u6267\u884c\u4e00\u4e2aexe\u6587\u4ef6\uff0c\u5e76\u4e14\u53ef\u4ee5\u5b9e\u65f6\u8bfb\u53d6\u8f93\u51fa\u3002<code>stdout=subprocess.PIPE<\/code>\u548c<code>stderr=subprocess.PIPE<\/code>\u7528\u4e8e\u6355\u83b7\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001OS.SYSTEM\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>os.system<\/code>\u662fPython\u4e2d\u6700\u7b80\u5355\u7684\u8c03\u7528\u5916\u90e8\u547d\u4ee4\u7684\u65b9\u6cd5\u3002\u5b83\u9002\u5408\u7528\u4e8e\u4e0d\u9700\u8981\u6355\u83b7\u8f93\u51fa\u7684\u7b80\u5355\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u8c03\u7528exe\u6587\u4ef6<\/strong><\/h2>\n<p>status = os.system(&#39;path\/to\/your\/executable.exe arg1 arg2&#39;)<\/p>\n<h2><strong>\u68c0\u67e5\u8fd4\u56de\u7801<\/strong><\/h2>\n<p>if status == 0:<\/p>\n<p>    print(&quot;Execution successful&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Execution failed&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>os.system<\/code>\u76f4\u63a5\u8c03\u7528\u547d\u4ee4\uff0c\u5e76\u8fd4\u56de\u547d\u4ee4\u7684\u8fd4\u56de\u7801\u3002\u4e0d\u8fc7\uff0c\u7531\u4e8e\u5b83\u4e0d\u80fd\u6355\u83b7\u547d\u4ee4\u7684\u8f93\u51fa\uff0c\u56e0\u6b64\u5728\u9700\u8981\u5904\u7406\u8f93\u51fa\u7684\u573a\u666f\u4e2d\u4e0d\u592a\u9002\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5176\u4ed6\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\uff0cPython\u4e2d\u8fd8\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u6a21\u5757\u53ef\u4ee5\u7528\u4e8e\u8c03\u7528exe\u6587\u4ef6\u3002\u4f8b\u5982\uff0c<code>shlex<\/code>\u53ef\u4ee5\u7528\u4e8e\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\uff0c<code>psutil<\/code>\u53ef\u4ee5\u7528\u4e8e\u7ba1\u7406\u7cfb\u7edf\u8fdb\u7a0b\u3002\u8fd9\u4e9b\u6a21\u5757\u53ef\u4ee5\u4e3a\u7279\u5b9a\u573a\u666f\u63d0\u4f9b\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>3.1\u3001\u4f7f\u7528shlex\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>shlex<\/code>\u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5b89\u5168\u5730\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u4ece\u800c\u907f\u514d\u5728\u5904\u7406\u590d\u6742\u53c2\u6570\u65f6\u51fa\u9519\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>import shlex<\/p>\n<h2><strong>\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570<\/strong><\/h2>\n<p>cmd = &#39;path\/to\/your\/executable.exe arg1 arg2&#39;<\/p>\n<p>args = shlex.split(cmd)<\/p>\n<h2><strong>\u8c03\u7528exe\u6587\u4ef6<\/strong><\/h2>\n<p>result = subprocess.run(args, capture_output=True, text=True)<\/p>\n<p>if result.returncode == 0:<\/p>\n<p>    print(&quot;Execution successful&quot;)<\/p>\n<p>    print(result.stdout)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Execution failed&quot;)<\/p>\n<p>    print(result.stderr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3.2\u3001\u4f7f\u7528psutil\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>psutil<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u7528\u4e8e\u83b7\u53d6\u548c\u5904\u7406\u7cfb\u7edf\u8fdb\u7a0b\u4fe1\u606f\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u76d1\u63a7exe\u6587\u4ef6\u7684\u6267\u884c\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import psutil<\/p>\n<p>import subprocess<\/p>\n<h2><strong>\u542f\u52a8exe\u6587\u4ef6<\/strong><\/h2>\n<p>process = subprocess.Popen([&#39;path\/to\/your\/executable.exe&#39;, &#39;arg1&#39;, &#39;arg2&#39;])<\/p>\n<h2><strong>\u83b7\u53d6\u8fdb\u7a0b\u4fe1\u606f<\/strong><\/h2>\n<p>proc = psutil.Process(process.pid)<\/p>\n<p>print(f&quot;Process ID: {proc.pid}&quot;)<\/p>\n<p>print(f&quot;Process Name: {proc.name()}&quot;)<\/p>\n<p>print(f&quot;CPU Usage: {proc.cpu_percent(interval=1.0)}%&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8c03\u7528exe\u6587\u4ef6\u6709\u591a\u79cd\u65b9\u6cd5\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u547d\u4ee4\u6267\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.system<\/code>\uff1b\u5982\u679c\u9700\u8981\u66f4\u591a\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027\uff0c<code>subprocess<\/code>\u6a21\u5757\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u590d\u6742\u53c2\u6570\u548c\u8fdb\u7a0b\u7ba1\u7406\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>shlex<\/code>\u548c<code>psutil<\/code>\u7b49\u6a21\u5757\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u5347\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8c03\u7528\u5176\u4ed6\u7a0b\u5e8f\u7684.exe\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>subprocess<\/code>\u6a21\u5757\u6765\u8c03\u7528.exe\u6587\u4ef6\u3002\u4f7f\u7528<code>subprocess.run()<\/code>\u51fd\u6570\u53ef\u4ee5\u6267\u884c\u5916\u90e8\u547d\u4ee4\u5e76\u7b49\u5f85\u5176\u5b8c\u6210\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import subprocess\n\nsubprocess.run([&#39;path_to_your_executable.exe&#39;, &#39;arg1&#39;, &#39;arg2&#39;])\n<\/code><\/pre>\n<p>\u786e\u4fdd\u66ff\u6362<code>path_to_your_executable.exe<\/code>\u4e3a\u5b9e\u9645\u7684.exe\u6587\u4ef6\u8def\u5f84\uff0c\u5e76\u6839\u636e\u9700\u8981\u6dfb\u52a0\u53c2\u6570\u3002<\/p>\n<p><strong>\u8c03\u7528.exe\u6587\u4ef6\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u6743\u9650\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8c03\u7528.exe\u6587\u4ef6\u53ef\u80fd\u9700\u8981\u7ba1\u7406\u5458\u6743\u9650\uff0c\u7279\u522b\u662f\u5f53\u8be5\u7a0b\u5e8f\u5c1d\u8bd5\u8bbf\u95ee\u7cfb\u7edf\u8d44\u6e90\u6216\u8fdb\u884c\u7cfb\u7edf\u66f4\u6539\u65f6\u3002\u5982\u679c\u9047\u5230\u6743\u9650\u95ee\u9898\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884cPython\u811a\u672c\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4ecePython\u6355\u83b7.exe\u6587\u4ef6\u7684\u8f93\u51fa\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7<code>subprocess.Popen()<\/code>\u6765\u6355\u83b7.exe\u6587\u4ef6\u7684\u6807\u51c6\u8f93\u51fa\u548c\u9519\u8bef\u8f93\u51fa\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">process = subprocess.Popen([&#39;path_to_your_executable.exe&#39;], stdout=subprocess.PIPE, stderr=subprocess.PIPE)\nstdout, stderr = process.communicate()\nprint(stdout.decode())\nprint(stderr.decode())\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u5f00\u53d1\u8005\u5206\u6790\u7a0b\u5e8f\u7684\u8f93\u51fa\uff0c\u4fbf\u4e8e\u8c03\u8bd5\u6216\u8bb0\u5f55\u65e5\u5fd7\u3002<\/p>\n<p><strong>\u5728\u8c03\u7528.exe\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\u6765\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u5757\u6765\u6355\u83b7<code>subprocess.CalledProcessError<\/code>\u5f02\u5e38\uff0c\u8fd9\u6837\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u7a0b\u5e8f\u6d41\u5e76\u8fdb\u884c\u9519\u8bef\u5904\u7406\uff1a<\/p>\n<pre><code class=\"language-python\">import subprocess\n\ntry:\n    subprocess.run([&#39;path_to_your_executable.exe&#39;], check=True)\nexcept subprocess.CalledProcessError as e:\n    print(f&#39;Error occurred: {e}&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5728.exe\u6587\u4ef6\u8fd0\u884c\u5931\u8d25\u65f6\u91c7\u53d6\u9002\u5f53\u7684\u8865\u6551\u63aa\u65bd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u8c03\u7528exe\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528subprocess\u6a21\u5757\u3001os.sys [&hellip;]","protected":false},"author":3,"featured_media":1001835,"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\/1001828"}],"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=1001828"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001828\/revisions"}],"predecessor-version":[{"id":1001841,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001828\/revisions\/1001841"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1001835"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1001828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1001828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1001828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}