可以 Python Subprocess.Popen handle/ignore Electron 应用程序启动时出现 ENOENT 错误并像在命令行中一样继续前进

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

subprocess.open 抛出错误生成。/jre/bin/java ENOENT 异常

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

也试过了,

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

也尝试了Popen中的shell=True参数但没有解决

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

  1. 不确定是什么导致了这个问题 - 我该如何解决?

  2. 当我 运行 this from command 有类似的出现在 控制台输出但没有出现阻塞对话框并且应用程序出现 没有崩溃 - 我有什么办法可以

同样复制 python 的非崩溃行为。

命令行输出如下:

c:\Users\<user>\AppData\Local\Programs\<base_dir>>
18:56:05.542 > Start in production mode
18:56:05.585 > electron-log.transports.file: Can't write to c:\Users\<user>\AppData\Local\Programs\<base_dir>\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\<user>\AppData\Local\Programs\<base_dir>\resources\app.asar\background.js:1:249748)
    at l.testFileWriting (c:\Users\<user>\AppData\Local\Programs\<base_dir>\resources\app.asar\background.js:1:249938)
    at l.createFile (c:\Users\<user>\AppData\Local\Programs\<base_dir>\resources\app.asar\background.js:1:249488)
    at l.provide (c:\Users\<user>\AppData\Local\Programs\<base_dir>\resources\app.asar\background.js:1:249315)
    at d (c:\Users\<user>\AppData\Local\Programs\<base_dir>\resources\app.asar\background.js:1:246666)
    at h (c:\Users\<user>\AppData\Local\Programs\<base_dir>\resources\app.asar\background.js:1:246165)
    at i (c:\Users\<user>\AppData\Local\Programs\<base_dir>\resources\app.asar\background.js:1:47829)
    at n (c:\Users\<user>\AppData\Local\Programs\<base_dir>\resources\app.asar\background.js:1:47621)
18:56:05.606 > Launching server at backend\<application>-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.<application>BackendApplication  : Starting <application>BackendApplication using Java 11.0.13 on G1-6FWX7C3-L with PID 69044 (C:\Users\<user>\AppData\Local\Programs\<base_dir>\backend\<application>-backend-0.0.1-SNAPSHOT.jar started by <user> in c:\Users\<user>\AppData\Local\Programs\<base_dir>)
18:56:11.127 > 2021-12-10 18:56:11.126  INFO 69044 --- [           main] p.core.<application>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.<application>BackendApplication  : Started <application>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

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

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

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

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