【问题标题】:Pyinstaller with Subprocess.popen fails as exe带有 Subprocess.popen 的 Pyinstaller 作为 exe 失败
【发布时间】:2019-01-02 02:37:41
【问题描述】:

在使用 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

gui.py 第 197 行的相应代码是:

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)

稍后我将进程存储在一个字典中,该字典之前使用 subproccesses = {} 初始化:

subprocesses['process' + str(i)] = proc

任何想法或帮助为什么我得到这个 FileNotFoundError? 从 PyCharm 运行时,它可以工作,但是一旦创建了 exe(尝试了 --onedir 和 --onefile),它就会失败

【问题讨论】:

    标签: python tkinter subprocess pyinstaller


    【解决方案1】:

    您需要确定哪个文件未找到 python.execommands/pec_cmd.py

    您可以尝试在当前 shell 中运行子进程:subprocess.Popen(... shell=True) 或使用子进程的完整路径

    subprocess.Popen(['c:/python/python.exe', r'c:/whateverPath/commands/pec_cmd.py'])
    

    但您仍然依赖预安装的 Python 解释器:
    想象一下,您想在另一台没有安装 Python 的计算机上运行您的应用程序/exe,子进程将失败,或者您将始终需要安装 Python。

    【讨论】:

      【解决方案2】:

      感谢@Maurice Meyer。我并不害怕这样一个事实,那就是仍然需要安装 python。所以我把它改成调用一个exe: 而不是调用subprocess.popen("python", commands/pec_cmd.py", ...),它现在可以在直接使用exe而不是使用脚本pec_cmd.py的python调用subprocess.popen("pec_cmd.exe", ...)时工作。

      【讨论】:

        猜你喜欢
        • 2018-12-09
        • 2013-08-22
        • 2021-09-14
        • 1970-01-01
        • 2016-08-01
        • 2019-12-04
        • 2019-08-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多