Subprocess.popen 的 Pyinstaller 作为 exe 失败
Pyinstaller with Subprocess.popen fails as exe
在使用 pyInstaller 创建 exe 时,我正在努力实现子进程。
我有一个程序有一个 gui (tkinter),其中多个按钮有它们的 subprocess.popen()。有一个通过 ssh 发送到网络设备的命令需要一些时间才能完成。
在 pyCharm 这工作顺利。
当我使用 pyInstaller 创建 exe 时,出现以下错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "tkinter\__init__.py", line 1699, in __call__
File "gui.py" in line 197 in wrapper
File "subprocess.py", line 707, in __init__
File "subprocess.py", line 990, in _execute_child
FileNotFoundError: [WinError 2] The system cannot find the file specified
key not found: process1
第 197 行 gui.py 对应的代码片段是:
proc = subprocess.Popen(['python', r'commands/pec_cmd.py',
str(cmd), pec_ip, '1', '0', '0', '0', '0'],
startupinfo=subprocess.STARTUPINFO(), env=os.environ,
stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
稍后我将流程存储在字典中,该字典之前使用 subprocesses = {} 初始化:
subprocesses['process' + str(i)] = proc
知道我为什么会收到此 FileNotFoundError 的任何想法或帮助吗?
当 PyCharm 中的 运行 有效时,但一旦创建了 exe(--onedir 和 --onefile 已尝试),它就会失败
您需要确定未找到哪个文件 python.exe
或 commands/pec_cmd.py
。
您可以尝试 运行 当前 shell 中的子流程:subprocess.Popen(... shell=True)
或使用子流程的完整路径
subprocess.Popen(['c:/python/python.exe', r'c:/whateverPath/commands/pec_cmd.py'])
但您仍然依赖于预装的 Python 解释器:
假设您想 运行 您的 application/exe 另一台没有 Python 安装子进程的计算机将失败,或者您将始终需要安装 Python.
谢谢@Maurice Meyer。我并不害怕这样的事实,那就是 python 仍然需要安装。所以我将其更改为调用一个exe:
现在,当使用 exe 直接调用 subprocess.popen("pec_cmd.exe", ...)
而不是使用脚本 pec_cmd.py 调用 python 时,它不再调用 subprocess.popen("python", commands/pec_cmd.py", ...)
。
在使用 pyInstaller 创建 exe 时,我正在努力实现子进程。
我有一个程序有一个 gui (tkinter),其中多个按钮有它们的 subprocess.popen()。有一个通过 ssh 发送到网络设备的命令需要一些时间才能完成。
在 pyCharm 这工作顺利。 当我使用 pyInstaller 创建 exe 时,出现以下错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "tkinter\__init__.py", line 1699, in __call__
File "gui.py" in line 197 in wrapper
File "subprocess.py", line 707, in __init__
File "subprocess.py", line 990, in _execute_child
FileNotFoundError: [WinError 2] The system cannot find the file specified
key not found: process1
第 197 行 gui.py 对应的代码片段是:
proc = subprocess.Popen(['python', r'commands/pec_cmd.py',
str(cmd), pec_ip, '1', '0', '0', '0', '0'],
startupinfo=subprocess.STARTUPINFO(), env=os.environ,
stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
稍后我将流程存储在字典中,该字典之前使用 subprocesses = {} 初始化:
subprocesses['process' + str(i)] = proc
知道我为什么会收到此 FileNotFoundError 的任何想法或帮助吗? 当 PyCharm 中的 运行 有效时,但一旦创建了 exe(--onedir 和 --onefile 已尝试),它就会失败
您需要确定未找到哪个文件 python.exe
或 commands/pec_cmd.py
。
您可以尝试 运行 当前 shell 中的子流程:subprocess.Popen(... shell=True)
或使用子流程的完整路径
subprocess.Popen(['c:/python/python.exe', r'c:/whateverPath/commands/pec_cmd.py'])
但您仍然依赖于预装的 Python 解释器:
假设您想 运行 您的 application/exe 另一台没有 Python 安装子进程的计算机将失败,或者您将始终需要安装 Python.
谢谢@Maurice Meyer。我并不害怕这样的事实,那就是 python 仍然需要安装。所以我将其更改为调用一个exe:
现在,当使用 exe 直接调用 subprocess.popen("pec_cmd.exe", ...)
而不是使用脚本 pec_cmd.py 调用 python 时,它不再调用 subprocess.popen("python", commands/pec_cmd.py", ...)
。