【发布时间】:2020-11-29 04:04:28
【问题描述】:
编辑:请注意,我已经看到有关此问题的其他主题,并且已经尝试了那里的大部分建议
我使用 pyinstaller 运行 .exe 文件,现在我尝试在没有控制台的情况下运行它(使用 -w 命令)。 我的一个关键库 patool 使用了子进程,它给出了以下错误:
Traceback (most recent call last):
File "apscheduler\executors\base.py", line 125, in run_job
File "script.py", line 478, in Archiver
File "patoolib\__init__.py", line 521, in _create_archive
File "patoolib\__init__.py", line 421, in run_archive_cmdlist
File "patoolib\util.py", line 227, in run_checked
File "patoolib\util.py", line 219, in run
File "subprocess.py", line 339, in call
File "subprocess.py", line 753, in __init__
File "subprocess.py", line 1090, in _get_handles
OSError: [WinError 6] The handle is invalid
这里是 patool util.py 代码的一部分,其中 subprocesses.call() 给出了错误:
def run (cmd, verbosity=0, **kwargs):
"""Run command without error checking.
@return: command return code"""
# Note that shell_quote_nt() result is not suitable for copy-paste
# (especially on Unix systems), but it looks nicer than shell_quote().
if verbosity >= 0:
log_info("running %s" % " ".join(map(shell_quote_nt, cmd)))
if kwargs:
if verbosity >= 0:
log_info(" with %s" % ", ".join("%s=%s" % (k, shell_quote(str(v)))\
for k, v in kwargs.items()))
if kwargs.get("shell"):
# for shell calls the command must be a string
cmd = " ".join(cmd)
if verbosity < 1:
# hide command output on stdout
with open(os.devnull, 'wb') as devnull:
kwargs['stdout'] = devnull
res = subprocess.call(cmd, **kwargs) <------------- ERROR
else:
res = subprocess.call(cmd, **kwargs)
return res
这是一个常见错误,因此我尝试阅读有关 subprocesses 模块的内容,并在网上挖掘了所有可能的建议,包括:
- 添加 kwargs['stdin'] = devnull,这里建议:Python running as Windows Service: OSError: [WinError 6] The handle is invalid
- 将 shell=True 添加到 call() 方法
- 在 run() 函数的开头添加 subprocess._cleanup()
这些都不起作用,处理程序仍然无效。该程序在控制台处于活动状态时运行良好。 我正在使用 Python 3.7、Anaconda3、64 位 Windows 10 操作系统。
在 util.py 后面有一个 subprocess.popen() 我怀疑它会导致我同样的问题。 我试图通过激活控制台然后隐藏它来运行.exe,但后来我遇到了其他问题(它不会在系统启动时启动)。我想现在的控制台非常重要,但我很想摆脱它以获得更好的用户体验。
【问题讨论】:
-
你试过
kwargs['stdout'] = subprocess.DEVNULL吗? (不是很乐观;文档暗示它只是os.devnull的别名。) -
链接的问题涉及
stdin,而您正在操纵stdout。尝试将两者都连接到os.devnull。如果解决了,这基本上是 stackoverflow.com/questions/40108816/… 的副本 -
感谢您的及时回复!你为什么说我在操纵标准输出?我发布的代码只是来自 patool util.py 的原始代码 - 原始代码中存在标准输出操作。在此之下,我说我还将
kwargs['stdin'] = devnull添加到此原始代码中。我也尝试将stdin=subprocess.DEVNULL添加为 call() 方法的参数,尽管它应该与 kwargs['stdin'] = devnull 一样。 -
我尝试实现
kwargs['stdout'] = subprocess.DEVNULL和kwargs['stdin'] = subprocess.DEVNULL- 没有变化:(
标签: python python-3.x subprocess pyinstaller