Python Subprocess.Popen 是否可以在 Electron 应用程序启动时处理/忽略 ENOENT 错误并继续进行而不失败,如在命令行中

分享于2022年07月17日 electron enoent java python subprocess 问答
【问题标题】:Python Subprocess.Popen 是否可以在 Electron 应用程序启动时处理/忽略 ENOENT 错误并继续进行而不失败,如在命令行中(Could Python Subprocess.Popen handle/ignore ENOENT error on Electron app launch and proceed ahead without fail as in command line)
【发布时间】:2022-01-14 23:06:18
【问题描述】:

subprocess.open 抛出错误 spawn ./jre/bin/java ENOENT 异常

import subprocess
subprocess.run([r"C:\Users\\AppData\Local\Programs\\.exe", "--args", "--remote-debugging-port=9000"])

也试过了,

subprocess.Popen([r"C:\Users\\AppData\Local\Programs\\.exe", "--args", "--remote-debugging-port=9000"])

还在 Popen 中尝试了 shell=True 参数,但没有解决

Could Python Subprocess.Popen handle/ignore ENOENT error on Electron app launch and proceed ahead without fail as in command line

手动启动此应用不会导致任何问题。

  1. 不确定导致此问题的原因 - 我该如何解决?

  2. 当我从命令运行此命令时,会出现类似的内容 控制台输出,但没有出现阻塞对话框并且应用程序会出现 不崩溃 - 有什么办法可以

也从 python 中复制非崩溃行为。

以下是命令行输出:

c:\Users\\AppData\Local\Programs\>
18:56:05.542 > Start in production mode
18:56:05.585 > electron-log.transports.file: Can't write to c:\Users\\AppData\Local\Programs\\resources\app.asar\logs\main.log Error: ENOTDIR, not a directory
    at createError (electron/js2c/asar_bundle.js:5:1382)
    at Object.e.mkdirSync (electron/js2c/asar_bundle.js:5:12161)
    at e (c:\Users\\AppData\Local\Programs\\resources\app.asar\background.js:1:249748)
    at l.testFileWriting (c:\Users\\AppData\Local\Programs\\resources\app.asar\background.js:1:249938)
    at l.createFile (c:\Users\\AppData\Local\Programs\\resources\app.asar\background.js:1:249488)
    at l.provide (c:\Users\\AppData\Local\Programs\\resources\app.asar\background.js:1:249315)
    at d (c:\Users\\AppData\Local\Programs\\resources\app.asar\background.js:1:246666)
    at h (c:\Users\\AppData\Local\Programs\\resources\app.asar\background.js:1:246165)
    at i (c:\Users\\AppData\Local\Programs\\resources\app.asar\background.js:1:47829)
    at n (c:\Users\\AppData\Local\Programs\\resources\app.asar\background.js:1:47621)
18:56:05.606 > Launching server at backend\-backend-0.0.1-SNAPSHOT.jar at port 8885...
18:56:05.724 > Server PID: 69044
18:56:10.731 >
18:56:10.733 >   .   ____          _            __ _ _
18:56:10.737 >  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
18:56:10.739 > ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
18:56:10.740 >  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
18:56:10.741 >   '  |____| .__|_| |_|_| |_\__, | / / / /
18:56:10.742 >  =========|_|==============|___/=/_/_/_/
18:56:10.744 >  :: Spring Boot ::                (v2.5.4)
18:56:10.745 >
18:56:11.126 > 2021-12-10 18:56:11.119  INFO 69044 --- [           main] p.core.BackendApplication  : Starting BackendApplication using Java 11.0.13 on G1-6FWX7C3-L with PID 69044 (C:\Users\\AppData\Local\Programs\\backend\-backend-0.0.1-SNAPSHOT.jar started by  in c:\Users\\AppData\Local\Programs\)
18:56:11.127 > 2021-12-10 18:56:11.126  INFO 69044 --- [           main] p.core.BackendApplication  : No active profile set, falling back to default profiles: default
18:56:18.360 > 2021-12-10 18:56:18.359  INFO 69044 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8885 (http)
18:56:18.446 > 2021-12-10 18:56:18.444  INFO 69044 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
18:56:18.447 > 2021-12-10 18:56:18.445  INFO 69044 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.52]
18:56:18.889 > 2021-12-10 18:56:18.887  INFO 69044 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
18:56:18.891 > 2021-12-10 18:56:18.888  INFO 69044 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 7499 ms
18:56:23.934 > 2021-12-10 18:56:23.933  INFO 69044 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 14 endpoint(s) beneath base path '/rest/actuator'
18:56:24.174 > 2021-12-10 18:56:24.172  INFO 69044 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8885 (http) with context path ''
18:56:24.241 > 2021-12-10 18:56:24.241  INFO 69044 --- [           main] p.core.BackendApplication  : Started BackendApplication in 15.385 seconds (JVM running for 17.665)
18:56:24.644 > 2021-12-10 18:56:24.642  INFO 69044 --- [nio-8885-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
18:56:24.645 > 2021-12-10 18:56:24.643  INFO 69044 --- [nio-8885-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
18:56:24.657 > 2021-12-10 18:56:24.647  INFO 69044 --- [nio-8885-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 4 ms

  • 你试过 Popen 来自 subprocess
  • 是的@TamilSelvan 这就是我正在尝试但没有工作
  • 这是一个常见的FAQ,你需要把命令行完全拆分成一个列表,比如 subprocess.run([r"C:\Users\<user>\AppData\Local\Programs\<some_dir>\<some_tool>.exe", "--args", "--remote-debugging-port=9000"]) 。等我找一个合适的副本。
  • @tripleee - 我也尝试过,但没有奏效 - 我也查找了这些解决方案,但没有奏效
  • 次要错误与 Python 中的任何内容都无关。如果 <some_tool>.exe 没有正确启动 Java,则调用代码无法解决此问题。

【解决方案1】:

好的 - 所以 Electron 包与它自己的 jre 版本捆绑在一起,这导致了问题,因为调用 exe 导致 JAVA 依赖项在系统默认 jre 路径中查找,而不是与捆绑在一起的路径Electron 应用程序。

解决方案 - 切换到应用程序的基本目录,然后启动它:

import subprocess, os
from subprocess import *
os.chdir(r"C:\app_dir")
subprocess.Popen(['app.exe'])

希望这对面临类似问题的人有所帮助。