python - Pyinstaller 可执行文件不断打开

标签 python pyinstaller

背景

我在 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 interpret 运行脚本(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(问题已找到但未修复)

调试了一段时间的代码,发现这个无限循环的原因不是imultis VideoStream的threading(我写了另一个脚本测试 imultis 完全没问题)。但问题来自于导入 sklearn!!!此问题已在 GitHub 中报告 this link .

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

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

最佳答案

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

在开始你的程序之前首先尝试设置它:

export JOBLIB_MULTIPROCESSING=0

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

关于python - Pyinstaller 可执行文件不断打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54662306/

相关文章:

Python - 从文件填充数组,避免最后断行

python - pyinstaller 和 reportlab 的问题

python - 请求模块的 PyInstaller SSL 错误 - 缺少模块 ssl (_ssl)

python - Pyinstaller EXE 的 __file__ 指的是一个 .py 文件

python - 使用 Django 生成 LaTeX PDF 时出现 FileNotFoundError

python : in which timezone is it a specific time right now?

python - 动态规划,带约束的最大子数组

python - 为什么修改 Controller 后不需要重新启动 Rails? Python Web 框架中是否存在这样的东西?

python - os.path 在多个文件导入中使用 Pyinstaller 指向错误的位置

python - Pyinstaller 和 Pycrypto