【问题标题】:Pyinstaller executable keeps openingPyinstaller 可执行文件不断打开
【发布时间】:2019-08-13 03:59:14
【问题描述】:

背景

我正在关注this link 进行人脸识别,我想使用 Python 构建一个独立的应用程序。我的main.py 脚本如下所示。

# main.py

# Import packages and other scripts
import tkinter as tk
...

# Some functions
def initialization():
    # OpenCV and sklearn are used here
    ...

def registration():
    # OpenCV, and sklearn are used here
    ...

def face_recognition():
    # OpenCV and sklearn are used here
    ...

# Start the Tkinter GUI
window = tk.Tk()

# Initialize the face recognition model
initialization()

# Input name for registration
tk.Label(window, text = "Name").grid(...)
entry1 = tk.Entry(window)
entry1.grid(row=0, column=1)

# When the button is clicked, different command is executed
tk.Button(window, text='Registration', command=registeration).grid(...) 
tk.Button(window, text='Face Recognition', command=face_recognition).grid(...)

window.mainloop()

使用python解释运行脚本(python main.py),一切正常。


问题

我使用 Pyinstaller 通过以下命令将脚本转换为单个 exe:

pyinstaller --onefile \
            --windowed \
            --hidden-import sklearn.neighbors.typedefs \
            main.py

然后,我生成了两个 exe。第一个在dist/main,第二个在dist/main.app/Contents/MacOS/main

运行exe时,exe会不断重复。我显示正在运行的进程,发现了这个结果

$ ps
PID    TTY    TIME    CMD
...    ...    ...     /path/to/main -B -s -S -E -c from multiprocessing.semaphore_tracker import main:main(8)
...    ...    ...     /path/to/main -B -s -S -E -c from multiprocessing.semaphore_tracker import main:main(8)
...    ...    ...     /path/to/main -B -s -S -E -c from multiprocessing.semaphore_tracker import main:main(7)
...    ...    ...     /path/to/main -B -s -S -E -c from multiprocessing.semaphore_tracker import main:main(7)
...    ...    ...     /path/to/main -B -s -S -E -c from multiprocessing.semaphore_tracker import main:main(8)

我不知道 exe 会发生什么,因为我没有在我的脚本中导入多处理包。知道如何解决这个问题吗?谢谢


更新 1

我在使用Pyinstaller 时添加了--log-level ERROR 并给出了这个结果。不确定是否与我的问题有关。

Traceback (most recent call last):
  File "<string>", line 2, in <module>
ModuleNotFoundError: No module named 'Crypto.Math'
174598 INFO: MKL libraries found when importing numpy. Adding MKL to binaries
176282 ERROR: Can not find path ./libtbb.dylib (needed by /Users/user/anaconda3/lib/libmkl_tbb_thread.dylib)

更新 2

我发现我正在使用的软件包之一 -- imultis VideoStream 涉及threading。我想这是观察from multiprocessing.semaphore_tracker 的原因,即使我没有明确import multiprocessing

然后遇到this post,建议加multiprocessing.freeze_support()。它可以抑制 GUI 窗口不断重复,但使用 $ ps 显示的后台进程仍然保持重复。问题还没有解决。


更新 3(问题已找到但未修复)

调试了半天代码,发现这个死循环的原因不是imultisVideoStream的threading造成的(我写了另一个脚本测试imultis完全OK )。但是问题来自于导入sklearn!!!此问题已在 GitHub 中this link 报告。

此 GitHub 链接还建议包含 multiprocessing.freeze_support()。此外,其中一个响应建议在调用multiprocessing.freeze_support() 之后导入sklearn。我尝试了一个简单的脚本,但问题仍然存在。

结论sklearn 导致可执行文件不断打开。但我不知道为什么会这样,也不知道如何解决。任何帮助,将不胜感激。谢谢。

【问题讨论】:

  • 您可能需要在 main.py 中使用 waitkey 来等待某些键破坏您的窗口。并且还使用 destroyAllWindows

标签: python pyinstaller


【解决方案1】:

这个答案很好地解释了为什么会发生这种情况:https://stackoverflow.com/a/55382641/109525

在启动程序之前先尝试设置:

export JOBLIB_MULTIPROCESSING=0

如果可行,您可以在程序中添加运行时挂钩以自动设置环境变量。见:https://pythonhosted.org/PyInstaller/when-things-go-wrong.html#changing-runtime-behavior

【讨论】:

  • 我对使用sklearn 并使用PyInstaller 为Mac 构建的应用程序有同样的问题。我可以通过在我的应用程序开头添加这个来解决这个问题:if getattr(sys, 'frozen', False): os.environ['JOBLIB_MULTIPROCESSING'] = '0'
【解决方案2】:

我的主要入口点是:

from multiprocessing import freeze_support
freeze_support()

为我工作!

【讨论】:

  • 完美运行!当尝试从wxpython 中运行并行进程时,它只是启动了一个新的 UI。作为参考,以下是我如何包装 UI 函数以使其与多处理一起使用:from multiprocessing import freeze_support; from my_gui import open_gui; freeze_support(); open_gui() 然后使用pyinstaller 导出此脚本@
【解决方案3】:

我遇到了同样的问题。 Scikit-learn 提供了一种解决方法。

https://scikit-learn.org/stable/faq.html#why-do-i-sometime-get-a-crash-freeze-with-n-jobs-1-under-osx-or-linux

我在脚本开头添加了以下代码并修复了我的问题:

import multiprocessing
multiprocessing.set_start_method('forkserver', force=True)
multiprocessing.freeze_support()

希望这也能解决你的问题。

【讨论】:

  • 谢谢。现在程序不会无限分叉进程但是,当我检查后台进程时,它显示/path/to/main -B -s -S -E -c from multiprocessing.semaphore_tracker import main:main(7)。知道为什么会有这样的过程吗?
猜你喜欢
  • 2021-11-02
  • 1970-01-01
  • 2022-10-21
  • 2016-12-08
  • 1970-01-01
  • 2019-07-07
  • 1970-01-01
  • 2021-07-12
  • 2014-12-22
相关资源
最近更新 更多