{"id":1184239,"date":"2025-01-15T19:24:11","date_gmt":"2025-01-15T11:24:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1184239.html"},"modified":"2025-01-15T19:24:13","modified_gmt":"2025-01-15T11:24:13","slug":"python%e7%a8%8b%e5%ba%8f%e5%a6%82%e4%bd%95%e6%89%93%e5%bc%80exe%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1184239.html","title":{"rendered":"python\u7a0b\u5e8f\u5982\u4f55\u6253\u5f00exe\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25133730\/290d4df5-2ffe-4a44-818c-cea4057a0c2a.webp\" alt=\"python\u7a0b\u5e8f\u5982\u4f55\u6253\u5f00exe\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u7a0b\u5e8f\u5982\u4f55\u6253\u5f00exe\u6587\u4ef6<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u3001<code>os<\/code>\u6a21\u5757\u3001<code>os.system<\/code>\u51fd\u6570\u3002<strong>\u5176\u4e2d\uff0c\u63a8\u8350\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u8fdb\u884c\u64cd\u4f5c\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u63a7\u5236\u9009\u9879\u548c\u66f4\u597d\u7684\u9519\u8bef\u5904\u7406\u673a\u5236<\/strong>\u3002\u4f8b\u5982\uff0c\u901a\u8fc7\u4f7f\u7528<code>subprocess.run()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u6355\u83b7\u8f93\u51fa\u3001\u5904\u7406\u9519\u8bef\u3001\u8bbe\u7f6e\u8d85\u65f6\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0<\/strong>\uff1a\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u6253\u5f00exe\u6587\u4ef6\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165<code>subprocess<\/code>\u6a21\u5757\uff0c\u7136\u540e\u8c03\u7528<code>subprocess.run()<\/code>\u6216<code>subprocess.Popen()<\/code>\u51fd\u6570\u6765\u6267\u884cexe\u6587\u4ef6\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4e0d\u4ec5\u4ec5\u9002\u7528\u4e8e\u6253\u5f00exe\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u6267\u884c\u5176\u4ed6\u7cfb\u7edf\u547d\u4ee4\u548c\u811a\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u4f7f\u7528subprocess.run()\u51fd\u6570\u6253\u5f00exe\u6587\u4ef6<\/strong><\/h2>\n<p>subprocess.run([&quot;path_to_exe_file\/executable_file.exe&quot;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u7a0b\u5e8f\u6253\u5f00exe\u6587\u4ef6\uff0c\u6db5\u76d6\u4e0d\u540c\u7684\u65b9\u6cd5\u548c\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528subprocess\u6a21\u5757<\/strong><\/h2>\n<p><h2>1\u3001subprocess.run()\u51fd\u6570<\/h2>\n<\/p>\n<p><p><code>subprocess.run()<\/code>\u51fd\u6570\u662fPython 3.5\u5f15\u5165\u7684\u4e00\u4e2a\u66f4\u9ad8\u7ea7\u7684\u63a5\u53e3\u3002\u5b83\u53ef\u4ee5\u6355\u83b7\u6807\u51c6\u8f93\u51fa\u3001\u6807\u51c6\u9519\u8bef\uff0c\u5e76\u8bbe\u7f6e\u8d85\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u6253\u5f00exe\u6587\u4ef6\u5e76\u7b49\u5f85\u5176\u5b8c\u6210\u6267\u884c<\/strong><\/h2>\n<p>result = subprocess.run([&quot;path_to_exe_file\/executable_file.exe&quot;], capture_output=True, text=True)<\/p>\n<h2><strong>\u6253\u5370\u8f93\u51fa<\/strong><\/h2>\n<p>print(result.stdout)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>capture_output=True<\/code>\u53c2\u6570\u6355\u83b7\u8f93\u51fa\uff0c\u5e76\u901a\u8fc7<code>text=True<\/code>\u53c2\u6570\u5c06\u8f93\u51fa\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h2>2\u3001subprocess.Popen()\u51fd\u6570<\/h2>\n<\/p>\n<p><p><code>subprocess.Popen()<\/code>\u51fd\u6570\u63d0\u4f9b\u4e86\u66f4\u5e95\u5c42\u7684\u63a5\u53e3\uff0c\u53ef\u4ee5\u5bf9\u8fdb\u7a0b\u8fdb\u884c\u66f4\u7cbe\u7ec6\u7684\u63a7\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u6253\u5f00exe\u6587\u4ef6\u5e76\u7ee7\u7eed\u6267\u884c\u540e\u7eed\u4ee3\u7801<\/strong><\/h2>\n<p>process = subprocess.Popen([&quot;path_to_exe_file\/executable_file.exe&quot;], stdout=subprocess.PIPE, stderr=subprocess.PIPE)<\/p>\n<h2><strong>\u7b49\u5f85\u8fdb\u7a0b\u5b8c\u6210\u5e76\u83b7\u53d6\u8f93\u51fa<\/strong><\/h2>\n<p>stdout, stderr = process.communicate()<\/p>\n<p>print(stdout.decode())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>subprocess.Popen()<\/code>\u51fd\u6570\u5141\u8bb8\u4f60\u5f02\u6b65\u5730\u6267\u884cexe\u6587\u4ef6\uff0c\u5e76\u901a\u8fc7<code>stdout<\/code>\u548c<code>stderr<\/code>\u53c2\u6570\u6355\u83b7\u8f93\u51fa\u548c\u9519\u8bef\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528os\u6a21\u5757<\/strong><\/h2>\n<p><h2>1\u3001os.system()\u51fd\u6570<\/h2>\n<\/p>\n<p><p><code>os.system()<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u5f0f\u6765\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\uff0c\u4f46\u5b83\u4e0d\u63d0\u4f9b\u6355\u83b7\u8f93\u51fa\u548c\u5904\u7406\u9519\u8bef\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u4f7f\u7528os.system()\u6253\u5f00exe\u6587\u4ef6<\/strong><\/h2>\n<p>os.system(&quot;path_to_exe_file\/executable_file.exe&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136<code>os.system()<\/code>\u51fd\u6570\u6613\u4e8e\u4f7f\u7528\uff0c\u4f46\u5b83\u5728\u529f\u80fd\u548c\u7075\u6d3b\u6027\u65b9\u9762\u4e0d\u5982<code>subprocess<\/code>\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h2>2\u3001os.startfile()\u51fd\u6570<\/h2>\n<\/p>\n<p><p><code>os.startfile()<\/code>\u51fd\u6570\u9002\u7528\u4e8eWindows\u64cd\u4f5c\u7cfb\u7edf\uff0c\u53ef\u4ee5\u7528\u6765\u6253\u5f00\u6587\u4ef6\u6216\u8fd0\u884c\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u4f7f\u7528os.startfile()\u6253\u5f00exe\u6587\u4ef6<\/strong><\/h2>\n<p>os.startfile(&quot;path_to_exe_file\/executable_file.exe&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>os.startfile()<\/code>\u51fd\u6570\u7c7b\u4f3c\u4e8e\u53cc\u51fb\u6587\u4ef6\uff0c\u5b83\u4f1a\u4f7f\u7528\u9ed8\u8ba4\u5173\u8054\u7684\u7a0b\u5e8f\u6253\u5f00\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u5904\u7406\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u5dee\u5f02<\/strong><\/h2>\n<p><p>\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u5bf9\u6587\u4ef6\u8def\u5f84\u548c\u547d\u4ee4\u7684\u5904\u7406\u65b9\u5f0f\u6709\u6240\u4e0d\u540c\uff0c\u56e0\u6b64\u5728\u7f16\u5199\u8de8\u5e73\u53f0\u4ee3\u7801\u65f6\u9700\u8981\u6ce8\u610f\u8fd9\u4e9b\u5dee\u5f02\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u68c0\u6d4b\u64cd\u4f5c\u7cfb\u7edf<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>os.name<\/code>\u6216<code>platform<\/code>\u6a21\u5757\u6765\u68c0\u6d4b\u64cd\u4f5c\u7cfb\u7edf\uff0c\u5e76\u6839\u636e\u4e0d\u540c\u7684\u7cfb\u7edf\u6267\u884c\u76f8\u5e94\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import platform<\/p>\n<p>if os.name == &#39;nt&#39;:  # Windows<\/p>\n<p>    os.startfile(&quot;path_to_exe_file\/executable_file.exe&quot;)<\/p>\n<p>elif os.name == &#39;posix&#39;:  # Unix\/Linux\/Mac<\/p>\n<p>    subprocess.run([&quot;.\/path_to_exe_file\/executable_file&quot;])<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Unsupported operating system&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84<\/h2>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u4ee3\u7801\u66f4\u5177\u53ef\u79fb\u690d\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u800c\u4e0d\u662f\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import subprocess<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u811a\u672c\u7684\u76ee\u5f55<\/strong><\/h2>\n<p>script_dir = os.path.dirname(os.path.abspath(__file__))<\/p>\n<h2><strong>\u6784\u5efa\u76f8\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>exe_path = os.path.join(script_dir, &quot;path_to_exe_file\/executable_file.exe&quot;)<\/p>\n<h2><strong>\u4f7f\u7528subprocess\u6253\u5f00exe\u6587\u4ef6<\/strong><\/h2>\n<p>subprocess.run([exe_path])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fddexe\u6587\u4ef6\u5728\u4e0d\u540c\u7cfb\u7edf\u4e0a\u7684\u76f8\u5bf9\u4f4d\u7f6e\u4e00\u81f4\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u6355\u83b7\u8f93\u51fa\u548c\u5904\u7406\u9519\u8bef<\/strong><\/h2>\n<p><p>\u5728\u8fd0\u884cexe\u6587\u4ef6\u65f6\uff0c\u6355\u83b7\u8f93\u51fa\u548c\u5904\u7406\u9519\u8bef\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<code>subprocess.run()<\/code>\u548c<code>subprocess.Popen()<\/code>\u51fd\u6570\u90fd\u63d0\u4f9b\u4e86\u6355\u83b7\u8f93\u51fa\u548c\u9519\u8bef\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u6355\u83b7\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528<code>subprocess.run()<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>capture_output<\/code>\u53c2\u6570\u6355\u83b7\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u6253\u5f00exe\u6587\u4ef6\u5e76\u6355\u83b7\u8f93\u51fa\u548c\u9519\u8bef<\/strong><\/h2>\n<p>result = subprocess.run([&quot;path_to_exe_file\/executable_file.exe&quot;], capture_output=True, text=True)<\/p>\n<h2><strong>\u6253\u5370\u6807\u51c6\u8f93\u51fa<\/strong><\/h2>\n<p>print(&quot;Standard Output:&quot;, result.stdout)<\/p>\n<h2><strong>\u6253\u5370\u6807\u51c6\u9519\u8bef<\/strong><\/h2>\n<p>print(&quot;Standard Error:&quot;, result.stderr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u5904\u7406\u5f02\u5e38\u548c\u9519\u8bef<\/h2>\n<\/p>\n<p><p>\u5728\u6267\u884cexe\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u548c\u9519\u8bef\u3002\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>try:<\/p>\n<p>    # \u6253\u5f00exe\u6587\u4ef6<\/p>\n<p>    result = subprocess.run([&quot;path_to_exe_file\/executable_file.exe&quot;], capture_output=True, text=True, check=True)<\/p>\n<p>    print(&quot;Standard Output:&quot;, result.stdout)<\/p>\n<p>except subprocess.CalledProcessError as e:<\/p>\n<p>    print(&quot;Error:&quot;, e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e<code>check=True<\/code>\u53c2\u6570\uff0c\u5982\u679c\u8fdb\u7a0b\u8fd4\u56de\u975e\u96f6\u9000\u51fa\u72b6\u6001\uff0c\u5c06\u5f15\u53d1<code>subprocess.CalledProcessError<\/code>\u5f02\u5e38\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u4f7f\u7528\u8d85\u65f6\u548c\u5f02\u6b65\u6267\u884c<\/strong><\/h2>\n<p><h2>1\u3001\u8bbe\u7f6e\u8d85\u65f6<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u4e3aexe\u6587\u4ef6\u7684\u6267\u884c\u8bbe\u7f6e\u8d85\u65f6\u3002\u53ef\u4ee5\u4f7f\u7528<code>timeout<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u8d85\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>try:<\/p>\n<p>    # \u6253\u5f00exe\u6587\u4ef6\u5e76\u8bbe\u7f6e\u8d85\u65f6<\/p>\n<p>    result = subprocess.run([&quot;path_to_exe_file\/executable_file.exe&quot;], timeout=10, capture_output=True, text=True)<\/p>\n<p>    print(&quot;Standard Output:&quot;, result.stdout)<\/p>\n<p>except subprocess.TimeoutExpired as e:<\/p>\n<p>    print(&quot;Process timed out:&quot;, e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u5f02\u6b65\u6267\u884c<\/h2>\n<\/p>\n<p><p><code>subprocess.Popen()<\/code>\u51fd\u6570\u5141\u8bb8\u5f02\u6b65\u6267\u884cexe\u6587\u4ef6\uff0c\u53ef\u4ee5\u5728\u7b49\u5f85\u8fdb\u7a0b\u5b8c\u6210\u7684\u540c\u65f6\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u6253\u5f00exe\u6587\u4ef6\u5e76\u5f02\u6b65\u6267\u884c<\/strong><\/h2>\n<p>process = subprocess.Popen([&quot;path_to_exe_file\/executable_file.exe&quot;], stdout=subprocess.PIPE, stderr=subprocess.PIPE)<\/p>\n<h2><strong>\u6267\u884c\u5176\u4ed6\u64cd\u4f5c<\/strong><\/h2>\n<p>print(&quot;Doing other work...&quot;)<\/p>\n<h2><strong>\u7b49\u5f85\u8fdb\u7a0b\u5b8c\u6210\u5e76\u83b7\u53d6\u8f93\u51fa<\/strong><\/h2>\n<p>stdout, stderr = process.communicate()<\/p>\n<p>print(&quot;Standard Output:&quot;, stdout.decode())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>subprocess.Popen()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u8fdb\u7a0b\u6267\u884c\u671f\u95f4\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u4ee3\u7801\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u6267\u884c\u5e26\u53c2\u6570\u7684exe\u6587\u4ef6<\/strong><\/h2>\n<p><p>\u6709\u65f6\u5019\u9700\u8981\u5411exe\u6587\u4ef6\u4f20\u9012\u53c2\u6570\uff0c\u53ef\u4ee5\u5728<code>subprocess.run()<\/code>\u6216<code>subprocess.Popen()<\/code>\u51fd\u6570\u4e2d\u6307\u5b9a\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u4f20\u9012\u547d\u4ee4\u884c\u53c2\u6570<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u6253\u5f00exe\u6587\u4ef6\u5e76\u4f20\u9012\u53c2\u6570<\/strong><\/h2>\n<p>subprocess.run([&quot;path_to_exe_file\/executable_file.exe&quot;, &quot;arg1&quot;, &quot;arg2&quot;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u4f7f\u7528\u5b57\u5178\u4f20\u9012\u73af\u5883\u53d8\u91cf<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>env<\/code>\u53c2\u6570\u4f20\u9012\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>import os<\/p>\n<h2><strong>\u6784\u5efa\u73af\u5883\u53d8\u91cf\u5b57\u5178<\/strong><\/h2>\n<p>env = os.environ.copy()<\/p>\n<p>env[&quot;MY_VAR&quot;] = &quot;value&quot;<\/p>\n<h2><strong>\u6253\u5f00exe\u6587\u4ef6\u5e76\u4f20\u9012\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p>subprocess.run([&quot;path_to_exe_file\/executable_file.exe&quot;], env=env)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u6267\u884cexe\u6587\u4ef6\u65f6\u8bbe\u7f6e\u7279\u5b9a\u7684\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<\/p>\n<h2><strong>\u4e03\u3001\u5904\u7406GUI\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p><p>\u5728\u4f7f\u7528Python\u6253\u5f00GUI\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u6709\u4e00\u4e9b\u7279\u5b9a\u7684\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u4f7f\u7528subprocess.Popen()\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u5bf9\u4e8eGUI\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f7f\u7528<code>subprocess.Popen()<\/code>\u51fd\u6570\u53ef\u4ee5\u907f\u514d\u963b\u585e\u4e3b\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u6253\u5f00GUI\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p>process = subprocess.Popen([&quot;path_to_exe_file\/gui_application.exe&quot;])<\/p>\n<h2><strong>\u6267\u884c\u5176\u4ed6\u64cd\u4f5c<\/strong><\/h2>\n<p>print(&quot;GUI application is running...&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u68c0\u6d4b\u8fdb\u7a0b\u72b6\u6001<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>poll()<\/code>\u65b9\u6cd5\u68c0\u6d4b\u8fdb\u7a0b\u7684\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u6253\u5f00GUI\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p>process = subprocess.Popen([&quot;path_to_exe_file\/gui_application.exe&quot;])<\/p>\n<h2><strong>\u68c0\u6d4b\u8fdb\u7a0b\u72b6\u6001<\/strong><\/h2>\n<p>if process.poll() is None:<\/p>\n<p>    print(&quot;GUI application is still running&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;GUI application has finished&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u68c0\u6d4b\u8fdb\u7a0b\u72b6\u6001\uff0c\u53ef\u4ee5\u5728GUI\u5e94\u7528\u7a0b\u5e8f\u8fd0\u884c\u65f6\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\uff0c\u5e76\u5728\u5176\u7ed3\u675f\u540e\u8fdb\u884c\u76f8\u5e94\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u516b\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/strong><\/h2>\n<p><p>\u9664\u4e86\u6807\u51c6\u5e93\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u6765\u6253\u5f00exe\u6587\u4ef6\uff0c\u4f8b\u5982<code>psutil<\/code>\u5e93\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u5b89\u88c5psutil\u5e93<\/h2>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install psutil<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u4f7f\u7528psutil\u5e93\u6253\u5f00exe\u6587\u4ef6<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">import psutil<\/p>\n<p>import subprocess<\/p>\n<h2><strong>\u6253\u5f00exe\u6587\u4ef6<\/strong><\/h2>\n<p>process = subprocess.Popen([&quot;path_to_exe_file\/executable_file.exe&quot;])<\/p>\n<h2><strong>\u83b7\u53d6\u8fdb\u7a0b\u4fe1\u606f<\/strong><\/h2>\n<p>proc_info = psutil.Process(process.pid)<\/p>\n<h2><strong>\u6253\u5370\u8fdb\u7a0b\u4fe1\u606f<\/strong><\/h2>\n<p>print(&quot;Process name:&quot;, proc_info.name())<\/p>\n<p>print(&quot;Process status:&quot;, proc_info.status())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>psutil<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u8fdb\u7a0b\u7ba1\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u83b7\u53d6\u8fdb\u7a0b\u4fe1\u606f\u3001\u76d1\u63a7\u8fdb\u7a0b\u72b6\u6001\u7b49\u3002<\/p>\n<\/p>\n<h2><strong>\u4e5d\u3001\u8de8\u5e73\u53f0\u517c\u5bb9\u6027<\/strong><\/h2>\n<p><p>\u5728\u7f16\u5199\u8de8\u5e73\u53f0\u4ee3\u7801\u65f6\uff0c\u9700\u8981\u8003\u8651\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u5dee\u5f02\uff0c\u5e76\u4f7f\u7528\u9002\u5f53\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u4f7f\u7528\u5e73\u53f0\u7279\u5b9a\u7684\u547d\u4ee4<\/h2>\n<\/p>\n<p><p>\u5bf9\u4e8eWindows\u548cUnix\u7cfb\u7edf\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u547d\u4ee4\u6765\u6253\u5f00exe\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import subprocess<\/p>\n<p>if os.name == &#39;nt&#39;:  # Windows<\/p>\n<p>    os.startfile(&quot;path_to_exe_file\/executable_file.exe&quot;)<\/p>\n<p>elif os.name == &#39;posix&#39;:  # Unix\/Linux\/Mac<\/p>\n<p>    subprocess.run([&quot;.\/path_to_exe_file\/executable_file&quot;])<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Unsupported operating system&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u4f7f\u7528\u8de8\u5e73\u53f0\u5e93<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u8de8\u5e73\u53f0\u5e93\uff0c\u4f8b\u5982<code>PyInstaller<\/code>\uff0c\u5c06Python\u811a\u672c\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u4ee5\u4fbf\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyinstaller<\/p>\n<h2><strong>\u6253\u5305Python\u811a\u672c<\/strong><\/h2>\n<p>pyinstaller --onefile script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u751f\u6210\u9002\u7528\u4e8e\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u7b80\u5316\u4e86\u8de8\u5e73\u53f0\u5f00\u53d1\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u4f7f\u7528Python\u7a0b\u5e8f\u6253\u5f00exe\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<strong>\u63a8\u8350\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u63a7\u5236\u9009\u9879\u548c\u66f4\u597d\u7684\u9519\u8bef\u5904\u7406\u673a\u5236<\/strong>\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u3001\u7b2c\u4e09\u65b9\u5e93\u7b49\u6765\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd\u3002\u5728\u7f16\u5199\u8de8\u5e73\u53f0\u4ee3\u7801\u65f6\uff0c\u9700\u8981\u8003\u8651\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u5dee\u5f02\uff0c\u5e76\u4f7f\u7528\u9002\u5f53\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5728Python\u7a0b\u5e8f\u4e2d\u6253\u5f00\u548c\u7ba1\u7406exe\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u5168\u5730\u6253\u5f00exe\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\u6253\u5f00exe\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u3002\u8fd9\u4e2a\u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u6267\u884c\u5916\u90e8\u547d\u4ee4\uff0c\u5e76\u80fd\u591f\u5904\u7406\u8f93\u5165\u548c\u8f93\u51fa\u6d41\u3002\u4f7f\u7528<code>subprocess.run()<\/code>\u6216<code>subprocess.Popen()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u542f\u52a8exe\u6587\u4ef6\uff0c\u786e\u4fdd\u5b89\u5168\u6027\u548c\u7a33\u5b9a\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import subprocess\n\nsubprocess.run([&#39;path_to_your_exe_file.exe&#39;])\n<\/code><\/pre>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u901a\u8fc7Python\u4f20\u9012\u53c2\u6570\u7ed9exe\u6587\u4ef6\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u65f6\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u4f20\u9012\u53c2\u6570\u7ed9exe\u6587\u4ef6\u3002\u53ea\u9700\u5728<code>subprocess.run()<\/code>\u6216<code>subprocess.Popen()<\/code>\u7684\u53c2\u6570\u5217\u8868\u4e2d\u6dfb\u52a0\u6240\u9700\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">subprocess.run([&#39;path_to_your_exe_file.exe&#39;, &#39;arg1&#39;, &#39;arg2&#39;])\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0cexe\u6587\u4ef6\u5c31\u53ef\u4ee5\u63a5\u6536\u5e76\u5904\u7406\u8fd9\u4e9b\u53c2\u6570\u4e86\u3002<\/p>\n<p><strong>\u5728\u6253\u5f00exe\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u5904\u7406\u53ef\u80fd\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u8c03\u7528exe\u6587\u4ef6\u65f6\uff0c\u5904\u7406\u6f5c\u5728\u7684\u9519\u8bef\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u5757\u6765\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import subprocess\n\ntry:\n    subprocess.run([&#39;path_to_your_exe_file.exe&#39;], check=True)\nexcept subprocess.CalledProcessError as e:\n    print(f&quot;An error occurred: {e}&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u6355\u83b7\u6267\u884cexe\u6587\u4ef6\u65f6\u7684\u9519\u8bef\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7a0b\u5e8f\u5982\u4f55\u6253\u5f00exe\u6587\u4ef6\uff1a\u53ef\u4ee5\u4f7f\u7528subprocess\u6a21\u5757\u3001os\u6a21\u5757\u3001os.system\u51fd\u6570\u3002\u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1184246,"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\/1184239"}],"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=1184239"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1184239\/revisions"}],"predecessor-version":[{"id":1184247,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1184239\/revisions\/1184247"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1184246"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1184239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1184239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1184239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}