【问题标题】:PyInstaller + subprocess.check_output + Windows Scheduled taskPyInstaller + subprocess.check_output + Windows 计划任务
【发布时间】:2020-11-24 15:28:40
【问题描述】:

这是一个未来可能面临类似事情的人的话题。 一旦您尝试使用带有选项 -noconsole 的 PyInstaller 编译的可执行文件在 Windows 中安排任务并尝试使用 subprocess.check_output 它就不起作用

有趣的是,如果您直接调用 .exe,它就可以正常工作。但是,如果您安排任务或尝试将执行包装成批处理,例如:

C:\git\backend.exe -p C:\Users\settings.json

然后它会失败,没有任何错误代码。
在任务事件日志中只能找到错误代码:2147942401

【问题讨论】:

    标签: python scheduled-tasks pyinstaller


    【解决方案1】:

    解决方案是使用 stdin、out、err 显式指定 Popen

    p = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    output = p.stdout.read().decode('utf-8')
    p.communicate()  
    

    我发现了类似的主题,但只想明确提及默认情况下命令调用或 .exe 双击它可以工作,但不能来自计划任务

    灵感来自这里:
    Pyinstaller issue with subprocess.check_output
    Pyinstaller subprocess.check_output error
    pyinstaller on Windows with --noconsole simply won't work

    PS 我一直在寻找更好的解决方案,如果您有,请提出建议!

    【讨论】: